#include<iostream>
#include<cstdio>
using namespace std;
int log[1000000],f[1000000][25],a[1000000];
int main() {
int n,m,x,y;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
log[0]=-1;
for(int i=1;i<=n;i++) {
f[i][0]=a[i];
log[i]=log[i/2]+1;
}
for(int j=1;j<=20;j++)
for(int i=1;i+(1<<j)-1<=n;i++)
f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]);
while(m--) {
scanf("%d%d",&x,&y);
int s=log[y-x+1];
printf("%d\n",max(f[x][s],f[y-(1<<s)+1][s]));
}
return 0;
}