#include<iostream>
#define MaxN 30000
using namespace std;
typedef struct node
{
int count;
int parent;
int d;
}NODE;
NODE S[MaxN+1];
void init()
{
for(int i=1;i<=MaxN;i++)
{
S[i].count=1;
S[i].d=0;
S[i].parent=i;
}
}
int find_set(int x)
{
int t=S[x].parent;
if(S[x].parent!=x)
{
S[x].parent=find_set(S[x].parent);
S[x].d+=S[t].d;
}
return S[x].parent;
}
void union_set(int x,int y)
{
x=find_set(x);
y=find_set(y);
S[y].parent=x;
S[y].d=S[x].count;
S[x].count+=S[y].count;
}
int main()
{
int P,x,y;
char c;
init();
scanf("%d",&P);
getchar();
while(P>0)
{
P--;
c=getchar();
if(c=='M')
{
scanf("%d%d",&x,&y);
union_set(x,y);
}
else
{
scanf("%d",&x);
y=find_set(x);
printf("%d\n",S[y].count-S[x].d-1);
}
getchar();
}
return 0;
}