#P8185. Bessie's Vocabulary Challenge

    ID: 21367 Type: Default 1000ms 256MiB

Bessie's Vocabulary Challenge

Bessie's Vocabulary Challenge

Bessie the cow is working hard to improve her vocabulary. She has obtained a set of four wooden blocks, each of which is a cube with a letter on each of its six faces. By arranging the blocks in a row, the letters on top of each block can form words.

Given the letters on each of Bessie's four blocks and a list of words she wants to spell, determine which words she can successfully form.
Note that each block can be used at most once per word. In other words, for a word of length \( L\), you must select \( L\) distinct blocks (out of 4) and use one letter from each.

inputFormat

The input consists of several lines:

  1. The first 4 lines each contain a string of 6 characters representing the letters on one block.
  2. The next line contains an integer N representing the number of words in Bessie's list.
  3. The following N lines each contain a word Bessie wishes to spell.

Note: Words with length greater than 4 cannot be formed because Bessie only has 4 blocks.

outputFormat

Output each word that Bessie can form, each on a new line, in the same order as they appear in the input. If no word can be formed, output nothing.

sample

abcdeg
cdefgh
ijkabc
lmnopq
3
bag
face
dog
bag

</p>