#pragma GCC optimize(3)
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
struct T{
int l, r, v;
}tr[N << 2];
int n, m;
int a[N];
inline void pushup(int u) {
tr[u].v = max(tr[u << 1].v, tr[u << 1 | 1].v);
}
void build(int u, int l, int r) {
tr[u] = {l, r, a[l]};
if(l == r) return;
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].v;
else {
int mid = tr[u].l + tr[u].r >> 1;
int v = -0x7fffffff;
if(l <= mid) v = max(v, query(u << 1, l, r));
if(r > mid) v= max(v, query(u << 1 | 1, l, r));
return v;
}
}
int main()
{
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
build(1, 1, n);
for(; m--; ) {
int l, r;
scanf("%d%d", &l, &r);
printf("%d\n", query(1, l, r));
}
return 0;
}