acwing837

#include<iostream>
#define read(x) scanf("%d",&x)
using namespace std;
const int MAX=1e5+10;
int father[MAX],n,m,x,y,sz[MAX];
string op;
void init(){
    for(int i=1;i<=n;i++)father[i]=i,sz[i]=1;
}
int find(int x){
    if(father[x]!=x)father[x]=find(father[x]);
    return father[x];
}
void Unoin(int x,int y){
    int a=find(x),b=find(y);
    if(a==b)return;
    father[a]=y;
    sz[b]+=sz[a];
}
int main(){
    read(n),read(m);
    init();
    while(m--){
        cin>>op;
        if(op=="Q1") {
            read(x),read(y);
            if(find(x)==find(y))puts("Yes");
                else puts("No");
        }else if(op=="Q2"){
                read(x);
                cout<<sz[find(x)]<<endl;
        }
         else{
             read(x),read(y);
             Unoin(x,y);
         }
    }
    
    
    return 0;
}