#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ref(i,l,r) for(int i = l;i<=r;i++)
const int N = 1e5+10;
struct node{
int l,r,maxv;
}tr[N*4];
int a[N];
int n,m;
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,a[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;
else{
int mid = (tr[u].l + tr[u].r) >> 1;
int maxv = INT_MIN;
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()
{
IOS
//cin >> n >> m;
scanf("%d%d",&n,&m);
for(int i = 1;i<=n;i++) scanf("%d",&a[i]);
build(1,1,n);
while(m--){
int l,r;
//cin >> l >> r;
scanf("%d%d",&l,&r);
// cout << query(1,l,r) << endl;
printf("%d\n",query(1,l,r));
}
return 0;
}