#include "bits/stdc++.h"

using namespace std;

using i64 = long long;

const i64 MOD = i64(1e9) + 7;
const i64 INF = i64(1e18) + 7;


template <typename T>
bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

template <typename T>
bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}

vector<vector<int>> edges;
vector<vector<int>> childs;
vector<int> values;
vector<int> perm;
vector<int> ret;
vector<int> depth;
vector<int> tps;


int g(){
    for(auto x : tps){
        bool maximize = (depth[x] % 2 == 0);
        int rettt = -1;
        for(auto y : childs[x]){
            int ret_idx = ret[y];
            if(rettt == -1)
                rettt = ret_idx;
            else{
                if(maximize && perm[rettt] <= perm[ret_idx])
                    rettt = ret_idx;
                if(!maximize && perm[rettt] >= perm[ret_idx])
                    rettt = ret_idx;
            }
        }
        if(rettt == -1)
            ret[x] = values[x];
        else
            ret[x] = rettt;
    }
    return ret[0];
}

bool solve(){
    int n, m;
    cin >> n >> m;
    vector<string> query(n);
    for(int i = 1; i < n; ++i){
        string s;
        cin >> s;
        query[i] = s.substr(3);
    }
    edges.resize(n);
    childs.resize(n);
    ret.resize(n);
    depth.resize(n, 0);
    for(int i = 0; i < n - 1; ++i){
        int u, v;
        scanf("%d%d", &u, &v);
        edges[u].push_back(v);
        edges[v].push_back(u);
    }
    int xx = 0b110;
    int yy = 0b011;
    map<int,int> res_map;
    vector<int> res(8);
    iota(res.begin(), res.end(), 0);
    for(int j = 0; j < 8; ++j)
        res_map[res[j]] = j;

    bitset<100007> visited;
    values.assign(n, 0);
    function<void(int)> f = [&](int x){
        tps.push_back(x);
        for(auto y : edges[x]){
            if(visited[y])
                continue;
            depth[y] = depth[x] + 1;
            childs[x].push_back(y);
            int val = values[x];
            if(query[y] == "&X")
                val = val & xx;
            if(query[y] == "&Y")
                val = val & yy;
            if(query[y] == "|X")
                val = val | xx;
            if(query[y] == "|Y")
                val = val | yy;
            if(query[y] == "^X")
                val = val ^ xx;
            if(query[y] == "^Y")
                val = val ^ yy;
            values[y] = val;
            visited.set(y);
            f(y);
        }
    };
    visited.set(0);
    f(0);

    reverse(tps.begin(), tps.end());

    for(int i = 0; i < n; ++i)
        values[i] = res_map[values[i]];

    map<vector<int>, int> perm_res;

    for(int i = 0; i < m; ++i){
        vector<pair<int,int>> v;
        int x, y;
        scanf("%d%d", &x, &y);
        vector<int> b{~x & y, x & y, x & ~y};
        for(int j = 0; j < 8; ++j){
            int sum = 0;
            for(int k = 0; k < 3; ++k)
                if(j & (1 << k))
                    sum += b[k];
            v.emplace_back(sum, j);
        }
        sort(v.begin(), v.end());
        vector<int> flag(8, 0);
        for(int j = 0; j < 8; ++j){
            flag[v[j].second] = j;
        }

        if(perm_res.find(flag) == perm_res.end()){
            perm = flag;
            perm_res[flag] = g();
        }

        int an = perm_res[flag];
        int ans = 0;
        for(int k = 0; k < 3; ++k)
            if(an & (1 << k))
                ans += b[k];
        printf("%d\n", ans);
    }

    return false;
}

signed main(){
    while(solve());
}



Measured memory_bytes: 27164