#include <iostream>//cin cout
#include <cmath> 
using namespace std;//命名空间
int x, y;
const int N = 100005;
int a[N];
int st[N][35], log1[N];
inline int query(int x, int y)
{
	int k = log1[y - x + 1];
	return max(st[x][k], st[y - (1 << k) + 1][k]);
}
int main()//主函数
{
	int n, m;
	scanf("%d%d", &n, &m);
	log1[0] = -1;
	for(int i = 1; i <= n; i++){
		scanf("%d", &a[i]);
		st[i][0] = a[i];
		log1[i] = log1[i >> 1] + 1;
	}
	for(int j = 1; j <= log1[n]; j++){
		for(int i = 1; i + (1 << j) - 1 <= n; i++){
			st[i][j] = max(st[i][j - 1], st[i + (1 << j - 1)][j - 1]);
		}
	}
	for(int i = 1; i <= m; i++){
		scanf("%d%d", &x, &y);
		printf("%d\n", query(x, y));
	}
	return 0;//华丽结束
}