#include using namespace std; typedef signed long long ll; #undef _P #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 ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++) #define ALL(a) (a.begin()),(a.end()) #define ZERO(a) memset(a,0,sizeof(a)) #define MINUS(a) memset(a,0xff,sizeof(a)) //------------------------------------------------------- int N; ll H[505050]; int X1[505050],Y1[505050]; int X2[505050],Y2[505050]; vector Ys[101010]; int C[505050]; int Q; int QX[101010],QY[101010]; ll ret[101010]; vector add[101010],del[101010],query[101010]; template class STma { public: vector val, ma; STma(){ val.resize(NV*2,0); ma.resize(NV*2,0); }; V getval(int x,int y,int l=0,int r=NV,int k=1) { if(r<=x || y<=l) return 0; if(x<=l && r<=y) return ma[k]; return val[k]+max(getval(x,y,l,(l+r)/2,k*2),getval(x,y,(l+r)/2,r,k*2+1)); } void update(int x,int y, V v,int l=0,int r=NV,int k=1) { if(l>=r) return; if(x<=l && r<=y) { val[k]+=v; ma[k]+=v; } else if(l < y && x < r) { update(x,y,v,l,(l+r)/2,k*2); update(x,y,v,(l+r)/2,r,k*2+1); ma[k]=val[k]+max(ma[k*2],ma[k*2+1]); } } }; template class STmi { public: vector val, ma; STmi(){ int i; val.resize(NV*2,0); ma.resize(NV*2,0); }; V getval(int x,int y,int l=0,int r=NV,int k=1) { if(r<=x || y<=l) return 1<<20; if(x<=l && r<=y) return ma[k]; return val[k]+min(getval(x,y,l,(l+r)/2,k*2),getval(x,y,(l+r)/2,r,k*2+1)); } void update(int x,int y, V v,int l=0,int r=NV,int k=1) { if(l>=r) return; if(x<=l && r<=y) { val[k]+=v; ma[k]+=v; } else if(l < y && x < r) { update(x,y,v,l,(l+r)/2,k*2); update(x,y,v,(l+r)/2,r,k*2+1); ma[k]=val[k]+min(ma[k*2],ma[k*2+1]); } } }; STma st1; STmi st2; template class BIT { public: V bit[1< bt; int id[101010]; void go(int c,int y1,int y2,int add) { int st=id[c]; int sy=y1; if(add==-1) { st1.update(st+y1,st+y2,-1); st2.update(st+y1,st+y2,-1); } while(sy=0;i--) if(la-(1<sy && st2.getval(st+sy,st+la-(1<=0;i--) if(sy+(1< cand[101010]; void solve() { int i,j,k,l,r,x,y; string s; cin>>N; FOR(i,N) cin>>H[i]; FOR(i,N) { cin>>X1[i]>>Y1[i]>>X2[i]>>Y2[i]>>C[i]; C[i]--; cand[C[i]].push_back(i); Ys[C[i]].push_back(Y1[i]); Ys[C[i]].push_back(Y2[i]); add[X1[i]].push_back(i); del[X2[i]].push_back(i); } /* FOR(i,N) { Ys[i].push_back(2*N+1); sort(ALL(Ys[i])); Ys[i].erase(unique(ALL(Ys[i])),Ys[i].end()); id[i+1]=id[i]+Ys[i].size(); } FOR(i,N) { Y1[i]=lower_bound(ALL(Ys[C[i]]),Y1[i])-Ys[C[i]].begin(); Y2[i]=lower_bound(ALL(Ys[C[i]]),Y2[i])-Ys[C[i]].begin(); } */ cin>>Q; FOR(i,Q) { cin>>QX[i]>>QY[i]; //query[QX[i]].push_back(i); FOR(j,N) { FORR(c,cand[j]) { if(X1[c]<=QX[i]&&QX[i]