Artinals

No attempts yetTime limit1sMemory limit512 MB

Problem

Nick has been learning about a special kind of set called artinian sets, or artinals for short. Their advantage is that each one has a finite representation, so a computer can process it. The formal definition is a little involved:

  • The only artinal of height 0\le 0 is the empty set ∅.
  • For a positive integer NN, the artinals of height N\le N are exactly the finite sets whose members are all artinals of height N1\le N-1.
  • AA is an artinal if it is an artinal of height N\le N for some positive integer NN.
  • The collection of all artinals is written UU.

Every artinal of height N\le N is also an artinal of height N+1\le N+1, so for any artinal AA we define its height h(A)h(A) as the smallest NN for which AA is an artinal of height N\le N. An artinal of height NN is called an NN-artinal.

Two further notions are needed: the canonical order on UU (written <) and the canonical form of an artinal.

  • The canonical form of an artinal AA of height N\le N is the listing A={A1,A2,,As}A = \{A_1, A_2, \ldots, A_s\} in which every AiA_i is an artinal of height N1\le N-1 and A1<A2<<AsA_1 < A_2 < \cdots < A_s.
  • If A={A1,,As}A = \{A_1, \ldots, A_s\} and B={B1,,Bt}B = \{B_1, \ldots, B_t\} are two artinals of height N\le N in canonical form, then A<BA < B holds iff there is an integer kk with 1kmin(s+1,t)1 \le k \le \min(s+1, t) such that Aj=BjA_j = B_j for every 1j<k1 \le j < k and either k=s+1k = s+1 or Ak<BkA_k < B_k. Intuitively, compare the sorted member lists left to right; the first differing member decides, and a proper prefix ranks first.

The canonical representation of an artinal is a string over the characters {, } and ,: repr(∅) = {}, and if A={A1,,As}A = \{A_1, \ldots, A_s\} in canonical form, then repr(AA) = { + repr(A1A_1) + , + \cdots + , + repr(AsA_s) + }.

Canonical representations become long, so a shorthand is introduced. For each integer N0N \ge 0 the finite ordinal N\mathbf{N} is defined by induction: 0:=\mathbf{0} := ∅ and N+1:={N}N\mathbf{N{+}1} := \{\mathbf{N}\} \cup \mathbf{N}, so that N={0,1,,N1}\mathbf{N} = \{\mathbf{0}, \mathbf{1}, \ldots, \mathbf{N{-}1}\}. The reduced canonical representation is obtained from the canonical representation by replacing, with the decimal numeral NN, every occurrence of the ordinal N\mathbf{N} that is not contained inside an occurrence of some larger ordinal M\mathbf{M} (with M>NM > N).

The following operations on artinals are defined, from highest priority to lowest:

  • Unary intersection ∩: for a non-empty artinal A={A1,,As}A = \{A_1, \ldots, A_s\}, ∩A:=A1A2AsA := A_1 \cap A_2 \cap \cdots \cap A_s.
  • Unary union ∪: for any artinal A={A1,,As}A = \{A_1, \ldots, A_s\}, ∪A:=A1A2AsA := A_1 \cup A_2 \cup \cdots \cup A_s; and ∪∅ := ∅.
  • Binary intersection ∩: AB:={x:xA and xB}A \cap B := \{x : x \in A \text{ and } x \in B\}.
  • Binary union ∪: AB:={x:xA or xB}A \cup B := \{x : x \in A \text{ or } x \in B\}.
  • Binary difference −: AB:={xA:xB}A - B := \{x \in A : x \notin B\}.
  • Binary symmetric difference △: AB:=(AB)(BA)A \mathbin{\triangle} B := (A - B) \cup (B - A).

The following relations between artinals are defined:

  • Equality = and inequality ≠.
  • Inclusion ⊂ and ⊃: ABBA(xAxB)A ⊂ B \Leftrightarrow B ⊃ A \Leftrightarrow (x \in A \Rightarrow x \in B); that is, ⊂ means "subset or equal".
  • Membership ∈ and ∋: BAB \in A (equivalently ABA \ni B) means BB is a member of AA.
  • The canonical-order relations <, ≤, ≥, > described above, where AB(A<B or A=B)A \le B \Leftrightarrow (A < B \text{ or } A = B), A>BB<AA > B \Leftrightarrow B < A, and ABBAA \ge B \Leftrightarrow B \le A.

You must run a small program that computes with artinals. The program is a sequence of operators, one per line. There are five kinds:

  • Assignment ⟨ident⟩:=⟨expr⟩ — sets the variable ⟨ident⟩ to the value of ⟨expr⟩.
  • Evaluate !⟨expr⟩ — evaluates ⟨expr⟩ and prints its reduced canonical representation on its own line.
  • Check ?⟨expr⟩⟨relation⟩⟨expr⟩ — evaluates the condition and prints TRUE or FALSE on its own line.
  • Comment #⟨any characters⟩ — the whole line is copied to the output.
  • Empty — a line consisting only of blank characters; it does nothing.

The grammar uses these definitions:

  • ⟨ident⟩ ::= ⟨alpha⟩{⟨alpha⟩}
  • ⟨alpha⟩ ::= ⟨letter⟩ | ⟨digit⟩ | _
  • ⟨digit⟩ ::= 0 | 1 | … | 9
  • ⟨letter⟩ ::= A | … | Z | a | … | z
  • ⟨expr⟩ ::= { [ ⟨expr⟩ { ,⟨expr⟩ } ] } | ⟨ident⟩ | ⟨expr⟩⟨binop⟩⟨expr⟩ | ⟨unop⟩⟨expr⟩ | (⟨expr⟩)
  • ⟨binop⟩ ::= + | * | - | ^
  • ⟨unop⟩ ::= + | *
  • ⟨relation⟩ ::= < | > | = | <= | >= | <> | -> | <- | << | >>

The binary operators +, *, -, ^ denote ∪, ∩, −, △ respectively; the unary operators +, * denote ∪ and ∩. The relations <, >, =, <=, >=, <>, ->, <-, <<, >> denote <, >, =, ≤, ≥, ≠, ∈, ∋, ⊂, ⊃ respectively. Parentheses ( and ) change precedence as usual. Any two tokens — except the ⟨alpha⟩ characters that make up a single ⟨ident⟩ — may be separated by any number of blank characters (spaces and tabs).

Before the program runs, every variable whose name is the decimal representation (without leading zeros) of a non-negative integer NN is preset to the finite ordinal N\mathbf{N}. Every other variable starts as ∅. Identifiers are case-sensitive.

Input

The input consists of at most one hundred lines, each holding a single operator. No line is longer than 254 characters.

Output

Produce one line of output for every ?, ! and # operator, as described above. The input is guaranteed to cause no run-time errors (for example, unary ∩ is never applied to the empty set).