#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXN = 500005;
const int K = 20;
int st[MAXN][K];
int lg[MAXN];
void build(int n, const vector<int>& arr) {
lg[1] = 0;
for (int i = 2; i <= n; i++)
lg[i] = lg[i / 2] + 1;
for (int i = 0; i < n; i++)
st[i][0] = arr[i];
for (int j = 1; j < K; j++)
for (int i = 0; i + (1 << j) <= n; i++)
st[i][j] = max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
int query(int L, int R) {
int j = lg[R - L + 1];
return max(st[L][j], st[R - (1 << j) + 1][j]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
build(n, arr);
int m;
cin >> m;
while (m--) {
int l, r;
cin >> l >> r;
if (l > r) swap(l, r);
cout << query(l - 1, r - 1) << "\n";
}return 0;
}