Find the Hidden Word

No attempts yetTime limit1sMemory limit256 MB

Problem

A white rabbit wants to send one word to a black rabbit without letting any other rabbit find out which word it is. The two rabbits agreed on a scheme beforehand, and the white rabbit handed the black rabbit the full list of words it knows. When the white rabbit sends a word, it mixes many other letters in front of the word, behind it, and around it, and sends the whole thing as one long message.

For a message, find every word on the list that occurs in the message as a contiguous substring. If exactly one distinct word occurs, that word is the one the white rabbit meant to send. Help the black rabbit answer for each message.

Input

The first line has the number of test cases TT (1T21 \le T \le 2). Each test case has the following form.

  1. The first line has the number of words the white rabbit knows, NN (1N2000001 \le N \le 200000).
  2. Each of the next NN lines has one known word. The length KiK_i of a word is at least 6 and at most 50, and a word uses lowercase English letters only.
  3. The next line has the number of messages the white rabbit sent, MM (1M101 \le M \le 10).
  4. Each of the next MM lines has one message. The length LiL_i of a message is at least 6 and at most 10000, and a message uses lowercase English letters only.

Output

Print one line for each message. First count the distinct words the white rabbit knows that occur in the message as a contiguous substring.

  1. If there is no such word, print NO.
  2. If there is exactly one, print that word as it is.
  3. If there are two or more, print AMBIGUOUS.