1 #include<cstdio>
2 const int maxn=1e6+10;
3 int n,m;
4 int f[maxn],s[maxn]={-1},d[maxn],l[maxn],r[maxn];
5 inline void swap_(int&x,int&y){x^=y,y^=x,x^=y;}
6 int ff(int x){return f[x]==x?x:f[x]=ff(f[x]);}
7 int merger(int a,int b){
8 if(!a) return b;
9 if(!b) return a;
10 if(s[a]>s[b]) swap_(a,b);
11 r[a]=merger(r[a],b);
12 if(d[r[a]]>d[l[a]]) swap_(l[a],r[a]);
13 d[a]=d[r[a]]+1;
14 return a;
15 }
16 int a,b;
17 char ch[3];
18 int main(){
19 scanf("%d",&n);
20 for(int i=1;i<=n;i++) scanf("%d",&s[i]),f[i]=i;
21 scanf("%d",&m);
22 while(m--){
23 scanf("%s",ch);
24 if(ch[0]==‘M‘){
25 scanf("%d%d",&a,&b);
26 if(s[a]==-1||s[b]==-1) continue;
27 a=ff(a),b=ff(b);
28 if(a!=b)
29 f[a]=f[b]=merger(a,b);
30 }
31 if(ch[0]==‘K‘){
32 scanf("%d",&a);
33 if(s[a]!=-1){
34 a=ff(a);
35 printf("%d\n",s[a]);s[a]=-1;
36 f[a]=merger(l[a],r[a]);
37 f[f[a]]=f[a];
38 }
39 else puts("0");
40 }
41 }
42 return 0;
43 }