Abandoned Animal

Time limit2sMemory limit512 MB

Summary
Given each store's stock and the ordered list of purchases, count how many store assignments make store numbers nondecreasing: zero, one, or many.
Level

Medium6 of 10

Topics
Greedy, Array, Hash map
Solved
No attempts yet

Problem

Your little sister did all the groceries in town today. Her stuffed animal, Mr. Fluffynose, came along for the whole trip, and when she got home the toy was gone. She let go of it only while she was picking up an item from her shopping list, so the toy stayed behind in one of the stores where she actually bought something.

She remembers which stores she visited and in what order, but not what she bought in each one. She may well have bought nothing at all in some of them. The purchase order she does remember exactly, because she stacked every item into her bag as she paid for it.

Number the stores 00 to N−1N-1 in the order she visited them. A possible path assigns every purchase to one store that sells that item, so that the store numbers read in purchase order never decrease. Given the stock of each store and the purchases in order, decide whether there is no such path, exactly one, or more than one.

Input

The first line contains an integer NN, the number of stores in town (1≤N≤100 0001 \le N \le 100\,000).

The second line contains an integer KK (N≤K≤100 000N \le K \le 100\,000).

Each of the next KK lines contains an integer ii (0≤i≤N−10 \le i \le N-1) and a string SS, separated by a space, meaning that item SS is sold at the ii-th store your sister visited. SS consists of lowercase letters only and has length at most 1010. Every store sells at least one item, every item is sold by at least one store, and the same item never appears twice at one store.

The next line contains an integer MM, the number of items your sister bought (M≤KM \le K). Each of the following MM lines contains a string TT, the name of one purchased item. The items are listed in the order she bought them, and they are all different.

Output

Print impossible if no path matches your sister's description, unique if exactly one path matches, and ambiguous if more than one path matches.

Examples3

  1. Example 1

    Input
    3
    3
    0 chocolate
    1 icecream
    2 cookies
    3
    chocolate
    cookies
    icecream
    
    Expected output
    impossible
    
  2. Example 2

    Input
    3
    4
    0 chocolate
    1 icecream
    2 cookies
    2 chocolate
    3
    chocolate
    icecream
    cookies
    
    Expected output
    unique
    
  3. Example 3

    Input
    3
    10
    0 tomatoes
    0 cucumber
    1 tomatoes
    2 tomatoes
    2 cucumber
    1 mustard
    0 salt
    2 salad
    2 salt
    2 mustard
    5
    tomatoes
    cucumber
    salad
    mustard
    salt
    
    Expected output
    ambiguous