intread(){ int x = 0; char c = gc(); while (!isdigit(c)) c = gc(); while (isdigit(c)) x = x * 10 + c - '0', c = gc(); return x; }
int n, m, a[maxn];
int Log[maxn], st[maxn][21]; voidinit_st(){ Log[0] = -1; for (int i = 1; i <= n; ++i) Log[i] = Log[i >> 1] + 1, st[i][0] = a[i]; for (int j = 1; j <= 20; ++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]); }
intquery(int l, int r){ int k = Log[r - l + 1]; return max(st[l][k], st[r - (1 << k) + 1][k]); }
intmain(){ n = read(); m = read(); for (int i = 1; i <= n; ++i) a[i] = read(); init_st(); for (int i = 1; i <= m; ++i) { int l = read(), r = read(); printf("%d\n", query(l, r)); } return0; }