Under construction

Still being built. Things may change

Spelling Bee

Interview

Time limit3sMemory limit1024 MB

Summary
Given a 7-letter Spelling Bee hexagon and a word list, the task is to print every word of length at least 4 that uses only those letters and contains the center letter.
Level

Easy3 of 10

Topics
String, Implementation
Solved
No attempts yet

Problem

The New York Times publishes a daily puzzle called "Spelling Bee." The puzzle shows 7 letters in a hexagon: 6 letters around a center letter. The goal is to find as many words as possible that meet all of these conditions:

  • They contain only letters shown in the hexagon.
  • They are at least 4 letters long.
  • They contain the center letter.

A letter may be used more than once, and not all letters need to be used.

After playing for a while, you get stuck. Then you remember that the Linux distribution on your computer includes a machine-readable file, /usr/share/dict/words, with 102,305 dictionary words.

You cannot excel at Spelling Bee, but you can excel at programming. So you decide to write a program that finds all solutions to a Spelling Bee puzzle in the dictionary.

Input

The input is a single test case. The first line has 7 distinct lowercase English letters. The first letter is the center letter. The next line contains an integer nn (1≤n≤102 3051 \le n \le 102\,305), the size of the dictionary. This is followed by nn lines, each containing a dictionary word of ll lowercase English letters (1≤l≤241 \le l \le 24).

Output

Print the words that match the Spelling Bee puzzle, in the order they appear in the dictionary, one per line. The input guarantees that at least one dictionary entry matches.

Examples1

  1. Example 1

    Input
    drulyag
    27
    dryad
    duly
    spelling
    multiplexed
    janna
    lard
    dryly
    the
    instances
    gradual
    gradually
    dual
    inimically
    off
    dullard
    grad
    equipage
    gladly
    mauritania
    drug
    a
    drag
    pickering
    yard
    daddy
    on
    lallygag
    
    Expected output
    dryad
    duly
    lard
    dryly
    gradual
    gradually
    dual
    dullard
    grad
    gladly
    drug
    drag
    yard
    daddy