This page is still under construction.

Parts of this page are still being built. What you see may change.

The SetStack Computer

Time limit1sMemory limit128 MB

Summary
Simulate a stack machine whose elements are hereditarily finite sets, printing top-of-stack cardinality after each of five set operations.
Level

Medium6 of 10

Topics
Hash map, Stack, Simulation, Recursion
Solved
No attempts yet

Problem

A group of theorists is building a supercomputer that operates on sets instead of numbers. Your job is to simulate its prototype, the SetStack Alpha.

The machine keeps a single stack of sets, which starts out empty. After every operation it prints the cardinality of the set currently on top of the stack. The cardinality of a set SS, written ∣S∣|S|, is the number of elements it contains.

There are five commands:

  • PUSH — push the empty set {} onto the stack.
  • DUP — duplicate the top set (pop it, then push it back twice).
  • UNION — pop the top two sets and push their union.
  • INTERSECT — pop the top two sets and push their intersection.
  • ADD — pop the top two sets, insert the first (upper) set as a single element of the second (lower) set, and push the result.

Sets may contain other sets as elements, and two sets with exactly the same elements are considered equal.

As an illustration, suppose the top of the stack is A = { {}, {{}} } and the set just below it is B = { {}, {{{}}} }. Then ∣A∣=2|A| = 2 and ∣B∣=2|B| = 2, and:

  • UNION produces { {}, {{}}, {{{}}} }, so the output is 3.
  • INTERSECT produces { {} }, so the output is 1.
  • ADD produces { {}, {{{}}}, {{}, {{}}} }, so the output is 3.

Input

The first line contains an integer TT with 0≤T≤50 \le T \le 5: the number of test cases.

Each test case begins with a line containing the number of operations NN with 0≤N≤20000 \le N \le 2000. The next NN lines each contain one of the five commands.

It is guaranteed that the machine can execute the whole command sequence without ever popping from an empty stack.

Output

For each operation, print a single line containing one integer: the cardinality of the set on top of the stack after that command has executed.

After each test case, print a line containing *** (three asterisks).

Examples1

  1. Example 1

    Input
    2
    9
    PUSH
    DUP
    ADD
    PUSH
    ADD
    DUP
    ADD
    DUP
    UNION
    5
    PUSH
    PUSH
    ADD
    PUSH
    INTERSECT
    
    Expected output
    0
    0
    1
    0
    1
    1
    2
    2
    2
    ***
    0
    0
    1
    0
    0
    ***