Word Ladder

No attempts yetTime limit1sMemory limit256 MB

Problem

The minions keep studying English, and they play games to grow their vocabulary. One of the games is the word ladder. A word ladder, also called Doublets, word links, or word golf, is a word game invented by Lewis Carroll.

A puzzle starts with two words. You solve it by finding a chain of words that links the two, where any two neighbouring words in the chain differ in exactly one letter.

For example, given COLD and WARM, this is a word ladder.

COLD --> CORD --> CARD --> WARD --> WARM

This is another one.

COLD --> WOLD --> WORD --> WARD --> WARM

The player who finds the shortest ladder between the two given words wins. If two players find ladders with the same number of words, compare the ladders word by word from the front. At the first position where the ladders differ, the player whose word comes earlier in lexicographic order wins. Comparing the two ladders above, CORD comes before WOLD, so the first ladder wins.

Kevin the minion plays with one fixed dictionary. Every word of a ladder, including the two end words, must be in that dictionary. Two words are neighbours only when they have the same length and differ in exactly one position, so words of different lengths are never neighbours. Find the winning ladder for each pair of words.

Input

The first line contains the number of test cases TT.

Each test case gives one dictionary and a list of word pairs. It begins with the number of words in the dictionary NN, followed by the NN words. Then comes the number of pairs QQ for which you have to find a ladder, followed by the QQ pairs. One pair is two words.

All numbers and words are separated by whitespace, and the line breaks are arbitrary. Every word consists of lowercase English letters (gru, not Gru).

  • 1T91 \le T \le 9
  • 1N10001 \le N \le 1000
  • 1Q201 \le Q \le 20
  • Each word has length between 11 and 1010.
  • The words of one dictionary are pairwise distinct.
  • Both words of every pair belong to that dictionary.

Output

For each pair, in the order given in the input, print one line.

If a ladder exists, print it in this format.

Word ladder from A to B: w1 --> w2 --> ... --> wk

Here AA is the first word of the pair, BB is the second word, and w1,,wkw_1, \dots, w_k are the words of the ladder. Print a ladder with the fewest words; if several shortest ladders exist, print the one whose word sequence comes first in lexicographic order. Join the words of the ladder with --> (a space, two hyphens, a >, and a space).

If no ladder exists, print this line.

No word ladder from A to B using the input dictionary.

When AA and BB are the same word, the ladder is that single word, so the line reads Word ladder from A to A: A.