#include<bits/stdc++.h>
using namespace std;
int f[2000010][24],n;
int get(int x,int y){
	int k=log(y-x+1)/log(2);
	return max(f[x][k],f[y-(1<<k)+1][k]);
}
int main(){
	int i,j,m,t,k;
	scanf("%d %d",&n,&m);
	for(i=1;i<=n;i++)
	scanf("%d",&f[i][0]);
	int x=log(n)/log(2)+1;
	for(int j=1;j<x;j++)
	for(int i=1;i<=n-(1<<j)+1;i++)
	f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]);
	while(m--){
		int a,b;
		scanf("%d %d",&a,&b);
		printf("%d\n",get(a,b));
		
		
	}

}