Under construction

Still being built. Things may change

Bonus Word

Time limit1sMemory limit128 MB

Summary
Find every pair of dictionary words that joins to ten letters and matches each query fixed and shuffled letters, listed in order.
Level

Medium5 of 10

Topics
String, Brute force, Sorting
Solved
No attempts yet

Problem

Lingo was once a popular game show where contestants guess words. In the original version a contestant guesses a five-letter word each round.

Between the regular guessing rounds, a contestant wins a bonus prize by guessing a ten-letter word. The ten-letter word is shown with its letters permuted, and a few of the letters are colored. A colored letter is already in the right position. Ten-letter words are not that common, so the word is often a compound: two shorter words written one after the other. In this problem the ten-letter word always has that form.

Given a dictionary and a sequence of ten letters, compute every possible solution to the ten-letter word game. Two solutions count as different when they are built from different parts, even if the concatenation is the same.

Input

The first line has an integer tt (1≤t≤1001 \le t \le 100), the number of test cases. Each test case is given as follows.

  • One line with an integer nn (1≤n≤2001 \le n \le 200), the number of words in the dictionary.
  • nn lines, each with one dictionary word. Each word is 1 to 9 characters long and consists of lowercase letters only.
  • One line with an integer qq (1≤q≤1001 \le q \le 100), the number of queries.
  • qq lines, each with one query string. Each query is exactly 10 characters long and consists of uppercase and lowercase letters. A lowercase letter is in the right position, and an uppercase letter may be in the wrong position.

All dictionary words inside a single test case are distinct.

Output

For each query, print the following.

  • One line with an integer ss, the number of solutions.
  • min⁡(1000,s)\min(1000, s) lines, each holding one solution written as two dictionary words joined by a hyphen (-).

Order the solutions of one query lexicographically. If there are more than 1000 solutions, print only the first 1000.

Hint

Let xx and yy be strings of equal length and let xix_i be the ii-th character of xx. Then xx precedes yy lexicographically when there is an ii with xi<yix_i < y_i and xj=yjx_j = y_j for every j<ij < i. In this problem the hyphen precedes every letter.

Examples1

  1. Example 1

    Input
    2
    5
    gunner
    integral
    relating
    tail
    un
    4
    TAILGUNNER
    unINTEGRAL
    UNrelating
    IMPOSSIBLE
    3
    aaaa
    aaaaa
    aaaaaa
    1
    AAAAAAAAAA
    
    Expected output
    6
    gunner-tail
    integral-un
    relating-un
    tail-gunner
    un-integral
    un-relating
    2
    un-integral
    un-relating
    1
    un-relating
    0
    3
    aaaa-aaaaaa
    aaaaa-aaaaa
    aaaaaa-aaaa