Follow My Logic

No attempts yetTime limit1sMemory limit128 MB

Problem

In this problem you must determine the output of a logic circuit made of one or more inputs, zero or more two-input AND/OR gates, and a single output. Each circuit is drawn with plain ASCII characters.

Wires are drawn with three characters: a horizontal segment is a dash - (ASCII 45), a vertical segment is a vertical bar | (ASCII 124), and a junction is a plus + (ASCII 43). Inputs are the capital letters A through Z, and the single output is a question mark ?.

AND and OR gates are drawn exactly as shown below, and they always appear in this orientation:

:\ 
: ) 
:/

AND gate

:\ 
: > 
:/

OR gate

The two gate inputs enter on the left and the gate output leaves on the right:

-:\
 : )-
-:/

Gate with inputs

Any gate input or output may be inverted. An inversion is marked by a lowercase letter o (ASCII 111) placed at that input or output location:

-o:\
  : )o-
--:/

An inverted top input and an inverted output

The figure below is a small but complete logic circuit with two inputs and one output:

A-o:\
   : )o-?
B--:/

Two logic inputs and the output

Every circuit obeys these rules:

  1. A circuit picture is at most 100 by 100 characters.
  2. A wire runs in a straight line except at a junction. At a junction the wire always makes a ninety-degree turn. No two junctions are horizontally or vertically adjacent.
  3. No wire is broken: every wire character is adjacent on both ends to another matching wire character, a junction, a gate input, a gate output, a logic input, or the logic output.
  4. Wires never cross or touch one another.
  5. Gate inputs always approach horizontally from the left, and gate outputs always leave horizontally to the right, as shown above.
  6. An inversion may appear only immediately next to a gate input or output, and it is always preceded (for an input) or followed (for an output) by at least one dash, as shown above.

A circuit diagram in the input ends with a line whose first column holds a single asterisk *. The lines that follow list the input states, one per line: each is a string of twenty-six 0 or 1 characters, where the first character is the state of input A, the second the state of input B, and so on. Input values for letters the circuit does not use are simply ignored. The list of input states ends with another line whose first column holds a single asterisk *.

Input

After the asterisk that ends a list of input states comes another circuit diagram, followed by its own list of input states, followed by another circuit diagram and its input states, and so on until the end of the file. The file always contains at least one circuit together with at least one set of inputs for that circuit.

Output

For each circuit, print the value of its output (0 or 1) on its own line, one line for each set of input values listed after that circuit. Separate the block of outputs for one circuit from the block for the next with a single blank line.