Strange Research

Aoccdrnig to a rseearch at an Elingsh uinervtisy, it deosn’t mttaer in waht oredr the ltteers in a wrod are, the olny iprmoatnt tihng is taht the frist and lsat ltteer is at the rghit pclae. The rset can be a toatl mses and you can sitll raed it wouthit any porbelm. Tihs is bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe. I hope you did not have much trouble reading the paragraph above and have realized that the research result is somewhat true. If you are still having trouble reading the paragraph above go to the end of this problem to see the actual paragraph. At first glance this research may appear as a joke but if you think for quite sometime you will find that this is not a joke at all since words of length 1, 2, 3 and 4 are merely affected by the scrambling that is being suggested here. And in a normal English text, words of length less than five makes almost 62% of the total words. However to solve this problem you neither need this statistics nor need to prove the correctness of this research. In this problem you will be given a dictionary of correct words and list of scrambled words. The first and the last letter of a scrambled word are the same as the original word. Position of letters between them may or may not be altered. Your job is to find out the total number of words in the dictionary that can actually be the correct form of the given scrambled word. You also need to find the lexicographically smallest and largest such word in the dictionary, when the word is present in the dictionary. Input The input file contains a single set of input. The first line of the input file is an integer N (0 < N < 500001) which indicates how many words are there in the given dictionary. Each of the next N lines will contain one word. All the words have less than 9 characters. The very next line contains an integer Q (Q < 50001) which indicates the total number of query. Each of the next Q lines contain 1 word (All query words have less than 16 characters). All words in the input file are made with lowercase alphabets. Output For each query you should output a single line. This single line should contain an integer F (must) and two strings S1 and S2 (These two strings will be present only when F > 0). The integer F denotes how many words are there in the dictionary that can be the correct form of the scrambled word in the query. S1 is the lexicographically smallest such word in the dictionary and S2 is the lexicographically largest such word in the dictionary.

2/2 First paragraph original text According to a research at an English university, it doesn’t matter in what order the letters in a word are, the only important thing is that the first and last letter is at the right place. The rest can be a total mess and you can still read it without any problem. This is because we do not read every letter by itself but the word as a whole. Sample Input 5 aabab aaaab aaabb aaaaa xyxyx 5aaaaa aabab zzzzz kkkkk aaabb Sample Output 1 aaaaa aaaaa 2 aaabb aabab 0 0 2 aaabb aabab