Ternary Logic
At the dawn of programming, it was often noted that the spouses of programmers, who themselves were not programmers (yes, such things do happen), would highlight their partners' detachment from the nuances of everyday life due to the rigid binary logic they worked with. Even the widely used branching operator, implemented in nearly all programming languages, faced criticism. Interestingly, there was once a project for an extended operator called IF THEN ELSE BUT !?, which required the use of the double symbol "!?" as its termination sign. This peculiar requirement is said to have spurred the development of various forms of non-binary logic and different versions of the switch operator. Although there is no official statistical data on how these innovations have impacted families where only one spouse is a programmer, it's clear that the role of programmers' spouses in the evolution of programming is significant.
Consider an expression composed of the numbers 0, 1, 2, variables that can only take one of these values (0, 1, 2), and operations including comparisons, logical conjunction, negation, and "strict" logical addition (similar to exclusive or), defined according to the following tables:
We will also define the operations ">", ">=", and "<>" as follows. For any two values x and y, which can be 0, 1, or 2:
x>y is equivalent to !(x<=y)
x>=y is equivalent to (x>y)|(x=y)
x<>y is equivalent to !(x=y)
The operations are prioritized in the following descending order:
!
< > <= >=
= <>
^
|
Parentheses can be used to alter the precedence in the expression. For each given expression and the variables it may include, determine the value of the expression.
Input
The first line contains a single number, indicating the number of test cases, which ranges from 1 to 1000. Each test case starts with a line containing the expression. Following this line is a sequence of one or more lines, each containing 3 numbers separated by spaces, representing the values of x, y, and z. The sequence ends with a line containing the single number 3.
The expression contains no spaces and may include up to three single-character variables: 'x', 'y', and 'z'. The length of each expression does not exceed 100 characters.
Output
The output should list the values of the expressions, each on a separate line, in the order they appear in the input file.