Problem: H --Bit Operation Game

Given a rooted tree with N vertices. The vertices are numbered from 0 to N − 1, and the 0th vertex represents the root. `T = 0` for the root, but for the other vertices

* `T = T & X;`
* `T = T & Y;`
* `T = T | X`
* `T = T | Y`
* `T = T ^ X`
* `T = T ^ Y`



One of the operations is written. Here, the operators &, |, ^ mean the bitwise operators and, or, xor, respectively.

Mr. A and Mr. B played the following games M times using this tree. The two start from the root and select the child vertices and proceed alternately, starting from Mr. A and reaching the leaves. The final T value when the operations written in the passed nodes are applied in the order of passing is the score. Mr. B wants to make the score as small as possible, and Mr. A wants to make it large. Given the X and Y values ​​of the M games, output the score for each game when the two make the best choice.

Constraints

* 1 ≤ N ≤ 100000
* 1 ≤ M ≤ 100000
* 0 ≤ X, Y <2 ^ {16}



Input Format

Input is given from standard input in the following format.


N M
o_1_1
o_2
...
o_ {N−1}
u_1 v_1
u_2 v_2
...
u_ {N−1} v_ {N−1}
X_1 Y_1
X_2 Y_2
...
X_M Y_M


On the first line, the number of vertices N of the tree and the integer M representing the number of games played are entered.
From the 2nd line to the Nth line, the operation written at the 1st ... N-1st vertex is input.
In addition, the numbers of the two vertices connected by each side are entered in the N-1 line.
Finally, the values ​​of X and Y in M ​​games are entered over M lines.


Output Format

Print the final T value for each game on the M line.

Sample Input 1


6 3
T = T | X
T = T | Y
T = T | Y
T = T ^ Y
T = T & X;
0 1
0 2
13
14
twenty five
5 6
3 5
0 0


Sample Output 1


Four
6
0


<image>
For X = 5, Y = 6, proceed to vertices 0-> 2-> 5, and T = 5 & 6 = 4.
If X = 3, Y = 5, then go to vertices 0-> 1-> 4, and T = 3 ^ 5 = 6.
If X = 0, Y = 0, T does not change from 0 no matter where you go





Example

Input

6 3
T=T|X
T=T|Y
T=T|Y
T=T^Y
T=T&X
0 1
0 2
1 3
1 4
2 5
5 6
3 5
0 0


Output

4
6
0

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

const int N = 100010;
char op[N],var[N];

vector<int> G[N];

int X,Y;
int dfs(int v, int p, int dep, int val){
    int ret = 0;
    if(dep%2==1) ret = N;

    int ch = 0;
    for(int e:G[v])if(e!=p){
        ++ch;
        int xy = 0;
        if(var[e]=='X') xy=X;
        else xy=Y;

        int nv = val;
        if(op[e]=='&') nv &= xy;
        else if(op[e]=='|') nv |= xy;
        else nv ^= xy;

        if(dep%2==1) ret = min(ret, dfs(e,v,dep+1,nv));
        else ret = max(ret, dfs(e,v,dep+1,nv));
    }

    // printf(" %d :: %d, %d\n", v, ret,val);
    if(ch==0) return val;
    else return ret;
}

int main(){
    int n,m;
    cin >>n >>m;

    for(int i=1; i<n; ++i){
        string s;
        cin >>s;
        op[i] = s[3];
        var[i] = s[4];
    }

    rep(i,n-1){
        int u,v;
        cin >>u >>v;
        G[u].pb(v);
        G[v].pb(u);
    }

    vector<int> x(m),y(m),ans(m);
    map<vector<int>, vector<int>> q;
    rep(i,m){
        cin >>x[i] >>y[i];

        int ct[4]={};
        vector<int> vv;
        for(int j=15; j>=0; --j){
            int bx = x[i]>>j&1, by = y[i]>>j&1;
            int bb = bx*2+by;
            if(ct[bb]==0){
                vv.pb(bb);
                ++ct[bb];
            }
        }

        rep(j,4){
            if(ct[j]==0) vv.pb(j);
        }

        q[vv].pb(i);
    }

    vector<int> p(4);
    iota(all(p),0);

    do{
        X = 0;
        Y = 0;
        rep(i,4){
            if(p[i]>=2) X = 2*X + 1;
            else X = 2*X;
            if(p[i]%2==1) Y = 2*Y + 1;
            else Y = 2*Y;
        }

        int res = dfs(0,-1,0,0);

        int a[4]={};
        rep(i,4){
            int b = (res>>(3-i))&1;
            a[p[i]] = b;
        }

        for(int idx:q[p]){
            for(int j=15; j>=0; --j){
                int bx = x[idx]>>j&1, by = y[idx]>>j&1;
                int bb = bx*2+by;

                ans[idx] <<= 1;
                ans[idx] += a[bb];
            }
        }
    }while(next_permutation(all(p)));

    rep(i,m) cout << ans[i] << endl;
    return 0;
}