#include <iostream>
#include <vector>
#define show(x) cerr << #x << " = " << x << endl
using namespace std;
struct Graph {
    Graph(const int n)
    {
        edge.resize(n);
    }
    void addEdge(const int from, const int to)
    {
        edge[from].push_back(to);
        edge[to].push_back(from);
    }
    vector<vector<int>> edge;
};


inline int calc(const int v, const int p)
{
    constexpr int WEIGHT[6][3] = {{1, 2, 4}, {1, 4, 2}, {2, 1, 4}, {2, 4, 1}, {4, 1, 2}, {4, 2, 1}};
    int ans = 0;
    for (int i = 0; i < 3; i++) {
        if (v & (1 << i)) {
            ans += WEIGHT[p][i];
        }
    }
    return ans;
}

inline int next(const int label, const int mask)
{
    switch (label) {
    case 0:
        return mask & 0b101;
    case 1:
        return mask & 0b110;
    case 2:
        return mask | 0b101;
    case 3:
        return mask | 0b110;
    case 4:
        return mask ^ 0b101;
    case 5:
        return mask ^ 0b110;
    default:
        return 0;
    }
}

void dfs(const Graph& g, const int s, const int d, const int mask, const vector<int>& label, vector<bool>& used, vector<vector<int>>& dp)
{
    used[s] = true;
    const int m = next(label[s], mask);
    vector<int> ch;
    for (const int to : g.edge[s]) {
        if (not used[to]) {
            ch.push_back(to);
            dfs(g, to, d + 1, m, label, used, dp);
        }
    }
    if (ch.empty()) {
        for (int i = 0; i < 6; i++) {
            dp[s][i] = m;
        }
    } else {
        for (int i = 0; i < 6; i++) {
            if (d % 2 == 0) {
                int maxi = -1;
                int maxind = 0;
                for (const int to : ch) {
                    const int x = dp[to][i];
                    const int v = calc(x, i);
                    if (maxi < v) {
                        maxi = v;
                        maxind = x;
                    }
                }
                dp[s][i] = maxind;
            } else {
                int mini = 8;
                int minind = 0;
                for (const int to : ch) {
                    const int x = dp[to][i];
                    const int v = calc(x, i);
                    if (mini > v) {
                        mini = v;
                        minind = x;
                    }
                }
                dp[s][i] = minind;
            }
        }
    }
}

inline int order(const int v[3])
{
    if (v[0] < v[1]) {
        if (v[1] < v[2]) {
            return 0;
        } else if (v[0] < v[2]) {
            return 1;
        } else {
            return 3;
        }
    } else {
        if (v[1] > v[2]) {
            return 5;
        } else if (v[0] < v[2]) {
            return 2;
        } else {
            return 4;
        }
    }
}

int main()
{
    const string S[] = {"T=T&X", "T=T&Y", "T=T|X", "T=T|Y", "T=T^X", "T=T^Y"};
    int N, M;
    cin >> N >> M;
    vector<int> label(N, 6);
    for (int i = 1; i < N; i++) {
        string s;
        cin >> s;
        for (int j = 0; j < 6; j++) {
            if (S[j] == s) {
                label[i] = j;
                break;
            }
        }
    }
    Graph g(N);
    for (int i = 0; i < N - 1; i++) {
        int u, v;
        cin >> u >> v;
        g.addEdge(u, v);
    }
    vector<bool> used(N, false);
    vector<vector<int>> dp(N, vector<int>(6, 0));
    dfs(g, 0, 0, 0, label, used, dp);
    for (int i = 0; i < M; i++) {
        int X, Y;
        cin >> X >> Y;
        int value[3] = {0, 0, 0};
        for (int i = 0; i < 16; i++) {
            const int num = ((X >> i) & 1) + (((Y >> i) & 1) << 1) - 1;
            if (num >= 0) {
                value[num] += 1 << i;
            }
        }
        const int ord = order(value);
        const int mask = dp[0][ord];
        int ans = 0;
        for (int i = 0; i < 3; i++) {
            if (mask & (1 << i)) {
                ans += value[i];
            }
        }
        cout << ans << endl;
    }
    return 0;
}



Measured memory_bytes: 48596