California Jones and the Gate to Freedom

No attempts yetTime limit1sMemory limit128 MB

Problem

California Jones (the sister of the famous Indiana Jones) is trapped in front of a huge gate and needs your help.

There are $n$ stones lying in a row, each marked with a distinct integer. In front of the gate there are exactly $n/2$ holes, and Jones must place stones into them. Which hole a stone goes into does not matter; only which $n/2$ stones are chosen matters.

The gate identifies one choice with a binary number. A binary number names a choice as follows:

  • Look at the stones by their position in the input: the 1st, 2nd, ..., $n$-th stone.
  • Describe any choice of $n/2$ stones by the ascending list of the positions it uses.
  • Sort all possible choices in increasing (lexicographic) order of these position lists. The choice using positions ${1, 2, \dots, n/2}$ comes first.
  • Number the sorted choices $0, 1, 2, \dots, \binom{n}{n/2} - 1$.

A binary string $b$ encodes a non-negative integer, and that integer is the index of a choice in this ordering.

Given a binary string $b$ and a set of $n/2$ stones, decide whether that set is exactly the choice indexed by $b$. If $b$ is not a valid index (that is, $b \ge \binom{n}{n/2}$), it cannot match any set, so the answer is FALSE.

Input

The input contains several test cases. Each test case begins with the number of stones $n$. The input is terminated by a line with $n = 0$.

For every other test case, $n$ is even and $2 \le n \le 32$. The next $n$ integers are the stone identifiers. The test case then gives $k$, the number of queries. Each of the following $k$ queries consists of a binary string $b$ followed by $n/2$ distinct integers naming the chosen stones. Every named stone is one of the $n$ stones, and the length of $b$ is at most $30$.

Output

For each query, print a single line containing TRUE if the chosen stones are exactly the choice identified by $b$, and FALSE otherwise.