cho.sh
Notes
Loading...

Logical Expressions

Time limit

1s

Memory limit

128 MB

Problem

A logical expression is an expression over truth values. In this problem, a logical expression is made from the following parts.

  • Variables whose values are true or false

  • Unary logical operators with one operand

  • Binary logical operators with two operands

  • Parentheses that determine evaluation order

Every logical operator is defined by a truth table. A unary operator table contains the result for operand value false, then for operand value true. A binary operator table has two rows: the first row is for left operand false, and the second row is for left operand true. Inside each row, the two values are for right operand false, then right operand true.

The grammar of a logical expression is:

<expression> = <variable> | ( <expression> <operator> <expression> ) | ( <operator> <expression> )<variable> = <lowercase_letter><operator> = <uppercase_letter> | <operator> <uppercase_letter>

Here | is only a grammar symbol meaning a choice; it never appears in an actual expression. <lowercase_letter> is one English lowercase letter, and <uppercase_letter> is one English uppercase letter.

The value of an expression can sometimes be determined even when not every variable value is known. Substitute all possible truth values for the unknown variables. If every possible result is true, the expression value is true; if every possible result is false, it is false; otherwise it is unknown.

Input

The input consists of one or more test cases. The first line of each test case contains two nonnegative integers, each at most 100. The first integer is the number of unary operators, and the second is the number of binary operators.

Then the operator names and truth tables are given. Each operator name has length at most 20, and all operator names in one test case are distinct.

Each unary operator is defined by two lines. The first line contains the operator name. The second line contains two truth values: the results when the operand is false and true, in that order.

Each binary operator is defined by three lines. The first line contains the operator name. The next two lines contain two truth values each. The first row is for left operand false, the second row is for left operand true, and each row lists the results for right operand false and true.

After all operator definitions, one line contains a valid logical expression following the grammar above. Variables and operators are separated by one or more spaces. Between a parenthesized expression and an adjacent operator, spaces may be present or absent. No variable appears more than once in the expression. The expression length is between 1 and 500 inclusive.

After the expression, zero or more known variable values follow, one per line.

<variable> true<variable> false

No variable value is given more than once. Each test case ends with a line containing *. The input ends with one line containing two negative integers.

Output

For each test case, print one line in the format Case k: result. Here k is the 1-based test case number, and result is one of true, false, or unknown.

<expression> = <variable> | ( <expression> <operator> <expression> ) | ( <operator> <expression> )<variable> = <lowercase_letter><operator> = <uppercase_letter> | <operator> <uppercase_letter>
<variable> true<variable> false