#include #include #include #include #include #include #include #include #include #include #include #include #define SZ(X) ((int)(X).size()) #define ALL(X) (X).begin(), (X).end() #define REP(I, N) for (int I = 0; I < (N); ++I) #define REPP(I, A, B) for (int I = (A); I < (B); ++I) #define RI(X) scanf("%d", &(X)) #define RII(X, Y) scanf("%d%d", &(X), &(Y)) #define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z)) #define DRI(X) int (X); scanf("%d", &X) #define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y) #define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z) #define RS(X) scanf("%s", (X)) #define CASET int ___T, case_n = 1; scanf("%d ", &___T); while (___T-- > 0) #define MP make_pair #define PB push_back #define MS0(X) memset((X), 0, sizeof((X))) #define MS1(X) memset((X), -1, sizeof((X))) #define LEN(X) strlen(X) #define PII pair #define VI vector #define VPII vector > #define PLL pair #define VPLL vector > #define F first #define S second typedef long long LL; using namespace std; const int MOD = 1e9+7; const int SIZE = 4e5+10; pair >ppp[SIZE]; int a[SIZE],an[SIZE]; int lt[SIZE]; int BIT[SIZE]; void ins(int x,int v){ x=SIZE-x-1; for(;x >& x){ if(x.F>x.S.F)return 0; if(x.S.F>x.S.S)return 0; return 1; } int main(){ CASET{ DRI(n); REP(i,n)RI(a[i]); if(n<3){ DRI(Q); REP(i,Q){ DRII(x,y); puts("0"); } continue; } REP(i,n-2){ ppp[i]=MP(a[i],MP(a[i+1],a[i+2])); } sort(ppp,ppp+n-2); int dn=unique(ppp,ppp+n-2)-ppp; REP(i,n-2)a[i]=lower_bound(ppp,ppp+dn,MP(a[i],MP(a[i+1],a[i+2])))-ppp; memset(lt,-1,sizeof(int)*dn); DRI(Q); REP(i,n-2)query[i].clear(); REP(i,Q){ DRII(x,y); x--;y--; y-=2; if(x>y)an[i]=0; else{ query[y].PB(MP(x,i)); } } MS0(BIT); REP(i,n-2){ if(valid(ppp[a[i]])){ ins(i,1); if(lt[a[i]]!=-1){ ins(lt[a[i]],-1); } lt[a[i]]=i; } REP(j,SZ(query[i])){ an[query[i][j].S]=qq(query[i][j].F); } } REP(i,Q){ printf("%d\n",an[i]); } } return 0; }