GHOST

Time limit1sMemory limit128 MB

Problem

GHOST is a spelling game played by bored students on long trips. The goal is to build up letters that spell the beginning of some word without ever actually finishing a word. Before the game starts, the players agree on a fixed turn order. Play passes from one player to the next and then wraps back to the first player, repeating until the game ends. On each turn a player must do exactly one of three things: extend the current sequence, bluff, or challenge.

  1. Extend. The usual play adds a single letter to the current sequence so that the result is still the beginning of some word. For example, the first player might say P (secretly thinking of part), the second L (thinking of play), and the third E (thinking of please). A player loses if they actually complete a valid word of 4 or more letters. With only three players, after PLE the first player adding A (aiming for plead) would lose, because plea is already a valid word.
  2. Bluff. A player who cannot think of a valid letter may call out an arbitrary letter and hope the next player does not notice.
  3. Challenge. A player who suspects the previous player bluffed or completed a word may challenge them. If everyone agrees the current sequence completes a word of at least 4 letters, the previous player loses. If the previous player cannot name a word beginning with the current sequence, the previous player loses. If the sequence is not a complete word and the previous player can name a word starting with it, the challenger loses.

Write a program that takes one turn of GHOST as the computer player. A strong player does more than find any legal extension: it weighs every word the sequence might grow into against the number of players, so that it is never forced on some later turn to be the one who completes a word.

Input

The input contains one or more scenarios.

Each scenario is given as follows:

  • One line with a single integer: the number of players. It is at least $2$ for a valid scenario. A value smaller than $2$ marks the end of the input.
  • The dictionary for this scenario: a list of words, one per line. Every word consists only of the letters az, with no leading, trailing, or internal spaces. An empty line ends the word list.
  • One more line holding the current sequence of letters, with no leading or trailing spaces. This sequence may be empty (when the computer plays first), and it may be longer than the number of players (when every player has already taken one or more turns).

Output

For each scenario, print exactly one line: the current sequence from the input, then a single space, then one of the following.

  • Challenge — if the current sequence is itself a word in the dictionary, or is not a prefix of any word in the dictionary.
  • A single letter — a valid extension. The computer is about to play the letter at position $\text{len}+1$ (positions are counted from $1$), so it plays every position $p$ with $p \equiv (\text{len}+1) \pmod{k}$, where $k$ is the number of players and $\text{len}$ is the length of the current sequence. A letter c is a valid extension when: the sequence followed by c is a prefix of some word; that extended sequence does not itself complete a word of $4$ or more letters; and there is a dictionary word $W$ beginning with the extended sequence such that, if the remaining letters of $W$ are spelled out one per turn, the first word of $4$ or more letters completed along the way does not land on the computer's turn. If several letters qualify, print the alphabetically smallest one.
  • Bluff — if at least one extension exists but every extension would eventually force the computer itself to complete a word.