1
10 2 20
8 5 2 4 10 3 9 7 1 6
5 10
3 17
6 31
3 46
10 42
7 19
9 36
7 3
3 13
8 32
10 25
9 34
10 34
6 33
3 9
2 17
4 5
10 44
9 15
5 40
 1
47 1 47
40 45 19 10 44 33 47 12 2 46 16 23 3 14 21 28 31 42 22 18 35 38 24 25 36 32 4 34 5 20 37 39 6 9 1 7 29 8 17 27 30 11 13 41 15 43 26
39 25
41 24
32 30
10 6
30 13
24 35
41 17
31 31
9 44
21 8
43 23
8 5
29 13
8 4
47 20
22 10
37 27
17 39
43 23
14 42
46 19
43 24
30 31
24 35
31 13
31 32
20 9
3 2
20 9
14 7
7 3
21 36
32 30
47 19
11 43
6 2
31 31
26 12
47 20
2 1
40 24
16 40
45 21
36 28
36 28
17 38
30 31
 1
1 1 4
1
1 1
1 2
1 3
1 4
 1
10 1 1
10 4 6 5 7 9 8 1 3 2
6 10

3
2
7
10
6
7
7
7
5
1
6
6
1
9
4
4
4
1
1
3
 27
27
37
16
20
24
11
20
46
35
11
2
34
47
26
24
7
28
11
3
26
13
37
24
37
37
22
10
22
14
47
38
37
43
23
33
20
4
26
19
30
21
43
1
1
42
37
 1
-1
-1
-1
 9

#include <bits/stdc++.h>

int main() {
	using namespace std;
	ios_base::sync_with_stdio(false), cin.tie(nullptr);
	int T; cin >> T;
	while (T--) {
		int N, C, Q; cin >> N >> C >> Q;
		vector<int> P(N); for (auto& a : P) cin >> a;

		const int64_t INF = 2e18;

		vector<vector<array<int, 2>>> cur_flip(C+1);
		vector<vector<int64_t>> weight(C+1);
		cur_flip[0] = vector<array<int, 2>>(1, {N, N});
		weight[0] = vector<int64_t>({0, 1});

		vector<vector<array<int, 2>>> bounds(C+1, vector<array<int, 2>>(N+1, {0,1}));
		vector<vector<int64_t>> node_weight(C+1, vector<int64_t>(N+1, 1));

		for (int c = 1; c <= C; c++) {
			cur_flip[c] = vector<array<int, 2>>(2*N*c+1, {N,N});
			weight[c] = vector<int64_t>(2*N*c+2, 0);

			int lo = N*c;
			int hi = N*c+1;
			weight[c][lo] = 0;
			weight[c][hi] = 1;
			bounds[c][N] = {lo, hi};
			for (int i = N-1; i >= 0; i--) {
				// we could do nothing
				vector<pair<int, int>> opts(min(N-i, c+1));
				for (int v = 0; v < min(N-i, c+1); v++) {
					opts[v] = {P[i+v], v};
				}
				sort(opts.begin(), opts.end());
				int x0 = 0;
				while (opts[x0].second != 0) x0++;
				for (int z = x0-1; z >= 0; z--) {
					int v = opts[z].second;
					assert(v != 0);
					auto w = node_weight[c-v][i+v+1];
					weight[c][lo-1] = weight[c][lo] - w;
					cur_flip[c][lo-1] = {i, i+v};
					lo--;
				}
				for (int z = x0+1; z < int(opts.size()); z++) {
					int v = opts[z].second;
					assert(v != 0);
					auto w = node_weight[c-v][i+v+1];
					weight[c][hi+1] = weight[c][hi] + w;
					cur_flip[c][hi] = {i, i+v};
					hi++;
				}
				node_weight[c][i] = weight[c][hi] - weight[c][lo];
				bounds[c][i] = {lo, hi};
			}
		}

		auto query = [&](int idx, int64_t perm) -> int {
			int cur_c = C;
			int cur_idx = 0;
			if (perm > node_weight[cur_c][cur_idx]) return -1;
			while (true) {
				// look for the current flip
				auto [mi, ma] = bounds[cur_c][cur_idx];
				while (ma - mi > 1) {
					int md = (mi + ma) / 2;
					if (perm <= weight[cur_c][md] - weight[cur_c][mi]) {
						ma = md;
					} else {
						perm -= weight[cur_c][md] - weight[cur_c][mi];
						mi = md;
					}
				}
				assert(1 <= perm && perm <= weight[cur_c][ma] - weight[cur_c][mi]);
				auto [f1, f2] = cur_flip[cur_c][mi];
				if (idx < f1) {
					return P[idx];
				} else if (f1 <= idx && idx <= f2) {
					return P[f1 + f2 - idx];
				} else {
					cur_c -= f2 - f1;
					cur_idx = f2+1;
				}
			}
		};

		for (int q = 0; q < Q; q++) {
			int idx; int64_t perm; cin >> idx >> perm; idx--;
			cout << query(idx, perm) << '\n';
		}
	}

	return 0;
}
