#include using namespace std; typedef signed long long ll; #define _P(...) (void)printf(__VA_ARGS__) #define FOR(x,to) for(x=0;x<(to);x++) #define FORR(x,arr) for(auto& x:arr) #define FORR2(x,y,arr) for(auto& [x,y]:arr) #define ALL(a) (a.begin()),(a.end()) #define ZERO(a) memset(a,0,sizeof(a)) #define MINUS(a) memset(a,0xff,sizeof(a)) template bool chmax(T &a, const T &b) { if(a bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;} //------------------------------------------------------- int N; int P[202020],rev[202020]; int F[202020]; template class BIT { public: V bit[1<=0;i--) if(tv+bit[ent+(1< bit,S,T,U; int ret[202020]; vector ev[202020],query[202020]; int Q; int X[202020],Y[202020]; void solve() { int i,j,k,l,r,x,y; string s; cin>>N; FOR(i,N) { cin>>P[i]; rev[P[i]]=i; F[i]=bit(P[i]); bit.add(P[i],1); S.add(P[i],1); ev[F[i]].push_back(i); } cin>>Q; FOR(i,Q) { cin>>X[i]>>Y[i]; query[X[i]].push_back(i); } for(int k=1;k<=N;k++) { FORR(e,ev[k-1]) { S.add(P[e],-1); T.add(P[e],1); U.add(e,1); } FORR(i,query[k]) { if(S(Y[i])-S(Y[i]-1)) { ret[i]=rev[Y[i]]+1; } else { int n=T(N)-T(Y[i]-1); ret[i]=U.lower_bound(n+k)+1; } } } FOR(i,Q) cout<