Stack Copying Game

No attempts yetTime limit1sMemory limit64 MB

Problem

Mirko is playing with stacks. When the game starts he has a single empty stack, numbered 00. In step ii of the game he picks one of the stacks that already exist, calls its number vv, copies that stack, and then does one of these three things.

  1. Push the number ii onto the top of the copy.
  2. Pop the number on top of the copy.
  3. Pick one more stack, call its number ww, and count how many distinct numbers sit in both the copy and stack ww.

The stack he just created is numbered ii.

Mirko does not want to handle the stacks himself, so write a program that does it for him. For every operation of type b, print the number popped off the stack. For every operation of type c, print how many numbers satisfy the condition.

Input

The first line contains NN, the number of steps in Mirko's game. (1N3000001 \le N \le 300\,000)

The steps of the game are numbered 11 through NN in chronological order.

The iith of the next NN lines describes step ii in one of these three forms.

  • "a v" for an operation of type a.
  • "b v" for an operation of type b.
  • "c v w" for an operation of type c.

The first character of the line gives the type of the operation. The one or two numbers after it are the stack labels the operation uses, and they are always integers in [0,i1][0, i-1].

For an operation of type b, the stack the element is popped from is not empty.

Output

For every operation of type b and every operation of type c, print the number you computed, one per line, in the order the operations appear in the input.

Note

Consider the first sample. At the start the only stack is S0={}S_0 = \{\}. Step 1 copies S0S_0 and pushes 11 on top, so S1={1}S_1 = \{1\}. Step 2 copies S1S_1 and pushes 22 on top, so S2={1,2}S_2 = \{1, 2\}. Step 3 copies S2S_2 and pops 22, so S3={1}S_3 = \{1\}. Step 4 copies S2S_2 and calls the copy S4S_4, then counts the numbers that appear in both S4S_4 and S3S_3. The only such number is 11, so the answer is 11. Step 5 copies S4S_4 and pops 22, so S5={1}S_5 = \{1\}.