Crypt Kicker

Time limit1sMemory limit128 MB

Summary
Decrypt each line of a substitution cipher so every word appears in a given dictionary, choosing the lexicographically smallest result, or mask the line if none exists.
Level

Hard8 of 10

Topics
Backtracking, String, Brute force, Implementation
Solved
No attempts yet

Problem

A common but insecure way to encrypt text is to replace the letters of the alphabet with a permutation of themselves. In the ciphertext, each letter of the alphabet is consistently replaced by some other letter. To keep the encryption reversible, no two letters are replaced by the same letter (the substitution is a one-to-one mapping).

Your task is to decrypt several encoded lines of text. Each line uses its own, independent set of replacements, and every word in the decrypted text must appear in a given dictionary of known words.

Input

The first line contains an integer nn, followed by nn lowercase words, one per line, in alphabetical order. These nn words form the dictionary of words that may appear in the decrypted text.

After the dictionary come several lines to be decrypted, until the end of input. Each line is encrypted as described above.

There are at most 10001000 dictionary words, and no word exceeds 1616 letters. Each encrypted line contains only lowercase letters and spaces and is at most 8080 characters long.

Output

Decrypt each line and print it to standard output. If a line has more than one valid decryption, print the lexicographically smallest decrypted line. If a line has no valid decryption, output the line with every lowercase letter replaced by an asterisk (*), leaving the spaces unchanged.

Examples3

  1. Example 1

    Input
    6
    and
    dick
    jane
    puff
    spot
    yertle
    bjvg xsb hxsn xsb qymm xsb rqat xsb pnetfn
    xxxx yyy zzzz www yyyy aaa bbbb ccc dddddd
    
    Expected output
    dick and jane and puff and spot and yertle
    **** *** **** *** **** *** **** *** ******
    
  2. Example 2

    Input
    2
    cat
    dog
    xyz
    
    Expected output
    cat
    
  3. Example 3

    Input
    1
    cat
    abcd
    
    Expected output
    ****