Under construction

Still being built. Things may change

The Killer Word (Small)

Interview

Time limit5sMemory limit512 MB

Summary
Pick the dictionary word that makes Sean miss the most guesses under his order-based guessing rule, breaking ties by dictionary order.
Level

Medium5 of 10

Topics
Simulation, String, Brute force
Solved
No attempts yet

Problem

You are playing Hangman against your friend Sean. Sean is not good at this game. Use his imperfect strategy to make him lose as many points as possible.

 +--+
 |    O
 |   / | \       Mystery word: _ a _ a _ a _
 |    /  \
 |
+-+---+

Hangman is played as follows.

  • There is a dictionary D of all valid words, and both you and Sean know it. A word consists only of the characters a to z, so there are no spaces.
  • You begin by choosing any word from D and writing it on a blackboard with every letter replaced by a blank _.
  • On his turn, Sean chooses a letter and asks whether it is in the word. If it is, you reveal all positions of that letter. If it is not, Sean loses one point.
  • Once every letter of the word has been revealed, the round ends.
  • The round never ends early, no matter how many points Sean loses.

Sean uses a very simple strategy. He makes a list L of the 26 letters in some order and walks through it one letter at a time. If D contains at least one word that (a) has the letter he is thinking of and (b) agrees with what you have written down so far and with the results of all of Sean's previous guesses, then Sean guesses that letter. Otherwise he skips it. Either way, Sean then moves on to the next letter of the list.

Given Sean's list, which word should you choose to make Sean lose as many points as possible? If several choices cost him the same number of points, choose the one that appears first in D.

How the rules apply

Suppose Sean decides to guess the letters in alphabetical order (that is, L = abcdefghijklmnopqrstuvwxyz) and D contains the words banana, caravan and pajamas. If you choose pajamas, the round plays out like this.

  • You begin by writing 7 blanks _ _ _ _ _ _ _ on the blackboard. From the number of blanks Sean knows at once that the word is either caravan or pajamas.
  • Sean guesses a because it comes first in L, and you reveal every position of a: _ a _ a _ a _.
  • Sean skips b, even though banana uses it. He already knows that banana is not your word.
  • He then guesses c because it appears in caravan. It does not appear in the word you actually chose, so Sean loses a point and nothing more is revealed.
  • Only pajamas is left, so Sean guesses j, m, p and s in that order without losing any more points.

Choosing pajamas costs Sean one point. Either of the other two words would have cost him nothing.

Input

The first line of the input gives the number of test cases T. T test cases follow. Each test case begins with a line containing the integers N and M, the number of words in the dictionary and the number of lists to consider.

The next N lines contain the words of the dictionary, one per line: D1,D2,…,DND_1, D_2, \dots, D_N. Each word is an arbitrary sequence of the characters a to z.

The final M lines contain all of the lists Sean will use, one per line: L1,L2,…,LML_1, L_2, \dots, L_M. Each list is exactly 26 letters long and contains each letter exactly once. Sean uses these lists to guess letters as described above.

Limits

  • 1≤T≤101 \le T \le 10
  • 1≤N≤1001 \le N \le 100
  • 1≤M≤101 \le M \le 10
  • Every word in D has between 1 and 10 characters.
  • No two words in D are the same within a single test case.

Output

For each test case, output one line in the form Case #x: w1 w2 ... wM, where x is the test case number (starting from 1) and wiw_i is the word you should choose when Sean guesses the letters in the order LiL_i. If several words cost Sean the same number of points, output the one that appears first in the dictionary.

Examples3

  1. Example 1

    Input
    2
    3 2
    banana
    caravan
    pajamas
    abcdefghijklmnopqrstuvwxyz
    etaoisnhrdlcumwfgypbvkjxqz
    4 1
    potato
    tomato
    garlic
    pepper
    zyxwvutsrqponmlkjihgfedcba
    
    Expected output
    Case #1: pajamas caravan
    Case #2: garlic
    
  2. Example 2

    Input
    1
    1 1
    a
    abcdefghijklmnopqrstuvwxyz
    
    Expected output
    Case #1: a
    
  3. Example 3

    Input
    1
    4 1
    ca
    ac
    ab
    ba
    abcdefghijklmnopqrstuvwxyz
    
    Expected output
    Case #1: ca