1 const maxn=100000+1000;
2 var l,r,s,rnd,v,fa:array[0..maxn] of longint;
3 i,n,m,x,y,xx,yy,rk:longint;
4 ch:char;
5 procedure swap(var x,y:longint);
6 var t:longint;
7 begin
8 t:=x;x:=y;y:=t;
9 end;
10 procedure pushup(k:longint);
11 begin
12 s[k]:=s[l[k]]+s[r[k]]+1;
13 end;
14 procedure rturn(var k:longint);
15 var t:longint;
16 begin
17 t:=l[k];l[k]:=r[t];r[t]:=k;s[t]:=s[k];pushup(k);k:=t;
18 end;
19 procedure lturn(var k:longint);
20 var t:longint;
21 begin
22 t:=r[k];r[k]:=l[t];l[t]:=k;s[t]:=s[k];pushup(k);k:=t;
23 end;
24 procedure ins(var k,x:longint);
25 begin
26 if k=0 then
27 begin
28 k:=x;s[k]:=1;l[k]:=0;r[k]:=0;
29 rnd[k]:=random(maxlongint);exit;
30 end;
31 inc(s[k]);
32 if v[x]<v[k] then begin ins(l[k],x);if rnd[l[k]]<rnd[k] then rturn(k);end
33 else begin ins(r[k],x);if rnd[r[k]]<rnd[k] then lturn(k);end;
34 end;
35 procedure print(x:longint);
36 begin
37 if x=0 then exit;
38 print(l[x]);
39 write(x,‘ ‘,v[x],‘!‘);
40 print(r[x]);
41 end;
42 function select(k,rk:longint):longint;
43 begin
44 if s[l[k]]+1=rk then exit(k)
45 else if s[l[k]]>=rk then exit(select(l[k],rk))
46 else exit(select(r[k],rk-1-s[l[k]]));
47 end;
48 function find(x:longint):longint;
49 begin
50 if fa[x]<>x then fa[x]:=find(fa[x]);
51 exit(fa[x]);
52 end;
53 procedure merge(x,y:longint);
54 begin
55 if x=0 then exit;
56 merge(l[x],y);merge(r[x],y);ins(y,x);
57 end;
58 procedure solvemerge;
59 begin
60 readln(x,y);
61 xx:=find(x);yy:=find(y);
62 if s[xx]>s[yy] then swap(xx,yy);
63 fa[xx]:=yy;
64 merge(xx,yy);
65 end;
66 procedure solveask;
67 begin
68 readln(x,rk);
69 xx:=find(x);
70 if rk>s[xx] then writeln(‘-1‘) else writeln(select(xx,rk));
71 end;
72 procedure init;
73 begin
74 readln(n,m);
75 for i:=1 to n do
76 begin
77 read(v[i]);
78 fa[i]:=i;s[i]:=1;l[i]:=0;r[i]:=0;
79 end;
80 for i:=1 to m do solvemerge;
81 end;
82 procedure main;
83 begin
84 readln(m);
85 for i:=1 to m do
86 begin
87 read(ch);
88 case ch of
89 ‘B‘:solvemerge;
90 ‘Q‘:solveask;
91 end;
92 end;
93 end;
94 begin
95 assign(input,‘input.txt‘);assign(output,‘output.txt‘);
96 reset(input);rewrite(output);
97 init;
98 main;
99 close(input);close(output);
100 end.