SCUSA Region ICPC Masthead ACM Balloon Logo
2011 ACM ICPC South Central USA Regional Programming Contest

3 - Periodic, My Dear Watson

Given the periodic table and a series of words, determine whether the word can be formed using the elements in the periodic table. If there are multiple ways of doing so, select the one with the fewest number of required elements. If multiple options use the same minimal number of elements, sum the atomic numbers (atomic numbers are implied by the order in which the elements are read in) used for each option, and select the one with the smallest value. If there are still multiple, then there's probably no point, as it is most likely too obvious, so in these cases, simply print “Too Obvious” for the given word.

An 'element' always starts with a capital letter followed by zero or more lower case letters.

Input

Input will begin with a line containing a single integer, N representing the number of cases to process. Each case will being with a line with a single integer, O, representing the number of symbols in the case. The next O lines will contain one element per line. The next line will contain a single integer, P representing the number of words to process in this case. The next P lines contain one word each. O and P as well as the length of any element or name will be less than 5000.
<Number of cases>
<number of elements>
<elements...>
<number of words>
<words...>

Output

If a given word has a deterministic answer based on the periodic table provided and the above stated rules, print the case number and the series of elements used, forming the word, but with square brackets around them to indicate the elements. One example output line might be: “[Ba][Na][Na]”

Sample InputSample Output
1
4
Ba
Na
N
A
2
banana
nan
[Ba][Na][Na]
[Na][N]