#include <iostream>
#include <cstdio>
using namespace std;
const int N = 1e5 + 5;
int tree[N << 2];
void bulidTree(int node, int lef, int rig)
{
if (lef == rig)
scanf("%d", &tree[node]);
else
{
int leftNode = node << 1;
int rightNode = node << 1 | 1;
int mid = (lef + rig) >> 1;
bulidTree(leftNode, lef, mid);
bulidTree(rightNode, mid+1, rig);
tree[node] = max(tree[leftNode], tree[rightNode]);
}
}
int queryTree(int node, int lef, int rig, int L, int R)
{
if (L <= lef && rig <= R)
return tree[node];
int leftNode = node << 1;
int rightNode = node << 1 | 1;
int mid = (lef + rig) >> 1;
int maxv = -0x3f3f3f3f;
if (L <= mid)
maxv = max(maxv, queryTree(leftNode, lef, mid, L, R));
if (R > mid)
maxv = max(maxv, queryTree(rightNode, mid+1, rig, L, R));
return maxv;
}
int main()
{
int n, m;
scanf("%d %d", &n, &m);
bulidTree(1, 1, n);
while (m--)
{
int l, r;
scanf("%d %d", &l, &r);
printf("%d\n", queryTree(1, 1, n, l, r));
}
return 0;
}