Description Description
In 5801 A.D., the inhabitants of the Earth migrated to the Taurus Alpha Second Planet, where they published the Galactic Federation Declaration, which reign title the first year of the cosmic calendar and began to expand deep into the Milky Way.
In the 799-year universe, the two largest military groups in the galaxy erupted in the Battle of the Basque star. Taishanyading Group sent the Universe fleet Commander Reinhart led more than 100,000 warships to battle, Majestic Group point name Yang Weili organized 30,000 warships to meet the enemy.
Yang Weili good at formations, skillfully use various tactics repeatedly beat, inevitably pleases birth overbearing. In this battle, he divides the Battle of the BA-Mi-star field into30000column, each column is numbered1, 2,... .., 30000. After that, he numbered his battleship in sequence1, 2,... .., 30000, let the firstIbattleship in the firstIcolumn(i = 1, 2,... .., 30000), forming"Higo", Lure. This is the initial pattern. When the enemy arrives, Yang Weili will release the merger instructions several times, concentrating most of the warships on a few columns and carrying out intensive attacks. The merge directive isM I J, meaning to let the firstIthe entire battleship queue, as a whole (head at the front end in the rear) to the firstJThe tail of the battleship queue where the battleship is located. Obviously the battleship queue is made up of one or more warships in the same column. The execution result of the merge directive increases the queue.
However, the wily Reinhart has already made a strategic initiative. During the war, he was able to monitor Yang Weili's fleet transfer instructions at any time through a vast intelligence network.
While Yang Weili issued instructions to mobilize the fleet, Reinhart also issued a number of inquiry instructions to keep abreast of the current distribution of Yang Weili warships:C i J. The directive means asking the computer if the Yang Weili battleship i and USS J are currently in the same column, and if they are in the same column, how many warships are arranged between them.
As a senior senior programmer, you are asked to write a program to analyze Yang Weili's instructions, and to answer the questions of Eli Hart.
The final battle has begun, and the history of the Milky Way has turned over a page ...
Input format
Line 1 has 1 integer T(1<=t<=500,000), indicating a total of T instructions.
There are T -lines below, with one instruction per line. Directives are available in two formats:
1. m i j : i and j is a two integer ( 1<=i , j<=30000 i j
2. C i J :i and J are two integers (1<=i, j<=30000), indicating the battleship number involved in the instruction. This directive is an inquiry instruction issued by Reinhart.
Output format
Your program should analyze and process each of the input commands in turn:
If it is a fleet transfer order issued by Yang Weili, it means that the fleet alignment has changed, and your program should be aware of this, but do not export any information;
If it is a Reinhart issued query instruction, your program will output a line containing only an integer representing the number of warships arranged between Battleship I and Battleship J on the same column. If battleship No. I is not currently on the same column as Battleship J, Output -1.
Sample input to sample
4
M 2 3
C 1 2
M 2 4
C 4 2
Sample output sample outputs
-1
1
Legend of the Galactic Heroes (NOI2002)