rt,
#include <bits/stdc++.h>
using namespace std;
inline int read(){
int x=0;bool f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=0;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
return f?x:-x;
}
int t,x,y,f[30005],fx,fy,num[30005],fr[30005];
char op;
int find(int x){
if(x==f[x]) return x;
int fax=find(f[x]);
fr[x]+=fr[f[x]];
return f[x]=fax;
}
int main(){
t=read();
for(int i=1;i<=30000;i++)
f[i]=i,num[i]=1;
while(t--){
op=getchar(),x=read(),y=read();
fx=find(x),fy=find(y);
if(op^'M') printf("%d\n",(fx^fy)?-1:abs(fr[x]-fr[y])-1);
else fr[fx]+=num[fy],f[fx]=fy,num[fy]+=num[fx],num[fx]=0;
}
return 0;
}
但是在我校 oj 上以最大点 822ms 的好成绩 T 了三个点?