SP406 LOGIC - Logic

Description

Consider a 10x10 grid. Cells in this grid can contain one of five logic operations (AND, OR, NOT, Input, Output). These can be joined together to form a logic circuit. Given a description of a circuit and a set of boolean values, build the logic circuit and execute the input stream against it.

Input Format

The first line of the input contains a single integer _n_, which specifies the number of circuits to be processed. There will then be _n_ groups of circuit descriptions and test values. A circuit is made up of a number of operations. Each line describing an operation begins with three characters: the co-ordinates for a cell, 0-9 on the _X_-axis then 0-9 on the _Y_-axis, followed by a single character to represent the operation of that cell (`&' for AND, `|' for OR, `!' for NOT, `i' for Input and `o' for Output). Optionally following each triple is a set of co-ordinate pairs which represent the _x_ and _y_ co-ordinates of cells that take the output of this cells operation as an input for theirs. This (possibly empty) output list is terminated by `..'. The list of operations is terminated by a line containing the word `end'. Next, for each circuit, comes the set of test values. The first line contains an integer _t_ which gives the number of test cases your program must run. Next, there are _t_ lines, each line containing a sequence of `0' and `1' characters symbolising the input values for one test case. The number of inputs will always correspond to the number of inputs defined by the circuit description. The input values are to be applied to the inputs in the order in which the input operations were defined in the circuit description. The next circuit description, if any, will then follow.

Output Format

For each circuit, your program should output one line for each test case given in the input. The line should contain one `0' or `1' character for each output defined by the circuit description in the order in which the outputs were defined. Your program should output a blank line after each set of test cases.