Jumbled Letters

No attempts yetTime limit1sMemory limit128 MB

Problem

In the tile-based word game Scrabble you are given a number of tiles, each printed with a single letter. The challenge is to build a word using some (or all) of these tiles. Write a program that, with the help of a dictionary, finds the longest word you can make from the given letters. As in the game, we ignore the board the tiles are placed on, as well as the point value of each letter.

Input

The input consists of:

  • one line with an integer $n$ ($1 \le n \le 100,000$): the number of words in the dictionary;
  • $n$ lines, each with one distinct word made of between 2 and 10 lower-case letters;
  • one line with an integer $c$ ($1 \le c \le 10,000$): the number of test cases;
  • $c$ lines, each with a string of between 2 and 10 lower-case letters: the letters you must build a word from.

The dictionary words are listed in alphabetical order.

Output

For each test case, print one line with the longest dictionary word you can build from the given letters, using each given letter at most once. If several words share that maximum length, print the one that comes first alphabetically. If no word can be built, print IMPOSSIBLE.