1 //It is made by Awson on 2017.10.20
2 #include <set>
3 #include <map>
4 #include <cmath>
5 #include <ctime>
6 #include <stack>
7 #include <queue>
8 #include <vector>
9 #include <string>
10 #include <cstdio>
11 #include <cstdlib>
12 #include <cstring>
13 #include <iostream>
14 #include <algorithm>
15 #define LL long long
16 #define Min(a, b) ((a) < (b) ? (a) : (b))
17 #define Max(a, b) ((a) > (b) ? (a) : (b))
18 #define Abs(x) ((x) < 0 ? (-(x)) : (x))
19 using namespace std;
20 const int N = 100000;
21
22 int n, m, a, b;
23 char opt[10];
24 int val[N+5], id[N+5];
25 struct node {
26 int key;
27 node* child[2];
28 }sgm[N*20], *root[N+5], *pos = sgm;
29 struct segment {
30 void insert(node* &o, int l, int r, int loc) {
31 if (o == NULL) o = pos++;
32 if (l == r) {
33 o->key = 1; return;
34 }
35 int mid = (l+r)>>1;
36 if (loc <= mid) insert(o->child[0], l, mid, loc);
37 else insert(o->child[1], mid+1, r, loc);
38 o->key = 0;
39 if (o->child[0]) o->key += o->child[0]->key;
40 if (o->child[1]) o->key += o->child[1]->key;
41 }
42 node* merge(node* a, node* b) {
43 if (!b || !a) return a ? a : b;
44 a->child[0] = merge(a->child[0], b->child[0]);
45 a->child[1] = merge(a->child[1], b->child[1]);
46 a->key = 0;
47 if (a->child[0]) a->key += a->child[0]->key;
48 if (a->child[1]) a->key += a->child[1]->key;
49 return a;
50 }
51 int query(node* o, int l, int r, int kth) {
52 if (l == r) return l;
53 int mid = (l+r)>>1;
54 if (!o->child[0]) return query(o->child[1], mid+1, r, kth);
55 if (o->child[0]->key >= kth) return query(o->child[0], l, mid, kth);
56 return query(o->child[1], mid+1, r, kth-o->child[0]->key);
57 }
58 }T;
59 int fa[N+5];
60 int find(int r) {
61 return fa[r] ? fa[r] = find(fa[r]) : r;
62 }
63
64 void work() {
65 scanf("%d%d", &n, &m);
66 for (int i = 1; i <= n; i++) {
67 scanf("%d", &val[i]); id[val[i]] = i;
68 }
69 for (int i = 1; i <= n; i++) T.insert(root[i], 1, n, val[i]);
70 while (m--) {
71 scanf("%d%d", &a, &b);
72 int q = find(a), p = find(b);
73 if (p != q) {
74 root[q] = T.merge(root[p], root[q]);
75 fa[p] = q;
76 }
77 }
78 scanf("%d", &m);
79 while (m--) {
80 scanf("%s%d%d", &opt, &a, &b);
81 if (opt[0] == ‘B‘) {
82 int q = find(a), p = find(b);
83 if (p != q) {
84 root[q] = T.merge(root[p], root[q]);
85 fa[p] = q;
86 }
87 }else {
88 int q = find(a);
89 if (b > root[q]->key) printf("-1\n");
90 else printf("%d\n", id[T.query(root[q], 1, n, b)]);
91 }
92 }
93 }
94 int main() {
95 work();
96 return 0;
97 }