Problem: problem

Given the formula $ S $ of length $ N $. The formula is in the format shown in BNF below.


<expr> :: = <number> | <expr> <op> <expr>

<op> :: = ‘^’ | ‘&’ | ‘|’



<number> represents an integer greater than or equal to $ 0 $ and less than or equal to $ 2 ^ {31} -1 $.

The operators ‘^’ ‘&’ ‘|’ represent exclusive OR, AND, and OR, respectively. The precedence of operators is as follows.

High ‘^’> ‘&’> ‘|’ Low

$ Q $ intervals $ [i, j] $ are given. Output the calculation result of $ S_i, \ dots, S_j $.

It is guaranteed that the formulas $ S_i, \ dots, S_j $ are in the format shown by the above BNF. Also, the formula does not contain zero-padded values.



output

Output the calculation result of $ S_i, \ dots, S_j $. Also, output a line break at the end.

Example

Input

7
9^2&1|2
4
0 6
0 2
2 6
4 4


Output

3
11
2
1

Solution (in CPP):

Solution:
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
#define def 0
template<class V, int NV> struct SegTreeXor { //[l,r)
    V comp(V& l, V& r) { return l ^ r; };

    vector<V> val; SegTreeXor() { val = vector<V>(NV * 2, def); }
    V get(int x, int y, int l = 0, int r = NV, int k = 1) {
        if (r <= x || y <= l)return def; if (x <= l && r <= y)return val[k];
        auto a = get(x, y, l, (l + r) / 2, k * 2); 
        auto b = get(x, y, (l + r) / 2, r, k * 2 + 1);
        return comp(a, b);
    }
    void update(int i, V v) {
        i += NV; val[i] = v;
        while (i>1) i >>= 1, val[i] = comp(val[i * 2], val[i * 2 + 1]);
    }
    void add(int i, V v) { update(i, val[i + NV] + v); }
    V operator[](int x) { return get(x, x + 1); }
};
template<class V, int NV> struct SegTreeAnd { //[l,r)
    V comp(V& l, V& r) { return l & r; };

    vector<V> val; SegTreeAnd() { val = vector<V>(NV * 2, def); }
    V get(int x, int y, int l = 0, int r = NV, int k = 1) {
        if (r <= x || y <= l) return INT_MAX; if (x <= l && r <= y)return val[k];
        auto a = get(x, y, l, (l + r) / 2, k * 2);
        auto b = get(x, y, (l + r) / 2, r, k * 2 + 1);
        return comp(a, b);
    }
    void update(int i, V v) {
        i += NV; val[i] = v;
        while (i>1) i >>= 1, val[i] = comp(val[i * 2], val[i * 2 + 1]);
    }
    void add(int i, V v) { update(i, val[i + NV] + v); }
    V operator[](int x) { return get(x, x + 1); }
};
template<class V, int NV> struct SegTreeOr { //[l,r)
    V comp(V& l, V& r) { return l | r; };

    vector<V> val; SegTreeOr() { val = vector<V>(NV * 2, def); }
    V get(int x, int y, int l = 0, int r = NV, int k = 1) {
        if (r <= x || y <= l)return def; if (x <= l && r <= y)return val[k];
        auto a = get(x, y, l, (l + r) / 2, k * 2);
        auto b = get(x, y, (l + r) / 2, r, k * 2 + 1);
        return comp(a, b);
    }
    void update(int i, V v) {
        i += NV; val[i] = v;
        while (i>1) i >>= 1, val[i] = comp(val[i * 2], val[i * 2 + 1]);
    }
    void add(int i, V v) { update(i, val[i + NV] + v); }
    V operator[](int x) { return get(x, x + 1); }
};
/*---------------------------------------------------------------------------------------------------
　　　　　　　　　　　 ∧＿∧  
　　　　　 ∧＿∧ 　（´<_｀ ）　 Welcome to My Coding Space!
　　　　 （ ´_ゝ`）　/　 ⌒i     
　　　　／　　　＼　 　 |　|     
　　　 /　　 /￣￣￣￣/　　|  
　 ＿_(__ﾆつ/　    ＿/ .| .|＿＿＿＿  
　 　　　＼/＿＿＿＿/　（u　⊃  
---------------------------------------------------------------------------------------------------*/




