#include <bits/stdc++.h>
using namespace std;
const unsigned long long MOD = 18446744065119617029ULL;
unsigned long long add(unsigned long long a, unsigned long long b) {
  if (a < MOD - b)
    return a + b;
  else
    return a - (MOD - b);
}
unsigned long long sub(unsigned long long a, unsigned long long b) {
  return add(a, MOD - b);
}
unsigned long long neg(unsigned long long a) {
  if (a == 0)
    return 0;
  else
    return MOD - a;
}
unsigned long long mul(unsigned long long a, unsigned long long b) {
  unsigned long long res = 0;
  while (b != 0) {
    if (b & 1) res = add(res, a);
    b >>= 1;
    a = add(a, a);
  }
  return res;
}
unsigned long long inv(unsigned long long a) {
  unsigned long long e = MOD - 2, res = 1;
  while (e != 0) {
    if (e & 1) res = mul(res, a);
    e >>= 1;
    a = mul(a, a);
  }
  return res;
}
const int MAXN = 58;
int n;
unsigned long long A[MAXN][MAXN], B[MAXN][MAXN], C[MAXN][MAXN];
unsigned long long U[MAXN], V[MAXN], TMP[MAXN];
void swap_rows(int i, int j, unsigned long long M[MAXN][MAXN]) {
  for (int k = 0; k < n; k++) swap(M[i][k], M[j][k]);
}
void mul_row(int i, unsigned long long t, unsigned long long M[MAXN][MAXN]) {
  for (int j = 0; j < n; j++) M[i][j] = mul(M[i][j], t);
}
void add_rows(int i, int j, unsigned long long t,
              unsigned long long M[MAXN][MAXN]) {
  for (int k = 0; k < n; k++) M[i][k] = add(M[i][k], mul(M[j][k], t));
}
void mul_mat_vec(unsigned long long M[MAXN][MAXN], unsigned long long X[MAXN],
                 unsigned long long Y[MAXN], bool with_mod = true) {
  for (int i = 0; i < n; i++) {
    Y[i] = 0;
    for (int j = 0; j < n; j++)
      if (with_mod)
        Y[i] = add(Y[i], mul(M[i][j], X[j]));
      else
        Y[i] += M[i][j] * X[j];
  }
}
int r[MAXN], b[MAXN];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  n--;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      A[i][j] = 0;
      B[i][j] = (i == j);
    }
  for (int i = 0; i < n; i++) {
    cin >> b[i] >> r[i];
    b[i]--;
    r[i]--;
    A[i][i] = add(A[i][i], neg(2));
    A[r[i]][i] = add(A[r[i]][i], 1);
    A[b[i]][i] = add(A[b[i]][i], 1);
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      C[i][j] = A[i][j];
      if (C[i][j] > 200) C[i][j] -= MOD;
    }
  for (int i = 0; i < n; i++) {
    int j = i;
    while (A[i][j] == 0 && j < n) j++;
    assert(j < n);
    swap_rows(i, j, A);
    swap_rows(i, j, B);
    unsigned long long t = inv(A[i][i]);
    mul_row(i, t, A);
    mul_row(i, t, B);
    for (int j = 0; j < n; j++) {
      if (j == i) continue;
      unsigned long long t = A[j][i];
      add_rows(j, i, neg(t), A);
      add_rows(j, i, neg(t), B);
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int v;
    string s;
    cin >> v >> s;
    v--;
    for (int i = 0; i < n; i++) U[i] = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] == 'R') {
        U[i] = add(U[i], 1);
        U[r[i]] = sub(U[r[i]], 1);
      }
      if (i == v) U[i] = add(U[i], 1);
      if (i == 0) U[i] = sub(U[i], 1);
    }
    mul_mat_vec(B, U, V);
    mul_mat_vec(C, V, TMP, false);
    bool mul_ok = true;
    for (int i = 0; i < n; i++) {
      unsigned long long u_without_mod = U[i];
      if (u_without_mod > 100) u_without_mod -= MOD;
      mul_ok &= (u_without_mod == TMP[i]);
    }
    bool vis_ok = true;
    for (int i = 0; i < n; i++) {
      if (V[i] == 0 && s[i] == 'B') continue;
      queue<int> Q({i});
      vector<bool> vis(n, false);
      while (!Q.empty()) {
        int i = Q.front();
        Q.pop();
        if (vis[i]) continue;
        vis[i] = true;
        if (s[i] == 'R')
          Q.push(r[i]);
        else
          Q.push(b[i]);
      }
      vis_ok &= vis[v];
    }
    unsigned long long sol = 0;
    for (int i = 0; i < n; i++) {
      sol = add(sol, add(V[i], V[i]));
      if (s[i] == 'R') sol = add(sol, 1);
    }
    if (mul_ok && vis_ok)
      cout << sol << "\n";
    else
      cout << "-1\n";
  }
  return 0;
}
