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), where X is the set of vertices. First a partial order on X is defined: x≤y holds if and only if X contains a path (possibly of zero length) from x to y. Next, let B be the set of all subsets of X, and let H⊂B consist of every α⊆X in which any two different elements x and y are incomparable, meaning that neither x≤y nor y≤x holds. Note that H always contains the empty set and every one-element subset of X.
Now it is possible to define a map Max:B→H⊂B. For M⊆X we put Max(M)={x∈M:¬∃y∈M, x=y, x≤y}, the set of all maximal elements of M.
Several operations on H follow. For α,β∈H we put
α∧β=Max(α∪β)
α∨β=Max({x∈X:∃y∈α, ∃z∈β, x≤y, x≤z})
α⇒β={x∈β:¬∃y∈α, x≤y}
0=Max(X),1=∅
¬α=(α⇒0),α≡β=((α⇒β)∧(β⇒α))
A logical formula consists of the following symbols.
The operations are listed from the highest priority to the lowest.
A formula E is called valid in the model defined by X if it evaluates to 1 after every substitution of elements of H for the variables involved in E. Otherwise it is called invalid.
You are given the graph X and a set of formulas. Determine which of them are valid.
The input contains one or more test cases and ends at end of file.
The first line of each test case contains two integers N and M separated by a single space, the number of vertices (1≤N≤100) and the number of edges (0≤M≤5000) of X. Each of the next M lines contains two integers si and ti, the beginning and the end of the i-th edge. The next line contains K (1≤K≤20), the number of formulas to be processed, and each of the following K lines contains one formula.
A formula is a string of the tokens 0, 1, A ... Z, (, ), ~, &, |, =>, =. The last five tokens stand for ¬, ∧, ∨, ⇒ and ≡ respectively. Tokens can be separated by an arbitrary number of spaces. No line is longer than 254 characters, and every formula in the input is syntactically correct. You may also assume that the number of elements H=∣H∣ is at most 100 and that ∑1≤j≤KHv[j]≤106, where v[j] is the number of different variables used in the j-th formula.
For each test case print K lines, one line for each formula. Write valid on the j-th line if the j-th formula is valid, and invalid otherwise.