#include<bits/stdc++.h>
using namespace std;
int n,m,a,b[100005][25],l,r,c,d,tot;
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",&a);
b[i][0]=a;
}
for(int j=1;j<=20;j++){
c=1<<j;
if(c>n)break;
for(int i=1;i<=n;i++){
if(i+c-1>n)break;
b[i][j]=max(b[i][j-1],b[i+c/2][j-1]);
}
}
for(int i=1;i<=m;i++){
scanf("%d%d",&l,&r);
d=0;
tot=20;
while(l<r){
c=1<<tot;
if(l+c-1<=r){
d=max(b[l][tot],d);
l+=c-1;
}
else tot--;
}
printf("%d\n",d);
}
return 0;
}