#include<iostream>
#include<string.h>
#include<algorithm>
#include<cmath>
#define LL long long
#define itn int
using namespace std;
const int N=300010;
itn f[N],num[N],front[N];
int look (int pos) {
if(f[pos]==pos) return pos;
int fn=look(f[pos]);
front[pos]+=front[f[pos]];
return front[pos]=fn;
}
int main() {
int t;
cin>>t;
for(int i=1; i<N; i++) {
f[i]=i;
num[i]=1;
front[i]=0;
}
while(t--) {
char opt;
cin>>opt;
itn x,y;
if(opt=='M') {
cin>>x>>y;
int fx=look(x),fy=look(y);
f[fx]=fy;
front[fx]+=num[fy];
num[fy]+=num[fx];
num[fx]=0;
} else {
cin>>x;
int qwq=look(f[x]);
printf("%d\n",front[x]);
}
}
// system("pause");
return 0;
}