#include<iostream> #include<cstring> #include<cstdio> #include<cmath> #include<algorithm> #include<cstdlib> #include<map> #include<queue> #include <deque> #include <list> #include <ctime> #include <stack> #include <vector> #include<set> #define Maxn 1024 #define MOD typedef long long ll; #define FOR(i,j,n) for(int i=j;i<=n;i++) #define DFR(i,j,k) for(int i=j;i>=k;--i) #define lowbit(a) a&-a #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 const int inf = 0x3f3f3f3f; const double pi = acos(-1.0); using namespace std; int MAX[Maxn<<2]; void PushUP(int rt) { MAX[rt] = max(MAX[rt<<1],MAX[rt<<1|1]); } void build(int l,int r,int rt) { if(l==r) { scanf("%d",&MAX[rt]); return ; } int m=(l+r)>>1; build(lson); build(rson); PushUP(rt); } void update(int p,int sc,int l,int r,int rt) { if(l==r) { MAX[rt]=sc; return ; } int m=(l+r)>>1; if(p<=m)update(p,sc,lson); else update(p,sc,rson); PushUP(rt); } int query(int L,int R,int l,int r,int rt) { if(L<=l && r<=R)return MAX[rt]; int m=(l+r)>>1; int ret =0; if(L<=m)ret = max(ret,query(L,R,lson)); if(R>m) ret = max(ret,query(L,R,rson)); return ret; } int main() { int T,n,l,r,q; scanf("%d",&T); while(T--) { scanf("%d",&n); build(1,n,1); scanf("%d",&q); while(q--) { scanf("%d%d",&l,&r); printf("%d\n",query(l,r,1,n,1)); } } return 0; }