https://codeforces.com/contest/1878/problem/E
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
static StringBuilder sb = new StringBuilder();
static StringTokenizer tk = null;
static int T, n, q, l, k, r, ubn;
static int[] segtree;
static int ALL_1 = 0x7fffffff;
public static void main(String[] args) throws Exception {
T = Integer.parseInt(rd.readLine());
for (int t = 1; t <= T; t++) {
n = Integer.parseInt(rd.readLine());
ubn = pow2ub(n);
segtree = new int[ubn*2];
tk = new StringTokenizer(rd.readLine());
for (int i=1; i<ubn; i++) segtree[i] = ALL_1;
for (int i=ubn; i<ubn+n; i++) segtree[i] = Integer.parseInt(tk.nextToken());
for (int i=ubn+n; i<ubn*2; i++) segtree[i] = ALL_1;
for (int i=ubn*2-1; i>1; i--) {
segtree[i/2] &= segtree[i];
}
q = Integer.parseInt(rd.readLine());
for (int i=0; i<q; i++) {
tk = new StringTokenizer(rd.readLine());
l = Integer.parseInt(tk.nextToken()) - 1;
k = Integer.parseInt(tk.nextToken());
if (segtree[ubn+l] < k) {
sb.append(-1).append(' ');
continue;
}
int lo = l;
int hi = n;
int mid = -1;
while (lo < hi) {
mid = lo + (hi - lo) / 2;
if (bitands(l, mid, 0, ubn, 1) >= k) {
lo = mid + 1;
}
else {
hi = mid;
}
}
sb.append(lo).append(' ');
}
sb.append('\n');
}
System.out.println(sb);
}
static int pow2ub(int n) {
for (int i=0; i<32; i++) {
if (1 << i >= n) return 1 << i;
}
return -1;
}
static int bitands(int l, int r, int s, int e, int idx) {
if (e - s == 1)
return segtree[idx];
if (l <= s && e < r)
return segtree[idx];
int m = (s + e) / 2;
int ret = ALL_1;
if (l < m) ret &= bitands(l, r, s, m, idx*2);
if (r >= m) ret &= bitands(l, r, m, e, idx*2+1);
return ret;
}
}