next up previous
Next: Problem B: Ladder Up: html Previous: html

Problem A: Decoding

A substitution cipher is a method used for encoding a plain text message. For every character c in the alphabet, a string f(c) is defined. A plain text message $c_1c_2c_3...c_n$ is encoded by concatenating the strings $f(c_1)f(c_2)f(c_3)...f(c_n)$ in the same order. You have to write a program, which given the substitution rules and a string, determines whether the string can be obtained by encoding some plain text message using the substitution rules.

Input

The input will consist of several test cases. The first line of each test case will specify $n$, the size of the alphabet for text messages. If $n$ is 0, it indicates end of input. Assume that $n$ is at most 10. The next $n$ lines will specify the values of $f(c)$, one per line, for the $n$ characters in the text alphabet. Assume that $f(c)$ may contain at most 10 characters, each of which is from 'a' to 'z'. The next line will contain a single integer $m$, which is the number of strings to be tested with the given substitution rule. The next $m$ lines will contain one string each with at most 10000 characters, each of which is from 'a' to 'z'.

Output

The output for each string tested is 1 if the string is an encoding of some string and 0 otherwise. The output should be written one per line, in the same order as the strings tested.

Sample Input

2
aba
ab
2
ababa
abaab
3
xyz
z
yyx
2
zyzy
yyxxyzz
0

Sample Output

1
1
0
1


next up previous
Next: Problem B: Ladder Up: html Previous: html
G. Sivakumar 2003-12-08