1 #include<iostream>
2 #include<cstdio>
3 #include<algorithm>
4 #include<vector>
5 #include<cstdlib>
6 #include<cmath>
7 #include<cstring>
8 using namespace std;
9 #define maxn 100100
10 #define llg long long
11 #define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
12 llg n,m,Q;
13 char ch;
14
15 struct TREAP
16 {
17 llg size;
18 struct
19 {
20 llg l,r,rnd,val,size,fa;
21 }po[maxn];
22
23 void update(llg x){po[x].size=po[po[x].l].size+po[po[x].r].size;}
24
25 llg right_(llg x)
26 {
27 llg t=po[x].l;
28 po[x].l=po[t].r; po[po[t].r].fa=x;
29 po[t].r=x; po[x].fa=t;
30 po[t].size=po[x].size;
31 update(x);
32 return t;
33 }
34
35 llg left_(llg x)
36 {
37 llg t=po[x].r;
38 po[x].r=po[t].l; po[po[t].l].fa=x;
39 po[t].l=x; po[x].fa=t;
40 po[t].size=po[x].size;
41 update(x);
42 return t;
43 }
44
45 void init()
46 {
47 size=0; memset(po,0,sizeof(po));
48 for (llg i=1;i<=n;i++) scanf("%lld",&po[i].val),po[i].fa=i;
49 }
50
51 llg insert(llg x,llg wz,llg lx)
52 {
53 if (x==0)
54 {
55 po[wz].size=1; po[wz].fa=lx;
56 return wz;
57 }
58 po[x].size++;
59 if (po[x].val<po[wz].val)
60 {
61 po[x].r=insert(po[x].r,wz,x);
62 if (po[po[x].r].rnd<po[x].rnd) x=left_(x);
63 }
64 else
65 {
66 po[x].l=insert(po[x].l,wz,x);
67 if (po[po[x].l].rnd<po[x].rnd) x=right_(x);
68 }
69 po[x].fa=lx;
70 return x;
71 }
72
73 llg find_root(llg x)
74 {
75 while (po[x].fa!=x)
76 x=po[x].fa;
77 return x;
78 }
79
80 void dfs(llg x,llg y)
81 {
82 if (x==0) return ;
83 dfs(po[x].l,y);
84 dfs(po[x].r,y);
85 insert(y,x,y);
86 }
87
88 void union_(llg x,llg y)
89 {
90 llg f1=find_root(x),f2=find_root(y);
91 if (f1==f2) return ;
92 if (po[f1].size<po[f2].size) swap(f1,f2);
93 dfs(f2,f1);
94 }
95
96 llg rank(llg x,llg res)
97 {
98 if (res==po[po[x].l].size+1) return x;
99 if (res<=po[po[x].l].size) return rank(po[x].l,res);
100 else return rank(po[x].r,res-po[po[x].l].size-1);
101 }
102 }tree;
103
104 int main()
105 {
106 yyj("a");
107 cin>>n>>m;
108 tree.init();
109 for (llg i=1;i<=m;i++)
110 {
111 llg x,y;
112 scanf("%lld%lld",&x,&y);
113 tree.union_(x,y);
114 }
115 cin>>Q;
116 while (Q--)
117 {
118 llg x,y;
119 ch=getchar();
120 while (ch!=‘Q‘ && ch!=‘B‘) ch=getchar();
121 scanf("%lld%lld",&x,&y);
122 if (ch==‘Q‘)
123 {
124 llg ans=tree.rank(tree.find_root(x),y);
125 if (ans==0) ans=-1;
126 printf("%lld\n",ans);
127 }
128 else
129 {
130 tree.union_(x,y);
131 }
132 }
133 return 0;
134 }