# 可并堆

#include<cstdio>
#include<algorithm>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
const int maxn=1000000+10;
int fa[maxn],dis[maxn],left[maxn],right[maxn],a[maxn],root[maxn];
bool bz[maxn];
int i,j,k,l,t,n,m;
char ch;
char get(){
char ch=getchar();
while (ch!='M'&&ch!='K') ch=getchar();
return ch;
}
int x=0;
char ch=getchar();
while (ch<'0'||ch>'9') ch=getchar();
while (ch>='0'&&ch<='9'){
x=x*10+ch-'0';
ch=getchar();
}
return x;
}
int getfa(int x){
return fa[x]?fa[x]=getfa(fa[x]):x;
}
int merge(int x,int y){
if (!x||!y) return x+y;
if (a[x]>a[y]) swap(x,y);
right[x]=merge(right[x],y);
if (dis[left[x]]<dis[right[x]]) swap(left[x],right[x]);
dis[x]=dis[right[x]]+1;
return x;
}
int deletemin(int x){
return merge(left[x],right[x]);
}
int main(){
fo(i,1,m){
ch=get();
if (ch=='M'){
if (bz[j]||bz[k]) continue;
j=getfa(j);
k=getfa(k);
if (j==k) continue;
fa[j]=k;
root[k]=merge(root[j],root[k]);
}
else{
if (bz[j]) printf("0\n");
else{
j=getfa(j);
printf("%d\n",a[root[j]]);
bz[root[j]]=1;
root[j]=deletemin(root[j]);
}
}
}
}

06-02 196

06-23 229

03-21 1107

01-06 269

04-18 91

09-26 116

01-15 63

01-03 232

01-12 115

05-31 3356