#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
const long long maxn=100010;
int n, m;
int llog[maxn], f[maxn][20];
inline void qread(int &x){
x = 0;
int ch = getchar();
while(ch < '0' || ch > '9') ch =getchar();
while(ch >='0' && ch <= '9') x = 10 * x + ch - 48, ch = getchar();
}
int main(void)
{
qread(n), qread(m);
for(int i=2; i<=n; ++i)
llog[i] = llog[i / 2] + 1;
for(int i=1; i<=n; ++i)
qread(f[i][0]);
for(int j=1; j <= llog[n]; ++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--){
int x, y;
qread(x), qread(y);
int s = llog[y - x + 1];
printf("%d\n", max(f[x][s], f[y - (1 << s) + 1][s]));
}
}