#include #include #include #define rep(i,n) for(int i=0; i=0; i--) using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N, K, L; cin >> N >> K >> L; vector A(N); rep(i,N) cin >> A[i]; vector I(N); rep(i,N) I[i] = i; sort(I.begin(), I.end(), [&](int l, int r){ return A[l] < A[r]; }); vector> ans(N+1); rep(i,N+1) ans[i].assign(i,-1); vector cnt0(N+1); vector borderRtoL(N+1,0); vector borderLtoR(N+1,N); vector borderFull(N+1,0); vector sumRtoL(N+2, 0); auto query = [&](int l, int r) -> int { if(cnt0[r] - cnt0[l] < K) return 0; int l0 = borderRtoL[r] - 1; int r0 = borderLtoR[l] + 1; int c = 0; c += (sumRtoL[r+1] - sumRtoL[r0]) - (r+1-r0) * l; return c; }; rep(i,N){ int p = I[i]; for(int j=p+1; j<=N; j++) cnt0[j]++; for(int r=1; r<=N; r++) while(cnt0[r] - cnt0[borderRtoL[r]] >= K) borderRtoL[r]++; for(int l=0; l<=N; l++) while(cnt0[borderLtoR[l]] - cnt0[l] >= K) borderLtoR[l]--; rep(j,N+1) sumRtoL[j+1] = sumRtoL[j] + borderRtoL[j]; for(int r=0; r<=N; r++) while(query(borderFull[r],r) >= L){ ans[r][borderFull[r]++] = i; } } int Q; cin >> Q; rep(qi,Q){ int l,r; cin >> l >> r; l--; cout << A[I[ans[r][l]]] << '\n'; } return 0; }