1 #pragma GCC optimze("O3")
2 #include<iostream>
3 #include<cstdlib>
4 #include<cstdio>
5 #include<cstring>
6 #include<queue>
7 #include<algorithm>
8 #include<cmath>
9 #include<map>
10 #define N 100004
11 using namespace std;
12 int n,m,q,f[N],fa[N],size[N],ch[N][2],data[N];
13 int find(int x)
14 {
15 if(f[x]==x)return x;
16 return f[x]=find(f[x]);
17 }
18 bool get(int x)
19 {
20 return x==ch[fa[x]][1];
21 }
22 void update(int x)
23 {
24 if(x)
25 {
26 size[x]=1;
27 if(ch[x][0]) size[x]+=size[ch[x][0]];
28 if(ch[x][1]) size[x]+=size[ch[x][1]];
29 }
30 }
31 void rotate(int x)
32 {
33 int faa=fa[x],ffa=fa[fa[x]];
34 int op=get(x);
35 ch[faa][op]=ch[x][op^1];
36 fa[ch[faa][op]]=faa;
37 ch[x][op^1]=faa;
38 fa[faa]=x;
39 fa[x]=ffa;
40 if(ffa)ch[ffa][ch[ffa][1]==faa]=x;
41 update(faa);
42 update(x);
43 return;
44 }
45 void splay(int x)
46 {
47 for(int ff;ff=fa[x];rotate(x))
48 {
49 if(fa[ff])
50 rotate((get(x)==get(ff))?ff:x);
51 }
52 return;
53 }
54 int lart;
55 void insert(int x,int now,int faa)
56 {
57
58 if(now==0)
59 {
60 fa[x]=faa;
61 if(data[x]<data[faa]) ch[faa][0]=x;
62 else ch[faa][1]=x;
63 splay(x);
64 lart=x;
65 return;
66 }
67 if(data[x]<data[now])insert(x,ch[now][0],now);
68 else insert(x,ch[now][1],now);
69 }
70 void dfs(int x,int y)
71 {
72 int le=ch[x][0],ri=ch[x][1];
73 ch[x][0]=ch[x][1]=0;
74 insert(x,y,0);
75 if(le)dfs(le,lart);
76 if(ri)dfs(ri,lart);
77 }
78 int get_rank(int x,int la)
79 {
80 if(la==size[ch[x][0]]+1)
81 return x;
82 if(la<=size[ch[x][0]])
83 return get_rank(ch[x][0],la);
84 else
85 return get_rank(ch[x][1],la-size[ch[x][0]]-1);
86 }
87 int main()
88 {
89 scanf("%d%d",&n,&m);
90 for(int i=1;i<=n;i++)
91 {
92 scanf("%d",&data[i]);
93 f[i]=i;size[i]=1;
94 }
95 for(int i=1;i<=m;i++)
96 {
97 int x,y;
98 scanf("%d%d",&x,&y);
99 int aa=find(x),bb=find(y);
100 if(aa==bb)continue;
101 f[aa]=bb;
102 splay(x),splay(y);
103 if(size[x]>size[y])
104 dfs(y,x);
105 else
106 dfs(x,y);
107 }
108 scanf("%d",&q);
109 char b[10];
110 for(int i=1;i<=q;i++)
111 {
112 scanf("%s",b);
113 int x,y;
114 scanf("%d%d",&x,&y);
115 if(b[0]==‘B‘)
116 {
117 int aa=find(x),bb=find(y);
118 if(aa==bb)continue;
119 f[aa]=bb;
120 splay(x),splay(y);
121 if(size[x]>size[y])
122 dfs(y,x);
123 else
124 dfs(x,y);
125 }
126 else
127 {
128 splay(x);
129 if(size[x]<y)printf("-1\n");
130 else
131 {
132 printf("%d\n",get_rank(x,y));
133 }
134 }
135 }
136 return 0;
137 }