Abandoned Animal
Time limit2sMemory limit512 MB
Given each store's stock and the ordered list of purchases, count how many store assignments make store numbers nondecreasing: zero, one, or many.
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 to 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 , the number of stores in town ().
The second line contains an integer ().
Each of the next lines contains an integer () and a string , separated by a space, meaning that item is sold at the -th store your sister visited. consists of lowercase letters only and has length at most . 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 , the number of items your sister bought (). Each of the following lines contains a string , 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.