A Word Puzzle in the Sunny Mountains

During their honeymoon in the Himalayas, Mr and Mrs Smith spent part of their time playing games (crosswords, cryptograms, etc.). One of the most exciting is a very simple crypto-game: given a list of words encrypted with numbers (positive integers) the objective is to find the correspondence table that associates a letter (’A’ to ’Z’) with each number in such a way that all the words in the list, after replacing numbers by the corresponding letters, become valid words according to a given dictionary. To start the game, the first word, called keyword or seed, is given, asserting the value (the corresponding letters) of the first numbers. To help them save precious time in their honeymoon, we are asking your assistance. Your task is to write a program that reads the input data (list of encrypted words, seed, and dictionary) and plays the game, printing out the solution (the mapping that applies numbers to letters and solves the problem). Notice that every puzzle proposed has one, and just one, solution. Input The first line of input contains C (0 < C < 10), the number of test cases that follows. Each test case is organized as follows: The first line contains the number 1 ≤ l ≤ 26 of different letters to be decode; The second line contains the number 1 ≤ n ≤ 100 of encrypted words; The next n lines contain the encrypted words to be decoded, one word per line represented by numbers, used sequentially starting in 1 (one number replacing the same letter, different numbers correspond to different letters). Numbers are separated by single spaces and the sequence ends with a 0. The length of a word is never less than 2 or greater than 15; The next line is the keyword or seed, i.e., the first word of the list to be decoded, formed by upper case letters; The remaining lines contain the words (upper case letters) of the dictionary (1 ≤ m ≤ 100 lines, m words of length not greater than 15). The end of the input for each case is marked by a line with an asterisk. Output The output is formed by a sequence of lines, one for each test case. Each line contains a string made up of l uppercase letters (l representing the number of letters to be decoded) describing the correspondence table that solves the problem for the respective test case. In each string, the first character is the letter associated with number 1, the second is the letter associated with number 2, and so on, and the last character is the letter associated with the last number to be decoded (the number l). Sample Game Below we show the input data and the output for a single game with 7 encrypted words and 11 numbers encoding the 11 different letters to be discovered. The solution is the map shown as Sample Output, and the list of valid words (existing in the given dictionary) that can be written with the correspondence table, discovered by the player, is: ADORNO ANA AMOR HORTA

2/2 PORTA PAPEL ELMO Note that in general, each input file corresponds to a sequence of games, as explained, and not to a single game, as the sample input illustrates. Sample Input 1 11 7 1234530 1510 16340 734810 934810 9 1 9 10 11 0 10 11 6 3 0 ADORNO ADORNO AMOR ANA ARLINDO ANTUNES HORTA PORTA PORTAO PORTAL PAPEL PARVO ELMO ESTE ESSE ARMADURA HELENA ERGONOMICO EVA ERVA CARMO COUVE

end{verbatim} \subsection*{Sample Output} \begin{verbatim} ADORNMHTPEL