#include <bits/stdc++.h>
using namespace std ;
int A[50001] , Seg[4 * 50001];
void built(int l , int r , int p = 1){
if(l == r) Seg[p] = A[l] ;
else {
int mid = (l + r) / 2 ;
built(l , mid , (p << 1)) ;
built(mid + 1 , r , 1 + (p << 1)) ;
Seg[p] = max(Seg[(p << 1)] , Seg[1 + (p << 1)]) ;
}
}
int RMQ(int i , int j , int l , int r , int p = 1){
// No OVERLAP
if(l > j || i > r) return INT_MIN ;
// TOTAL OVERLAP
if(l >= i && j >= r) return Seg[p] ;
int mid = (l + r) / 2 ;
int p1 = RMQ(i , j , l , mid , (p << 1)) ;
int p2 = RMQ(i , j , mid + 1 , r , 1 + (p << 1)) ;
return max(p1 , p2) ;
}
int main(){
long n ;
cin >> n ;
for(int i = 0 ; i < n ; i++)
cin >> A[i] ;
built(0 , n - 1) ;
int q ;
cin >> q ;
while(q--){
int a , b ;
cin >> a >> b;
cout << RMQ(a - 1 , b - 1 , 0 , n - 1) << endl;
}
return 0 ;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkIDsKaW50IEFbNTAwMDFdICwgU2VnWzQgKiA1MDAwMV07Cgp2b2lkIGJ1aWx0KGludCBsICwgaW50IHIgLCBpbnQgcCA9IDEpewogICAgaWYobCA9PSByKSBTZWdbcF0gPSBBW2xdIDsKICAgIGVsc2UgewogICAgICAgIGludCBtaWQgPSAobCArIHIpIC8gMiA7CiAgICAgICAgYnVpbHQobCAsIG1pZCAsIChwIDw8IDEpKSA7CiAgICAgICAgYnVpbHQobWlkICsgMSAsIHIgLCAxICsgKHAgPDwgMSkpIDsKICAgICAgICBTZWdbcF0gPSBtYXgoU2VnWyhwIDw8IDEpXSAsIFNlZ1sxICsgKHAgPDwgMSldKSA7CiAgICB9Cn0KCmludCBSTVEoaW50IGkgLCBpbnQgaiAsIGludCBsICwgaW50IHIgLCBpbnQgcCA9IDEpewogICAgLy8gTm8gT1ZFUkxBUAogICAgaWYobCA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