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
is encoded by concatenating
the strings
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
, the size of the alphabet for text messages. If
is 0,
it indicates end of input. Assume that
is at most 10. The next
lines will
specify the values of
, one per line, for the
characters in the text
alphabet. Assume that
may
contain at most 10 characters, each of which is from 'a' to 'z'. The next line
will contain a single integer
, which is the number of strings to be tested
with the given substitution rule. The next
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