This page is still under construction.

Parts of this page are still being built. What you see may change.

Puzzle Out

Time limit1sMemory limit128 MB

Summary
Given a dictionary and an encrypted uppercase text, recover the substitution cipher table or report no solution or multiple solutions.
Level

Hard8 of 10

Topics
Backtracking, Hash map, String, Brute force
Solved
No attempts yet

Problem

The scientific committee of the 26th ACM/ICPC, who design the contest problems, use the following encryption scheme to exchange problem drafts securely over the Internet. To encrypt a text, every occurrence of each letter is replaced by another letter (possibly itself), so that no two distinct letters are encrypted to the same letter — that is, the substitution is a bijection over the 26 uppercase letters. Both the original and the encrypted texts consist only of uppercase letters and blanks; blanks are never encrypted and are copied to the encrypted text exactly. For example, under the table A→Z, B→Y, C→X, …, Z→AA \to Z,\ B \to Y,\ C \to X,\ \dots,\ Z \to A, the string THIS IS THE FIRST SAMPLE is encrypted as GSRH RH GSV URIHG HZNKOV.

A recipient has lost the encryption table, but has a dictionary that contains every word that may appear in the problems. Help him build a decryption table that restores the original draft from the encrypted one. Given the dictionary of possible original words and the encrypted text, find an encryption table such that decrypting the encrypted text yields a text in which every word occurs in the dictionary.

Input

The first part of the input is a dictionary shared by all test cases. Its first line contains dd (1≤d≤500001 \le d \le 50000), the number of dictionary words, followed by dd lines each holding one word. The dictionary words are given in alphabetical order and are all uppercase. Each word has at most 2020 characters, and the total length of all dictionary words is at most 350,000350{,}000.

The next line contains a single integer tt (1≤t≤101 \le t \le 10), the number of test cases. Each test case is preceded by a single blank line and consists of one or more lines that together form the encrypted text. Every such line contains only uppercase letters and blanks. No word is split across a line break, and each line may contain any number of trailing blanks. The length of each input line is at most 8080.

Output

Print exactly tt lines, one per test case. Each line must be a string of 2626 uppercase characters: the encryption of ABCDEFGHIJKLMNOPQRSTUVWXYZ under the encryption table used in that test case (position ii is the letter that the ii-th letter of the alphabet is encrypted to). Some letters may never appear in the decrypted text; for every such letter, print * at its position instead. If the test case has no valid table, print #No solution#. If more than one encryption table is possible (that is, the decrypted text is not uniquely determined), print #More than one solution#.

Examples1

  1. Example 1

    Input
    14
    BE
    CHANGE
    FIRST
    IN
    IS
    MUST
    SAMPLE
    SEE
    THE
    THIS
    TO
    WISH
    WORLD
    YOU
    4
    
    GSRH RH GSV URIHG HZNKOV
    
    IZM BMVU    SP UGP
    RGTANP IZM KFVG UZ VPP
    FA UGP KZWCQ
    
    XYZ ABCDEFG
    
    XZY ABD
    
    
    Expected output
    Z***VU*SR**ON**K*IHG******
    TSRQP*NGF**CBAZ**WVUM*K*I*
    #No solution#
    #More than one solution#