Under construction

Still being built. Things may change

Squirrels

Time limit2sMemory limit1024 MB

Summary
Given the occupied hollow numbers in each tree, find the lowest free hollow (at least 1) in the tree each new squirrel chooses.
Level

Medium5 of 10

Topics
Hash map, Implementation, Binary search
Solved
No attempts yet

Statement

In a magical forest, magical trees grow. So that the trees can talk to each other, each tree has its own name. They do not want to disturb each other for no reason, so all names are different.

Magical squirrels live in magical hollows inside the magical trees. Each hollow has a number, and the higher the hollow, the larger its number. So the squirrels do not get lost, a wise owl gave each squirrel a magical note with its address, which lists the tree name and the hollow number in that tree. The owl also records the addresses of all squirrels in her magic notebook.

From time to time, new squirrels appear in the forest and want to move into a hollow. Each of them has already chosen a tree to live in, but some hollows in that tree are already taken. The squirrel asks the wise owl to suggest a free hollow in that tree. All squirrels are lazy, so they want the hollow to be as low as possible. The tree is magical, so it has as many hollows as needed, and all of them are at different heights. Help the owl choose a hollow for each squirrel.

Input

The first line contains nn, the number of entries in the owl's notebook. The next nn lines contain the entries. Each entry is the tree name concatenated with the hollow number where the squirrel lives. A tree name is a string of lowercase Latin letters with a positive length. The hollow number is a natural number not exceeding 10910^9.

The next line contains the number of new squirrels. Each of the following lines gives the name of the tree where that squirrel wants to live. The input file does not exceed 10610^6 bytes.

Output

For each squirrel, print the number of the lowest free hollow where it can settle.

Examples1

  1. Example 1

    Input
    5
    stepan1
    vasiliy4
    stepan4
    stepan3
    vladimir1000000
    3
    stepan
    vladimir
    stepan
    
    Expected output
    2
    1
    5