cho.sh
Notes
Loading...

Card Placement

Time limit

2s

Memory limit

128 MB

Problem

There are N cards and N bins. Each card has a positive integer and one uppercase English letter written on it. Each bin also has a positive integer written on it.

A card with number x may be placed into a bin with number b only when b <= x. Every card must be used exactly once, and every bin must receive exactly one card.

After the cards are placed, read the letters of the cards from the first bin to the last bin to form a string. Find the lexicographically smallest string that can be made under the rule. If no valid placement exists, output -1.

Input

The first line contains the number of cards N (1 <= N <= 50).

Each of the next N lines contains one card description: a positive integer x and one uppercase English letter, separated by a space. The integer on a card is at most 1000.

The last line contains N positive integers, separated by spaces. These are the numbers written on the bins in order, and each is at most 1000.

Output

Print the lexicographically smallest string that can be formed. If the cards cannot be placed according to the rule, print -1.