#include <bits/stdc++.h>

using namespace std;
int a[200007];
int s[507];
int ur;
void sqrtdecomp(int n) {
    int u = round(n / sqrt(n));
    int sol = a[0];
    for (int i = 0; i < n; i++) {
        sol = sol & a[i];
        if ((i+1) % u == 0 || i == n-1) {
             s[i/u] = sol;
             if (i+1 < n) sol = a[i+1];
        }
    }
    ur = u;
}

int getvalue(int l, int r) {

    int sol = -1;
    if (l/ur == r/ur) {
        sol = a[l];
        for (int i = l; i <= r; i++) {
            sol = sol & a[i];
        }
        return sol;
    }
    for (int i = l; i < (l/ur + 1)*ur; i++) {
        if (sol == -1) sol = a[i];
        else sol = sol & a[i];
    }
    for (int i = (l/ur)+1; i < r/ur; i++) {
        if (sol == -1) sol = s[i];
        else sol = sol & s[i];
    }
    for (int i = r-(r%ur); i <= r; i++) {
        if (sol == -1) sol = a[i];
        else sol = sol & a[i];
    }
    return sol;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, q;
    cin >> n >> q;
    for (int i = 0; i < n; i++)
        cin >> a[i];
    sqrtdecomp(n);
    int l, r;
    while (q--) {
        cin >> l >> r;
        cout << getvalue(l-1, r-1) << "\n";
    }
    return 0;
}