#include<bits/stdc++.h>
#define N 30010
using namespace std;
int f[N],fa[N],sum[N];
int find(int k){
if(fa[k]==k)return k;
int p=fa[k];
fa[k]=find(fa[k]);
f[k]+=f[p];
}
void un(int x,int y){ //x->y
x=find(x);y=find(y);
//fa[x]=y;
f[x]+=sum[y];
sum[y]+=sum[x];
}
void pre( ){
std::ios::sync_with_stdio(0);
memset(sum,1,sizeof(sum));
int i;
for(i=1;i<=30000;i++)fa[i]=i;
}
void work( ){
char s;
int x,y;
cin>>s>>x>>y;
if(s=='M'){
un(x,y);
}else{
if(find(x)==find(y))
cout<<abs(f[x]-f[y])<<endl;
else cout<<"-1"<<endl;
}
}
int main( ){
pre( );
int t;
cin>>t;
while(t--)work( );
}
为什么注释掉的一行会导致RE?