#C8978. Anagram Finder

    ID: 53019 Type: Default 1000ms 256MiB

Anagram Finder

Anagram Finder

Given a string \(s\) and a dictionary of words, your task is to find all unique valid anagrams of \(s\) that appear in the dictionary. An anagram is any rearrangement of the characters of \(s\). The total number of possible permutations is given by \(n!\), where \(n\) is the length of the string \(s\). The output should be sorted in lexicographical order.

Example:

Input:
ate
6
eat
tea
tan
ate
nat
bat

Output: ate eat tea

</p>

If there are no valid anagrams, print an empty line.

inputFormat

The input is given via standard input (stdin) in the following format:

  1. The first line contains the string \(s\).
  2. The second line contains an integer \(n\), representing the number of words in the dictionary.
  3. The following \(n\) lines each contain one word from the dictionary.

outputFormat

Print a single line to standard output (stdout) containing all valid anagrams of \(s\) that exist in the dictionary, sorted in lexicographical order and separated by a single space. If there are no valid anagrams, output an empty line.

## sample
ate
6
eat
tea
tan
ate
nat
bat
ate eat tea