1 #include<stdio.h>
2 #define maxn 1000000
3 using namespace std;
4
5 int TIM,chart[maxn],root[maxn],n,m,q,rank[maxn];
6
7 struct node{
8 int L,R,lc,rc,sum;
9 }T[maxn*5];
10
11 int build(int L,int R,int pos){
12 int rt = ++TIM;
13 T[rt].L = L, T[rt].R = R;
14 if(L == R){ T[rt].sum = 1; return rt; }
15 int mid = (L+R)>>1;
16 if(pos <= mid) T[rt].lc = build(L,mid,pos);
17 else T[rt].rc = build(mid+1,R,pos);
18 T[rt].sum = T[T[rt].lc].sum+T[T[rt].rc].sum;
19 return rt;
20 }
21
22 int query(int rt,int pos){
23 if(!rt || pos > T[rt].sum) return -1;
24 if(T[rt].L == T[rt].R) return chart[T[rt].L];
25 if(pos <= T[T[rt].lc].sum) return query(T[rt].lc,pos);
26 else return query(T[rt].rc,pos-T[T[rt].lc].sum);
27 }
28
29 int Merge(int rt_a,int rt_b){
30 if(!rt_a||!rt_b) return rt_a^rt_b;
31 T[rt_a].lc = Merge(T[rt_a].lc,T[rt_b].lc);
32 T[rt_a].rc = Merge(T[rt_a].rc,T[rt_b].rc);
33 T[rt_a].sum += T[rt_b].sum;
34 return rt_a;
35 }
36
37 int pre[maxn*4]; int find(int x){
38 if(pre[x] == x) return x;
39 else{ pre[x] = find(pre[x]); return pre[x]; }
40 }void unite(int u,int v){
41 if(find(root[u]) == find(root[v]) && root[u] == root[v]) return;
42 Merge(find(root[u]),find(root[v]));
43 pre[find(root[v])] = find(root[u]);
44 }
45
46 void Q(){
47 int x,k; scanf("%d%d",&x,&k);
48 int ans = query(find(root[x]),k);
49 printf("%d\n",ans);
50 }
51
52 void B(){
53 int u,v; scanf("%d%d",&u,&v);
54 if(!u && !v) return;
55 unite(u,v);
56 }
57
58 int main(){
59 // freopen("input7.in","r",stdin);
60 // freopen("1.out","w",stdout);
61
62 scanf("%d%d",&n,&m);
63
64 for(int i = 1;i <= n;i++){ scanf("%d",&rank[i]); chart[rank[i]] = i; }
65
66 for(int i = 1;i <= n;i++) root[i] = build(1,n,rank[i]),pre[root[i]] = root[i];
67
68 for(int i = 1;i <= m;i++){
69 int u,v; scanf("%d%d",&u,&v);
70 if(!u && !v) continue;
71 unite(u,v);
72 }
73
74 // printf("root[34534]: %d\n",root[34534]);
75
76 scanf("%d",&q);
77
78 for(int i = 1;i <= q;i++){
79 char ctr[2]; scanf("%s",ctr);
80 if(ctr[0] == ‘Q‘) Q();
81 else B();
82 }return 233;
83
84 return 0;
85 }