There is a directed graph on n vertices numbered 1 through n where each vertex (except n) has two outgoing arcs, red and blue. At any point in time, exactly one of the arcs is active for each vertex. Initially, all blue arcs are active and there is a token located at vertex 1. In one second, the vertex with token first switches its active arcs — the inactive arc becomes active and vice versa. Then, the token is moved along the active arc. When the token reaches the vertex n, it stops. It is guaranteed that n is reachable via arcs from every vertex.

You are given q queries. Each query contains a state of the graph — a pair (v, s) of the following form: 

  * v is the vertex where the token is currently located; 
  * s is a string consisting of n - 1 characters. The i-th character corresponds to the color of the active edge leading from the i-th vertex (the character is 'R' if red arc is active, otherwise the character is 'B'). 



For each query, determine whether the given state is reachable from the initial state and the first time this configuration appears. Note that the two operations (change active arc and traverse it) are atomic — a state is not considered reached if it appears after changing the active arc but before traversing it.

Input

The first line contains a single integer n (2 ≤ n ≤ 58) — the number of vertices.

n-1 lines follow, i-th of contains two space separated integers b_i and r_i (1 ≤ b_i, r_i ≤ n) representing a blue arc (i, b_i) and red arc (i, r_i), respectively. It is guaranteed that vertex n is reachable from every vertex.

The next line contains a single integer q (1 ≤ q ≤ 5000) — the number of queries.

Then q lines with queries follow. The j-th of these lines contains an integer v (1 ≤ v < n) and a string s of length n-1 consiting only of characters 'R' and 'B'. The i-th of these characters is 'R' if the red arc going from i is active and 'B' otherwise.

Output

Output q lines, each containing answer to a single query.

If the state in the i-th query is unreachable, output the integer -1. Otherwise, output t_i — the first time when the state appears (measured in seconds, starting from the initial state of the graph which appears in time 0).

Example

Input


6
2 1
5 5
2 1
6 3
4 3
21
1 BBBBB
1 RBBBB
2 BBBBB
5 BRBBB
3 BRBBR
1 BRRBR
1 RRRBR
2 BRRBR
5 BBRBR
4 BBRBB
3 BBRRB
2 BBBRB
5 BRBRB
3 BRBRR
1 BRRRR
1 RRRRR
2 BRRRR
5 BBRRR
4 BBRRB
2 BRBBB
4 BRBBR


Output


0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
-1
-1

Note

The graph in the first example is depticed in the figure below.

<image>

The first 19 queries denote the journey of the token. On the 19-th move the token would reach the vertex 6. The last two queries show states that are unreachable.
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 60;
const long long MOD[2] = {1000000000000000009ll, 1000000000000000031ll};
int n, q, v;
int B[MAXN];
int R[MAXN];
long long ans;
long long b[MAXN];
long long x[2][MAXN];
long long a[2][MAXN][MAXN];
long long inv[2][MAXN][MAXN];
char s[MAXN];
bool vis[MAXN];
vector<pair<int, int> > swp;
long long mul(long long a, long long b, long long p) {
  long long v = 1;
  if (b < 0) {
    b = -b;
    v = -1;
  }
  long long res = 0;
  while (b) {
    if (b & 1) (res += a) %= p;
    (a += a) %= p;
    b >>= 1;
  }
  return res * v;
}
long long power(long long a, long long b, long long p) {
  long long res = 1;
  while (b) {
    if (b & 1) res = mul(res, a, p);
    a = mul(a, a, p);
    b >>= 1;
  }
  return res;
}
void Gauss(int id) {
  for (int i = 1; i < n; i++) {
    if (!a[id][i][i]) {
      for (int j = i + 1; j < n; j++)
        if (a[j][i]) {
          swap(a[id][i], a[id][j]);
          swap(inv[id][i], inv[id][j]);
          swp.push_back(make_pair(i, j));
          break;
        }
    }
    long long Inv = power(a[id][i][i], MOD[id] - 2, MOD[id]);
    for (int j = i + 1; j < n; j++)
      if (a[id][j][i]) {
        long long v = mul(a[id][j][i], Inv, MOD[id]);
        for (int k = 1; k < n; k++) {
          (a[id][j][k] -= mul(a[id][i][k], v, MOD[id])) %= MOD[id];
          (inv[id][j][k] -= mul(inv[id][i][k], v, MOD[id])) %= MOD[id];
        }
      }
  }
  for (int i = n - 1; i >= 1; i--) {
    long long Inv = power(a[id][i][i], MOD[id] - 2, MOD[id]);
    for (int j = i - 1; j >= 1; j--)
      if (a[id][j][i]) {
        long long v = mul(a[id][j][i], Inv, MOD[id]);
        for (int k = 1; k < n; k++) {
          (a[id][j][k] -= mul(a[id][i][k], v, MOD[id])) %= MOD[id];
          (inv[id][j][k] -= mul(inv[id][i][k], v, MOD[id])) %= MOD[id];
        }
      }
  }
  for (int i = 1; i < n; i++)
    for (int j = 1; j < n; j++)
      inv[id][i][j] =
          mul(inv[id][i][j], power(a[id][i][i], MOD[id] - 2, MOD[id]), MOD[id]);
  while (swp.size()) {
    swap(inv[id][swp.back().first], inv[id][swp.back().second]);
    swp.pop_back();
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &B[i], &R[i]);
    a[0][i][i] = 2;
    if (B[i] != n) a[0][B[i]][i]--;
    if (R[i] != n) a[0][R[i]][i]--;
  }
  memcpy(a[1], a[0], sizeof(a[1]));
  for (int i = 1; i < n; i++) inv[0][i][i] = inv[1][i][i] = 1;
  Gauss(0);
  Gauss(1);
  scanf("%d", &q);
  while (q--) {
    scanf("%d%s", &v, s + 1);
    memset(b, 0, sizeof(b));
    memset(x, 0, sizeof(x));
    b[1]++;
    b[v]--;
    for (int i = 1; i < n; i++)
      if (s[i] == 'R') {
        b[i]++;
        b[B[i]]--;
      }
    for (int k = 0; k <= 1; k++)
      for (int i = 1; i < n; i++)
        for (int j = 1; j < n; j++)
          (x[k][i] += mul(inv[k][i][j], b[j], MOD[k])) %= MOD[k];
    bool ok = 1;
    ans = 0;
    for (int i = 1; i < n; i++) {
      (x[0][i] += MOD[0]) %= MOD[0];
      (x[1][i] += MOD[1]) %= MOD[1];
      if (x[0][i] != x[1][i] || x[0][i] - (s[i] == 'R') < 0) {
        ok = 0;
        break;
      }
      long long it = x[0][i];
      ans += it + it - (s[i] == 'R');
      if (it + it - (s[i] == 'R') > 0) {
        int cur = i;
        memset(vis, 0, sizeof(vis));
        while (!vis[cur] && cur != v) {
          vis[cur] = 1;
          cur = (s[cur] == 'R' ? R[cur] : B[cur]);
        }
        if (cur != v) {
          ok = 0;
          break;
        }
      }
    }
    printf("%lld\n", ok ? ans : -1ll);
  }
  return 0;
}
