#include<bits/stdc++.h>

using namespace std;

using int64 = long long;

vector< pair< int, int > > beet(int x, int y) {
  vector< pair< int, int > > ret;
  ret.emplace_back(0, 0);
  ret.emplace_back(x, 1);
  ret.emplace_back(y, 2);
  ret.emplace_back(x & y, 3);
  ret.emplace_back(x | y, 4);
  ret.emplace_back(x ^ y, 5);
  ret.emplace_back(x ^ (x & y), 6);
  ret.emplace_back(y ^ (x & y), 7);
  return ret;
}


int N, M;
vector< int > g[100000];
map< vector< pair< int, bool > >, int > dp;
function< int(int, int, int) > fs[100000];

function< int(int, int, int) > getf(string s) {
  if(s == "T=T&X") return [](int t, int x, int y) { return t & x; };
  if(s == "T=T&Y") return [](int t, int x, int y) { return t & y; };
  if(s == "T=T|X") return [](int t, int x, int y) { return t | x; };
  if(s == "T=T|Y") return [](int t, int x, int y) { return t | y; };
  if(s == "T=T^X") return [](int t, int x, int y) { return t ^ x; };
  if(s == "T=T^Y") return [](int t, int x, int y) { return t ^ y; };
  throw 0;
}

int dfs(int idx, int par, int x, int y, int v, bool f) {
  bool leaf = true;
  for(auto &to : g[idx]) if(to != par) leaf = false;
  if(leaf) return v;
  if(f) {
    int ret = 1 << 30;
    for(auto &to : g[idx]) {
      if(to == par) continue;
      ret = min(ret, dfs(to, idx, x, y, fs[to](v, x, y), false));
    }
    return ret;
  } else {
    int ret = -1;
    for(auto &to : g[idx]) {
      if(to == par) continue;
      ret = max(ret, dfs(to, idx, x, y, fs[to](v, x, y), true));
    }
    return ret;
  }
}

int solve(int x, int y) {
  return dfs(0, -1, x, y, 0, false);
}

int main() {
  cin >> N >> M;
  for(int i = 1; i < N; i++) {
    string s;
    cin >> s;
    fs[i] = getf(s);
  }
  for(int i = 1; i < N; i++) {
    int x, y;
    cin >> x >> y;
    g[x].emplace_back(y);
    g[y].emplace_back(x);
  }
  while(M--) {
    int X, Y;
    cin >> X >> Y;
    auto uku = beet(X, Y);
    auto uku2 = uku;
    sort(begin(uku), end(uku));
    vector< pair< int, bool > > state;
    for(int i = 0; i < uku.size(); i++) {
      state.emplace_back(uku[i].second, i > 0 && uku[i - 1].first == uku[i].first);
    }
    if(!dp.count(state)) {
      int ret = solve(X, Y);
      for(int i = 0; i < uku.size(); i++) {
        if(ret == uku[i].first) {
          dp[state] = uku[i].second;
          break;
        }
      }
    }
    cout << uku2[dp[state]].first << endl;
  }
}

