A very big corporation is developing its corporative network. In the beginning, each of the enterprises of the corporation, numerated from to , organized its own computing and telecommunication center. Soon, for amelioration of the services, the corporation started to collect some enterprises in clusters, each of them served by a single computing and telecommunication center as follows. The corporation chose one of the existing centers (serving the cluster ) and one of the enterprises in some other cluster (not necessarily the center) and link them with a telecommunication line. The length of the line between the enterprises and is . In such a way the two old clusters are joined in a new cluster, served by the center of the old cluster . Unfortunately, after each join, the sum of the lengths of the lines linking an enterprise to its serving center could be changed and the end users would like to know what is the new length. Write a program to keep track of the changes in the organization of the network that is able in each moment to answer the questions of the users.
Input Specification
Your program has to be ready to solve more than one test case. The first line of the input will contain the number of the test cases. Each test will start with the number of enterprises (). Then some number of lines (no more than ) will follow with one of the commands:
- – asking the length of the path from the enterprise to its serving center in the moment;
- – informing that the serving center is linked to the enterprise .
The test case finishes with a line containing the letter O
. The commands are less than .
Output Specification
The output should contain as many lines as the number of commands in all test cases with a single number each – the asked sum of length of lines connecting the corresponding enterprise with its serving center.
Sample Input
1
4
E 3
I 3 1
E 3
I 1 2
E 3
I 2 4
E 3
O
Sample Output
0
2
3
5
Comments