#include<iostream>
#define int long long
using namespace std;
int n, T, lg[100010];
int a[100010];
int f[100010][32];
void read(int &x){
x = 0;
char ch = getchar();
while(!isdigit(ch)) {
ch = getchar();
}
while(isdigit(ch)) {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
}
void pre(int l) {
lg[1] = 0, lg[2] = 1;
for(int i = 3;i <= l;i++) {
lg[i] = lg[i / 2] + 1;
}
}
signed main(){
read(n), read(T);
for(int i = 1;i <= n;i++) {
read(a[i]);
f[i][0] = a[i];
}
pre(n + 10);
for(int j = 1;j <= lg[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(T--) {
int x, y;
read(x), read(y);
int s = lg[y - x + 1];
cout << max(f[x][s], f[y - (1 << s) + 1][s]) << "\n";
}
}