#include <iostream>
#include <cstdio>
using namespace std;
const int N = 1E5 + 10;
int w[N];
struct node{
int l, r;
int maxv;
}tr[N * 4];
void pushup(int u){
tr[u].maxv = max(tr[u << 1].maxv, tr[u << 1 | 1].maxv);
}
void build(int u, int l, int r){
if(l == r) tr[u] = {l, r, w[l]};
else{
tr[u] = {l, r};
int mid = l + r >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
pushup(u);
}
}
int query(int u, int l, int r){
if(tr[u].l >= l && tr[u].r <= r) return tr[u].maxv;
int mid = tr[u].l + tr[u].r >> 1;
int maxv = -0x3f3f;
if(l <= mid) maxv = max(maxv, query(u << 1, l, r));
if(r > mid) maxv = max(maxv, query(u << 1 | 1, l, r));
return maxv;
}
int main(){
int n, m; cin >> n >> m;
for(int i = 1; i <= n; i ++ ) scanf("%d", &w[i]);
build(1, 1, n);
while(m -- ){
int l, r; scanf("%d%d", &l, &r);
// cout << query(1, l, r) << endl; 超时可能是cout的问题
printf("%d\n", query(1, l, r));
}
return 0;
}