//分两棵树,第二棵树dis[i]+dis[f[i]]曾经的父亲, 1(到曾经的父亲距离)+5 (曾经的父亲到当下的父亲的距离) #include<cstdio> #include<iostream> #include<algorithm> using namespace std; const int M=30000; int t; int f[M+10],dis[M+10]; int size[M+10];int find(int i) {if (f[i]==i) return i;dis[i]+=dis[f[i]];//dis[x] dis[y]return f[i]=find(f[i]); }int main() {scanf("%d",&t);for (int i=1;i<=M;i++){f[i]=i;size[i]=1;}char c; int x,y; int fx,fy;int ans=0;while(t--){scanf("\n");c=getchar();scanf("%d %d",&x,&y);fx=find(x);fy=find(y);if (c=='M'){f[fx]=fy;dis[fx]=size[fy];size[fy]+=size[fx];}else{if(fx!=fy) ans=-1;else{if(dis[x]<dis[y]){ans=dis[y]-dis[x]-1;}else ans=dis[x]-dis[y]-1;} // for(int i=1;i<=5;i++) // cout<<dis[i]<<",";printf("%d\n",ans);}}return 0; }