#include <cstdio> #include <iostream> #include <cctype> #include <cmath> #include <algorithm> using namespace std; const int N=30010; int p[N],t,s[N],d[N],num[N],x,y; char c; int find(int x){ if(x==p[x])return x; int fx=find(p[x]); d[x]+=d[p[x]]; return p[x]=fx; } int main(){ for(int i=1;i<=30000;i++){p[i]=i; d[i]=0; num[i]=1;} scanf("%d",&t); while(t--){ cin>>c>>x>>y; int fx=find(x),fy=find(y); if(c=='M'){ d[fx]+=num[fy]; p[fx]=fy; num[fy]+=num[fx]; num[fx]=0; } else{ if(fx!=fy){printf("-1 "); continue;} printf("%d ",abs(d[x]-d[y])-1); } } return 0; }