numbers and LETTERS

Welcome, contestant. ?‘Do you like letters? Because in the today test you must solve the famous game of the letters, of the well known game show “Numbers and Letters”. (Yes, really, it is a well known game show; if you do not know it, it is your fault. It was broadcasted in the 90s in La 2, and it is still broadcasted in some autonomic televisions; the original French game show, “Des chiffres et des lettres”, is being broadcasted since 1972).

The test of Letters consists of: given 9 letters, possibly some of them repeated, you have to form with them the longest possible word. For instance, if the letters were

G Y H D N O E U R

we could form the word younger or, better still, the word hydrogen.

Input

The input consists of a number n<1000n<1000 in a line, followed by exactly nn lines, each one of them contains a word (lowercase letters) of at most 99 letters. These nn words form the dictionary of valid words in the game. Then, a natural k<100k<100, with number of cases to solve, followed by kk lines, each one of them contains a case (exactly 9 uppercase letters).

Your program must solve all the cases in 1 second of time.

Output

Exactly kk lines, one for each case, with the longest word (with uppercase letters) of the dictionary that is possible to form using the letters of the case, or a line in white if it not possible to write any of the words. If there are more than one word of maximal length, it must print the word that appears first in the dictionary.

Problem information

Author: Unknown
Translator: Carlos Molina

Generation: 2026-01-25T11:17:37.353Z

© Jutge.org, 2006–2026.
https://jutge.org