#include<bits/stdc++.h> #define re register #define int long long
usingnamespace std;
constint N = 2e5 + 10,M = 34; int T,n,q; int arr[N]; int num[M][N];
inlineintread(){ int r = 0,w = 1; char c = getchar(); while (c < '0' || c > '9'){ if (c == '-') w = -1; c = getchar(); } while (c >= '0' && c <= '9'){ r = (r << 3) + (r << 1) + (c ^ 48); c = getchar(); } return r * w; }
inlineboolcheck(int l,int r,int k){ int sum = 0; for (re int bit = 0;bit <= 30;bit++){ int cnt = num[bit][r] - num[bit][l - 1]; if (cnt == r - l + 1) sum += (1ll << bit); } return (sum >= k); }
signedmain(){ T = read(); while (T--){ n = read(); for (re int i = 0;i <= 30;i++){ for (re int j = 1;j <= n;j++) num[i][j] = 0; } for (re int i = 1;i <= n;i++){ arr[i] = read(); for (re int bit = 0;bit <= 30;bit++){ if (arr[i] >> bit & 1) num[bit][i] = 1; num[bit][i] += num[bit][i - 1]; } } q = read(); while (q--){ int l,L,r = n,x; L = l = read(); x = read(); while (l < r){ int mid = l + r + 1 >> 1; if (check(L,mid,x)) l = mid; else r = mid - 1; } if (check(L,l,x)) printf("%lld ",l); elseprintf("-1 "); } puts(""); } return0; }