int N; string S; int Q;
SegTreeXor<int, 1 << 17> stxor;
SegTreeAnd<int, 1 << 17> stand;
SegTreeOr<int, 1 << 17> stor;
int get_or_id[101010];
int get_and_id[101010];
int get_xor_id[101010];
int get_number_id[101010];
int start_xor[101010], end_xor[101010];
int start_and[101010], end_and[101010];
//---------------------------------------------------------------------------------------------------
pair<int,int> dfs_number(int L, int R) {
    static int number_id = 0;
    number_id++;

    int res = 0;
    rep(i, L, R + 1) {
        res = res * 10 + S[i] - '0';
        get_number_id[i] = number_id;
    }

    return { res, number_id };
}
pair<int,int> dfs_xor(int L, int R) {
    static int xor_id = 0;
    xor_id++;

    int st = L, res = 0;
    int isfirst = 1;
    rep(i, L, R + 1) if(S[i] == '^') {
        auto p = dfs_number(st, i - 1);
        st = i + 1;

        stxor.update(p.second, p.first);
        res ^= p.first;
        get_xor_id[p.second] = xor_id;

        if (isfirst) {
            isfirst = 0;
            start_xor[xor_id] = p.second;
        }
    }

    auto p = dfs_number(st, R);

    stxor.update(p.second, p.first);
    res ^= p.first;
    get_xor_id[p.second] = xor_id;
    end_xor[xor_id] = p.second;
    if (isfirst) start_xor[xor_id] = p.second;

    return { res, xor_id };
}
pair<int,int> dfs_and(int L, int R) {
    static int and_id = 0;
    and_id++;

    int st = L, res = -1;
    int isfirst = 1;
    rep(i, L, R + 1) if (S[i] == '&') {
        auto p = dfs_xor(st, i - 1);
        st = i + 1;

        stand.update(p.second, p.first);

        if (res < 0) res = p.first;
        else res &= p.first;
        get_and_id[p.second] = and_id;

        if (isfirst) {
            isfirst = 0;
            start_and[and_id] = p.second;
        }
    }

    auto p = dfs_xor(st, R);

    stand.update(p.second, p.first);
    if (res < 0) res = p.first;
    else res &= p.first;
    get_and_id[p.second] = and_id;
    end_and[and_id] = p.second;
    if (isfirst) start_and[and_id] = p.second;

    return { res, and_id };
}
pair<int,int> dfs_or(int L, int R) {
    static int or_id = 0;
    or_id++;

    int st = L, res = 0;
    rep(i, L, R + 1) if (S[i] == '|') {
        auto p = dfs_and(st, i - 1);
        st = i + 1;

        stor.update(p.second, p.first);

        res |= p.first;
        get_or_id[p.second] = or_id;
    }

    auto p = dfs_and(st, R);

    stor.update(p.second, p.first);

    res |= p.first;
    get_or_id[p.second] = or_id;

    return { res, or_id };
}
void pre() {
    dfs_or(0, N - 1);
}
//---------------------------------------------------------------------------------------------------
int getnumber(int i, int j) {
    int res = 0;
    while ('0' <= S[i] and S[i] <= '9' and i <= j) res = res * 10 + S[i] - '0', i++;
    return res;
}
int getnumber_back(int i) {
    int res = 0;
    int ten = 1;
    while (0 <= i and '0' <= S[i] and S[i] <= '9') res = res + (S[i] - '0') * ten, ten *= 10, i--;
    return res;
}
int query(int i, int j) {
    int L = getnumber(i, N - 1);
    int R = getnumber_back(j);

    int i_num = get_number_id[i];
    int j_num = get_number_id[j];
    if (i_num == j_num) return getnumber(i, j);

    int i_xor = get_xor_id[i_num];
    int j_xor = get_xor_id[j_num];
    if (i_xor == j_xor) return L ^ stxor.get(i_num + 1, j_num) ^ R;

    L = L ^ stxor.get(i_num + 1, end_xor[i_xor] + 1);
    R = R ^ stxor.get(start_xor[j_xor], j_num);

    int i_and = get_and_id[i_xor];
    int j_and = get_and_id[j_xor];
    if (i_and == j_and) return L & stand.get(i_xor + 1, j_xor) & R;

    L = L & stand.get(i_xor + 1, end_and[i_and] + 1);
    R = R & stand.get(start_and[j_and], j_xor);

    int i_or = get_or_id[i_and];
    int j_or = get_or_id[j_and];

    assert(i_or == j_or);

    return L | stor.get(i_and + 1, j_and) | R;
}
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N >> S >> Q;
    
    pre();

    rep(_, 0, Q) {
        int i, j; cin >> i >> j;
        printf("%d\n", query(i, j));
    }
}