Poetry with an Asterisk
InterviewTime limit5sMemory limit128 MB
Count for each one-asterisk query how many dictionary words start with its prefix and end with its suffix without overlap.
Problem
In a certain exotic language there are distinct non-empty words made of lowercase English letters.
The language has a peculiar property found in no other language: in writing it uses a special sign, the asterisk (*), which can replace any (possibly empty) contiguous fragment of a single word. This makes written words ambiguous, which lets the language express unusually deep poems. It complicates everyday life, but in the end art matters more than biography.
You are given the list of all words in the language (in full, without asterisks) and the text of a poem in which every word contains exactly one asterisk. Compute how many words of the language match each word of the poem.
Formally, a poem word consists of a prefix , then the asterisk, then a suffix (either part may be empty). It matches a language word exactly when starts with , ends with , and (so the fragment hidden by the asterisk is a valid contiguous piece).
For example, if the language contains the words zupa, z, malpy, intruz, pyszny, then in the poem z*, m*y, g*ingo the first poem word is matched by two language words, the second by one, and the third by none (there must have been a misprint).
Input
The first line contains a natural number (), the number of test sets. The test sets follow.
The first line of each test set contains a natural number (), the number of words in the language.
Each of the next lines contains one word of the language. The words are pairwise distinct, and each consists of 1 to 10 lowercase English letters.
The next line contains a natural number (), the number of words in the poem.
Each of the next lines contains one word of the poem. These words need not be distinct; each consists of 1 to 10 characters, exactly one of which is an asterisk and the rest lowercase English letters.
Output
For each test set, print lines. The -th of those lines contains the number of language words that match the -th word of the poem.