/*
author :hzoi_ztx
title :cogs 58. 延绵的山峰
ALG :RMQ
comment: 默写 = =
*/
#include <cstdio>
#define maxn 1000010
#define maxk 21
int n , Q ;
int d[maxn][maxk] = {0} ;
inline int max(int a , int b) { return a > b ? a : b ; }
int main() {
#define READ
#ifdef READ
freopen("climb.in" ,"r",stdin ) ;
freopen("climb.out","w",stdout) ;
#endif
int i , k , x , y ;
scanf("%d", &n ) ;
for (i = 0 ; i <= n ; i ++ )
scanf("%d", &d[i][0] ) ;
for (k = 1 ; (1<<k) <= n ; k ++ )
for (i = 0 ; i+(1<<k)-1 <= n ; i ++ )
d[i][k] = max(d[i][k-1] , d[i+(1<<(k-1))][k-1]) ;
scanf("%d", &Q ) ;
while ( Q -- ) {
scanf("%d%d", &x , &y ) ;
for (k = 0 ; (1<<(k+1)) < (y-x+1) ; k ++ ) ;
printf("%d\n", max(d[x][k] , d[y-(1<<k)+1][k])) ;
}
return 0 ;
}