Intuitionistic Logic

No attempts yetTime limit2sMemory limit128 MB

Problem

Vasya recently ran into a movement in mathematics and logic called "intuitionism". Its central idea is the rejection of the law of the excluded middle, the logical law saying that any assertion is either true or false. Vasya liked the idea. He says: "Classical mathematics tells me that Fermat's Last Theorem is either true or false, but that statement is useless to me until I see a proof or a counterexample." So Vasya became an intuitionist. He tries to use intuitionistic logic in everything he does, above all in his scientific work. This logic is much harder than the classical one, and Vasya often writes formulas that are valid in classical logic but not in the intuitionistic one.

He now wants a program that checks his formulas automatically. He found a book that explains how to do it, but he is not good at programming, so you have to help him.

The construction starts from an arbitrary acyclic oriented graph X=(X,G)X = (\mathcal{X}, G), where X\mathcal{X} is the set of vertices. First a partial order on X\mathcal{X} is defined: xyx \le y holds if and only if XX contains a path (possibly of zero length) from xx to yy. Next, let B\mathcal{B} be the set of all subsets of X\mathcal{X}, and let HB\mathcal{H} \subset \mathcal{B} consist of every αX\alpha \subseteq \mathcal{X} in which any two different elements xx and yy are incomparable, meaning that neither xyx \le y nor yxy \le x holds. Note that H\mathcal{H} always contains the empty set and every one-element subset of X\mathcal{X}.

Now it is possible to define a map Max:BHBMax : \mathcal{B} \to \mathcal{H} \subset \mathcal{B}. For MXM \subseteq \mathcal{X} we put Max(M)={xM:¬yM, xy, xy}Max(M) = \{x \in M : \neg \exists y \in M,\ x \ne y,\ x \le y\}, the set of all maximal elements of MM.

Several operations on H\mathcal{H} follow. For α,βH\alpha, \beta \in \mathcal{H} we put

αβ=Max(αβ)\alpha \wedge \beta = Max(\alpha \cup \beta)

αβ=Max({xX:yα, zβ, xy, xz})\alpha \vee \beta = Max(\{x \in \mathcal{X} : \exists y \in \alpha,\ \exists z \in \beta,\ x \le y,\ x \le z\})

αβ={xβ:¬yα, xy}\alpha \Rightarrow \beta = \{x \in \beta : \neg \exists y \in \alpha,\ x \le y\}

0=Max(X),1=0 = Max(\mathcal{X}), \qquad 1 = \varnothing

¬α=(α0),αβ=((αβ)(βα))\neg \alpha = (\alpha \Rightarrow 0), \qquad \alpha \equiv \beta = ((\alpha \Rightarrow \beta) \wedge (\beta \Rightarrow \alpha))

A logical formula consists of the following symbols.

  • Constants 11 and 00.
  • Variables, the capital letters from AA to ZZ.
  • Parentheses. If EE is a formula, then (E)(E) is another one.
  • Negation. If EE is a formula, then ¬E\neg E is a formula.
  • Conjunction, E1E2EnE_1 \wedge E_2 \wedge \dots \wedge E_n. The conjunction is evaluated from left to right, so E1E2E3=(E1E2)E3E_1 \wedge E_2 \wedge E_3 = (E_1 \wedge E_2) \wedge E_3.
  • Disjunction, E1E2EnE_1 \vee E_2 \vee \dots \vee E_n. The same remark applies.
  • Implication, E1E2E_1 \Rightarrow E_2. Unlike the previous two operations it is evaluated from right to left, so E1E2E3E_1 \Rightarrow E_2 \Rightarrow E_3 means E1(E2E3)E_1 \Rightarrow (E_2 \Rightarrow E_3).
  • Equivalence, E1E2EnE_1 \equiv E_2 \equiv \dots \equiv E_n. This expression equals (E1E2)(E2E3)(En1En)(E_1 \equiv E_2) \wedge (E_2 \equiv E_3) \wedge \dots \wedge (E_{n-1} \equiv E_n).

The operations are listed from the highest priority to the lowest.

A formula EE is called valid in the model defined by XX if it evaluates to 11 after every substitution of elements of H\mathcal{H} for the variables involved in EE. Otherwise it is called invalid.

You are given the graph XX and a set of formulas. Determine which of them are valid.

Input

The input contains one or more test cases and ends at end of file.

The first line of each test case contains two integers NN and MM separated by a single space, the number of vertices (1N1001 \le N \le 100) and the number of edges (0M50000 \le M \le 5\,000) of XX. Each of the next MM lines contains two integers sis_i and tit_i, the beginning and the end of the ii-th edge. The next line contains KK (1K201 \le K \le 20), the number of formulas to be processed, and each of the following KK lines contains one formula.

A formula is a string of the tokens 0, 1, A ... Z, (, ), ~, &, |, =>, =. The last five tokens stand for ¬\neg, \wedge, \vee, \Rightarrow and \equiv respectively. Tokens can be separated by an arbitrary number of spaces. No line is longer than 254254 characters, and every formula in the input is syntactically correct. You may also assume that the number of elements H=HH = |\mathcal{H}| is at most 100100 and that 1jKHv[j]106\sum_{1 \le j \le K} H^{v[j]} \le 10^6, where v[j]v[j] is the number of different variables used in the jj-th formula.

Output

For each test case print KK lines, one line for each formula. Write valid on the jj-th line if the jj-th formula is valid, and invalid otherwise.