#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int M = 30005;
int Set[M];
int sum[M]; //记录各点树对应的节点数。
int up[M]; //记录当前节点上面有多少个盒子。
int n, m;
void init() {
for(int i = 1; i < M; i++) {
Set[i] = i;
sum[i] = 1;
up[i] = 0;
}
}
int Find(int x) {
if(x != Set[x]) {
int t = Set[x];
Set[x] = Find(Set[x]);
up[x] += up[t];
return Set[x];
}
return x;
}
void Union(int a, int b) {
a = Find(a);
b = Find(b);
if(a != b) {
Set[b] = a;
up[b] += sum[a];
sum[a] += sum[b];
}
}
int main()
{
int a, b;
char str[3];
while(scanf("%d", &n) != EOF) {
init();
for(int i = 0; i < n; i++) {
scanf("%s", str);
if(str[0] == 'M') {
scanf("%d%d", &a, &b);
Union(a, b);
} else {
scanf("%d", &a);
b = Find(a);
printf("%d\n", sum[b]- up[a]-1);
}
}
}
return 0;
}