#include <vector>
#include <iostream>
#include <stack>
using namespace std;

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n, m;
    cin >> n >> m;

    vector<int> t(2 * n, -1);

    for(int i = 0; i < n; i++)
        cin >> t[n + i];

    for(int i = n - 1; i > 0; i--)
        t[i] = t[i * 2] & t[i * 2 + 1];

    int x, y, res = -1;

    for(int i = 0; i < m; i++){
        cin >> x >> y;

        x = n + x - 1;
        y = n + y - 1;

        while(x < y){

            if(x % 2 == 1){
                if(res == -1)res = t[x];
                else res = res & t[x];
                x++;
            }

            if(y % 2 == 0){
                if(res == -1)res = t[y];
                else res = res & t[y];
                y--;
            }

            x /= 2;
            y /= 2;

        }

        if(x == y)
            res = res & t[x];

        cout << res << endl;

        res = -1;
    }

    return 0;
}