#include <vector>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <fstream>
using namespace std;
class InParser {
private:
FILE *fin;
char *buff;
int sp;
char read_ch() {
++sp;
if (sp == 4096) {
sp = 0;
fread(buff, 1, 4096, fin);
}
return buff[sp];
}
public:
InParser(const char* nume) {
fin = fopen(nume, "r");
buff = new char[4096]();
sp = 4095;
}
InParser& operator >> (int &n) {
char c;
while (!isdigit(c = read_ch()) && c != '-');
int sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
InParser& operator >> (long long &n) {
char c;
n = 0;
while (!isdigit(c = read_ch()) && c != '-');
long long sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
};
class OutParser {
private:
FILE *fout;
char *buff;
int sp;
void write_ch(char ch) {
if (sp == 50000) {
fwrite(buff, 1, 50000, fout);
sp = 0;
buff[sp++] = ch;
} else {
buff[sp++] = ch;
}
}
public:
OutParser(const char* name) {
fout = fopen(name, "w");
buff = new char[50000]();
sp = 0;
}
~OutParser() {
fwrite(buff, 1, sp, fout);
fclose(fout);
}
OutParser& operator << (int vu32) {
if (vu32 <= 9) {
write_ch(vu32 + '0');
} else {
(*this) << (vu32 / 10);
write_ch(vu32 % 10 + '0');
}
return *this;
}
OutParser& operator << (long long vu64) {
if (vu64 <= 9) {
write_ch(vu64 + '0');
} else {
(*this) << (vu64 / 10);
write_ch(vu64 % 10 + '0');
}
return *this;
}
OutParser& operator << (char ch) {
write_ch(ch);
return *this;
}
OutParser& operator << (const char *ch) {
while (*ch) {
write_ch(*ch);
++ch;
}
return *this;
}
};
/* This is an O(n) precalculation, O(1) query RMQ implementation.
* I learned about this method from Andrei-Costin Constantinescu */
template <typename T, T inf, typename Cmp>
class range_min_query{
Cmp c;
int n, k;
std::vector<unsigned long long> msk;
std::vector<T> buf;
T& get(int niv, int j){
return buf[n + (n / 64) * niv + j];
}
T small_query(int st, int dr){
return st >= dr ? inf
: buf[st + 63 - __builtin_clzll(msk[st] & ((unsigned long long)-1ll >> (64 - dr + st)))];
}
T large_query(int st, int dr){
if(st >= dr) return inf;
const int niv = 31 - __builtin_clz(dr - st);
return std::min<T, Cmp>(get(niv, st), get(niv, dr - (1 << niv)), c);
}
public:
range_min_query(): n(0), msk(), buf() {}
template <typename F>
range_min_query(int N, F f, Cmp C): c(C), n(N), k(31 - __builtin_clz(n / 64)), msk(n + 1, 0), buf(n + (k + 1) * n / 64 + 1){
for(int i = 0; i < n; ++i) buf[i] = f(i);
for(int i = n - 1; i >= 0; msk[i--] |= 1)
for(msk[i] = 2 * msk[i + 1]; msk[i] && !c(buf[i + __builtin_ctzll(msk[i])], buf[i]); )
msk[i] ^= (msk[i]&-msk[i]);
for(int i = 0; i + 64 <= n; i += 64) get(0, i / 64) = small_query(i, i + 64);
for(int niv = 1; niv <= k; ++niv)
for(int i = 0, j = 1 << (niv - 1); j <= n / 64; ++i, ++j)
get(niv, i) = std::min<T, Cmp>(get(niv - 1, i), get(niv - 1, j), c);
}
T query(int st, int dr){
return std::min<T, Cmp>({ small_query(st, std::min(st + 64, dr)),
small_query(std::max(dr - 64, st), dr),
large_query((st + 63) / 64, dr / 64) }, c);
}
};
int main(){
InParser f("rmq.in");
OutParser g("rmq.out");
int n, m;
f >> n >> m;
range_min_query<int, 1000000000, less<int>> rmq(n, [&](int){ int x; f >> x; return x; }, less<int>());
for(int x, y; m; --m){
f >> x >> y;
g << rmq.query(x - 1, y) << '\n';
}
}