#P1033F. Boolean Computer

    ID: 3149 Type: RemoteJudge 7000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>bitmasksbrute forcefftmath*2800

Boolean Computer

No submission language available for this problem.

Description

Alice has a computer that operates on ww-bit integers. The computer has nn registers for values. The current content of the registers is given as an array a1,a2,,ana_1, a_2, \ldots, a_n.

The computer uses so-called "number gates" to manipulate this data. Each "number gate" takes two registers as inputs and calculates a function of the two values stored in those registers. Note that you can use the same register as both inputs.

Each "number gate" is assembled from bit gates. There are six types of bit gates: AND, OR, XOR, NOT AND, NOT OR, and NOT XOR, denoted "A", "O", "X", "a", "o", "x", respectively. Each bit gate takes two bits as input. Its output given the input bits b1b_1, b2b_2 is given below:

b1b2AOXaox00000111010111001001110011110001\begin{matrix} b_1 & b_2 & A & O & X & a & o & x \\ 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 \\ 0 & 1 & 0 & 1 & 1 & 1 & 0 & 0 \\ 1 & 0 & 0 & 1 & 1 & 1 & 0 & 0 \\ 1 & 1 & 1 & 1 & 0 & 0 & 0 & 1 \\ \end{matrix}

To build a "number gate", one takes ww bit gates and assembles them into an array. A "number gate" takes two ww-bit integers x1x_1 and x2x_2 as input. The "number gate" splits the integers into ww bits and feeds the ii-th bit of each input to the ii-th bit gate. After that, it assembles the resulting bits again to form an output word.

For instance, for 44-bit computer we might have a "number gate" "AXoA" (AND, XOR, NOT OR, AND). For two inputs, 13=1101213 = 1101_2 and 10=1010210 = 1010_2, this returns 12=1100212 = 1100_2, as 11 and 11 is 11, 11 xor 00 is 11, not (00 or 11) is 00, and finally 11 and 00 is 00.

You are given a description of mm "number gates". For each gate, your goal is to report the number of register pairs for which the "number gate" outputs the number 00. In other words, find the number of ordered pairs (i,j)(i,j) where 1i,jn1 \leq i,j \leq n, such that wk(ai,aj)=0w_k(a_i, a_j) = 0, where wkw_k is the function computed by the kk-th "number gate".

The first line contains three integers: ww, nn, and m (1w12,1n3104,1m5104)m~(1 \leq w \leq 12, 1 \leq n \leq 3\cdot 10^4, 1 \leq m \leq 5\cdot 10^4) — the word size, the number of variables, and the number of gates.

The second line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (0ai<2w)(0 \leq a_i < 2^w) — the value of variables stored in the registers.

Each of the next mm lines contains a string gj (gj=w)g_j~(|g_j| = w) with a description of a single gate. Each character of gjg_j is one of "A", "O", "X", "a", "o", "x".

Print mm lines. The ii-th line should contain the number of ordered pairs of variables for which the ii-th gate returns zero.

Input

The first line contains three integers: ww, nn, and m (1w12,1n3104,1m5104)m~(1 \leq w \leq 12, 1 \leq n \leq 3\cdot 10^4, 1 \leq m \leq 5\cdot 10^4) — the word size, the number of variables, and the number of gates.

The second line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (0ai<2w)(0 \leq a_i < 2^w) — the value of variables stored in the registers.

Each of the next mm lines contains a string gj (gj=w)g_j~(|g_j| = w) with a description of a single gate. Each character of gjg_j is one of "A", "O", "X", "a", "o", "x".

Output

Print mm lines. The ii-th line should contain the number of ordered pairs of variables for which the ii-th gate returns zero.

Samples

Sample Input 1

4 3 1
13 10 6
AXoA

Sample Output 1

3

Sample Input 2

1 7 6
0 1 1 0 1 0 0
A
O
X
a
o
x

Sample Output 2

40
16
25
9
33
24

Sample Input 3

6 2 4
47 12
AOXaox
AAaaAA
xxxxxx
XXXXXX

Sample Output 3

2
3
0
2

Sample Input 4

2 2 2
2 0
xO
Ox

Sample Output 4

2
0

Note

In the first test case, the inputs in binary are 11011101, 10101010, 01100110. The pairs that return 00 are (13,6)(13, 6), (6,13)(6, 13), and (6,6)(6, 6). As it was already mentioned in the problem statement, 1310=1013=1213 \oplus 10 = 10 \oplus 13 = 12. The other pairs are 1313=1113 \oplus 13 = 11, 1010=810 \oplus 10 = 8 and 106=610=410 \oplus 6 = 6 \oplus 10 = 4.