#include using namespace std; using ll = long long; #define rep(i,n) for(int i=0;i>n; vector> ht(n,vector(3)); rep(i,n) cin>>ht.at(i).at(0); rep(i,n) cin>>ht.at(i).at(1); rep(i,n) ht.at(i).at(2)=i; sort(ht.begin(),ht.end()); vector p(n); rep(i,n) p.at(ht.at(i).at(2))=i; vector cnd(n+1,-1); vector cph(n); rep(i,n) cph.at(i)=ht.at(i).at(0); rep(i,n){ auto it=upper_bound(cph.begin(),cph.end(),ht.at(i).at(1)); cnd.at(i+1)=max(cnd.at(i),int(it-cph.begin()-1)); } vector> ddv(30,vector(n)); rep(i,n) ddv.at(0).at(i)=cnd.at(i+1); rep(i,29){ rep(j,n){ ddv.at(i+1).at(j)=ddv.at(i).at(ddv.at(i).at(j)); } } int q; cin>>q; rep(Qi,q){ int a,b; cin>>a>>b; a--; b--; int na=p.at(a),nb=p.at(b); int ans=1; na=upper_bound(cph.begin(),cph.end(),ht.at(na).at(1))-cph.begin()-1; if(na=0;i--){ if(ddv.at(i).at(na)