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;
inline double _abs(double a) { return a > 0 ? a : -a; }
int n, q, R[60], B[60];
int pos;
char col[60];
struct matrix {
  int mod, mat[60][120], f[60];
  inline int sum(int a, int b) { return a + b < mod ? a + b : a + b - mod; }
  inline int dec(int a, int b) { return a < b ? a - b + mod : a - b; }
  inline int mul(int a, int b) { return (int)((long long int)a * b % mod); }
  inline int pow(int a, int b) {
    int res = 1;
    while (b > 0) {
      if (b & 1) res = mul(res, a);
      a = mul(a, a);
      b >>= 1;
    }
    return res;
  }
  inline int inv(int x) { return pow(x, mod - 2); }
  void gauss(void) {
    int half = inv(2);
    for (int i = 0; i < n; i++) {
      mat[B[i]][i] = sum(mat[B[i]][i], half);
      mat[R[i]][i] = sum(mat[R[i]][i], half);
      mat[i][i] = dec(mat[i][i], 1);
    }
    for (int i = 0; i < n; i++) mat[i][i + n] = 1;
    for (int i = 0; i < n; i++) {
      int maxp = i;
      for (int j = i + 1; j < n; j++)
        if (mat[j][i] > 0) maxp = j;
      for (int j = 0; j < n + n; j++) swap(mat[i][j], mat[maxp][j]);
      for (int j = 0; j < n; j++)
        if (i != j) {
          int t = mul(mat[j][i], inv(mat[i][i]));
          for (int k = 0; k < n + n; k++)
            mat[j][k] = dec(mat[j][k], mul(mat[i][k], t));
        }
    }
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        mat[i][j + n] = mul(mat[i][j + n], inv(mat[i][i]));
    return;
  }
  void getf(void) {
    for (int i = 0; i < n; i++) f[i] = 0;
    int half = inv(2);
    for (int i = 0; i < n; i++)
      if (col[i] == 'R') {
        f[R[i]] = sum(f[R[i]], half);
        f[B[i]] = dec(f[B[i]], half);
      }
    f[0] = sum(f[0], 1);
    f[pos] = dec(f[pos], 1);
    return;
  }
  int getout(int id) {
    int res = 0;
    for (int i = 0; i < n; i++) res = dec(res, mul(mat[id][i + n], f[i]));
    return res;
  }
} m1, m2;
struct edges {
  int to;
  long long int cnt;
} e1[60], e2[60];
bool incyc[60], vis[60];
int sta[60], top = -1;
inline long long int query(int id) {
  return e1[id].cnt == 0 ? e2[id].cnt : e1[id].cnt;
}
inline void dec(int id, long long int x) {
  if (e1[id].cnt >= x) {
    e1[id].cnt -= x;
    return;
  }
  e2[id].cnt -= x - e1[id].cnt;
  e1[id].cnt = 0;
  return;
}
bool check(void) {
  int now = 0;
  while (e1[now].cnt + e2[now].cnt > 0) {
    while (e1[now].cnt + e2[now].cnt > 0 && !vis[now]) {
      vis[now] = 1;
      sta[++top] = now;
      if (e1[now].cnt > 0)
        now = e1[now].to;
      else
        now = e2[now].to;
    }
    if (!vis[now]) break;
    while (sta[top] != now) {
      vis[sta[top]] = 0;
      incyc[sta[top--]] = 1;
    }
    vis[now] = 0;
    incyc[now] = 1;
    top--;
    long long int mn = 1e18;
    for (int i = 0; i < n; i++)
      if (mn > query(i) && incyc[i]) mn = query(i);
    for (int i = 0; i < n; i++)
      if (incyc[i]) {
        dec(i, mn);
        incyc[i] = 0;
      }
  }
  while (top >= 0) dec(sta[top--], 1);
  for (int i = 0; i < n; i++)
    if (e1[i].cnt + e2[i].cnt != 0) return 0;
  return 1;
}
long long int out[60];
long long int getans(void) {
  m1.getf();
  m2.getf();
  for (int i = 0; i < n; i++) {
    int p1 = m1.getout(i), p2 = m2.getout(i);
    long long int k1 = m1.mul(p1, m1.inv(m2.mod));
    long long int k2 = m2.mul(p2, m2.inv(m1.mod));
    k1 *= m2.mod;
    k2 *= m1.mod;
    out[i] = (k1 + k2) % ((long long int)m1.mod * m2.mod);
    if ((out[i] & 1) && col[i] == 'B') return -1;
    if (!(out[i] & 1) && col[i] == 'R') return -1;
  }
  for (int i = 0; i < n; i++) {
    incyc[i] = vis[i] = 0;
    if (out[i] & 1ll) {
      e1[i].cnt = out[i] >> 1ll;
      e1[i].to = B[i];
      e2[i].cnt = (out[i] + 1) >> 1ll;
      e2[i].to = R[i];
    } else {
      e1[i].cnt = e2[i].cnt = out[i] >> 1ll;
      e1[i].to = R[i];
      e2[i].to = B[i];
    }
  }
  if (!check()) return -1;
  long long int ans = 0;
  for (int i = 0; i < n; i++) ans += out[i];
  return ans;
}
int main(void) {
  scanf("%d", &n);
  n--;
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &B[i], &R[i]);
    B[i]--;
    R[i]--;
  }
  m1.mod = 1000000007;
  m2.mod = 1000000009;
  m1.gauss();
  m2.gauss();
  scanf("%d", &q);
  while (q--) {
    scanf("%d", &pos);
    pos--;
    for (int i = 0; i < n; i++) scanf(" %c", &col[i]);
    printf("%lld\n", getans());
  }
  return 0;
}
