Problem: Mishka's favourite experimental indie band has recently dropped a new album! Songs of that album share one gimmick. Each name s_i is one of the following types:

  * 1~c — a single lowercase Latin letter; 
  * 2~j~c — name s_j (1 ≤ j < i) with a single lowercase Latin letter appended to its end. 



Songs are numbered from 1 to n. It's guaranteed that the first song is always of type 1.

Vova is rather interested in the new album but he really doesn't have the time to listen to it entirely. Thus he asks Mishka some questions about it to determine if some song is worth listening to. Questions have the following format:

  * i~t — count the number of occurrences of string t in s_i (the name of the i-th song of the album) as a continuous substring, t consists only of lowercase Latin letters. 



Mishka doesn't question the purpose of that information, yet he struggles to provide it. Can you please help Mishka answer all Vova's questions?

Input

The first line contains a single integer n (1 ≤ n ≤ 4 ⋅ 10^5) — the number of songs in the album.

Each of the next n lines contains the desciption of the i-th song of the album in the following format:

  * 1~c — s_i is a single lowercase Latin letter; 
  * 2~j~c — s_i is the name s_j (1 ≤ j < i) with a single lowercase Latin letter appended to its end. 



The next line contains a single integer m (1 ≤ m ≤ 4 ⋅ 10^5) — the number of Vova's questions.

Each of the next m lines contains the desciption of the j-th Vova's question in the following format:

  * i~t (1 ≤ i ≤ n, 1 ≤ |t| ≤ 4 ⋅ 10^5) — count the number of occurrences of string t in s_i (the name of the i-th song of the album) as a continuous substring, t consists only of lowercase Latin letters. 



It's guaranteed that the total length of question strings t doesn't exceed 4 ⋅ 10^5.

Output

For each question print a single integer — the number of occurrences of the question string t in the name of the i-th song of the album as a continuous substring.

Example

Input


20
1 d
2 1 a
2 2 d
2 3 a
2 4 d
2 5 a
2 6 d
2 7 a
1 d
2 9 o
2 10 k
2 11 i
2 12 d
2 13 o
2 14 k
2 15 i
2 1 o
2 17 k
2 18 i
2 15 i
12
8 da
8 dada
8 ada
6 dada
3 dada
19 doki
19 ok
16 doki
15 doki
9 d
1 a
20 doki


Output


4
3
3
2
0
1
1
2
1
1
0
2

Note

Song names of the first example:

  1. d 
  2. da 
  3. dad 
  4. dada 
  5. dadad 
  6. dadada 
  7. dadadad 
  8. dadadada 
  9. d 
  10. do 
  11. dok 
  12. doki 
  13. dokid 
  14. dokido 
  15. dokidok 
  16. dokidoki 
  17. do 
  18. dok 
  19. doki 
  20. dokidoki 



Thus the occurrences for each question string are:

  1. string "da" starts in positions [1, 3, 5, 7] in the name "dadadada"; 
  2. string "dada" starts in positions [1, 3, 5] in the name "dadadada"; 
  3. string "ada" starts in positions [2, 4, 6] in the name "dadadada"; 
  4. string "dada" starts in positions [1, 3] in the name "dadada"; 
  5. no occurrences of string "dada" in the name "dad"; 
  6. string "doki" starts in position [1] in the name "doki"; 
  7. string "ok" starts in position [2] in the name "doki"; 
  8. string "doki" starts in positions [1, 5] in the name "dokidoki"; 
  9. string "doki" starts in position [1] in the name "dokidok"; 
  10. string "d" starts in position [1] in the name "d"; 
  11. no occurrences of string "a" in the name "d"; 
  12. string "doki" starts in positions [1, 5] in the name "dokidoki". 

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const long double eps = 1e-10;
const long double pi = acos(-1);
inline int dcmp(long double x) {
  if (x < -eps) return -1;
  if (x > eps) return 1;
  return 0;
}
const int maxn = 4e5 + 5;
struct {
  int to, nxt;
} e[maxn];
int tot;
int head[maxn];
char c[maxn];
void ae(int u, int v) {
  e[tot] = {v, head[u]};
  head[u] = tot++;
}
struct node {
  int to[26], fail;
} a[maxn];
int cnt = 0;
int newnode() {
  memset((a[cnt].to), 0, sizeof((a[cnt].to)));
  a[cnt].fail = 0;
  return cnt++;
}
void init() {
  cnt = 0;
  newnode();
  tot = 0;
  memset((head), -1, sizeof((head)));
}
int add(char s[]) {
  int n = strlen(s);
  int now = 0;
  for (int i = 0; i < n; ++i) {
    int k = s[i] - 'a';
    if (!a[now].to[k]) a[now].to[k] = newnode();
    now = a[now].to[k];
  }
  return now;
}
void build() {
  queue<int> q;
  a[0].fail = 0;
  for (int i = 0; i < 26; ++i)
    if (a[0].to[i]) {
      a[a[0].to[i]].fail = 0;
      q.push(a[0].to[i]);
    }
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    for (int i = 0; i < 26; ++i) {
      int son = a[now].to[i];
      if (son) {
        a[son].fail = a[a[now].fail].to[i];
        q.push(son);
      } else
        a[now].to[i] = a[a[now].fail].to[i];
    }
  }
}
struct ed {
  int to, nxt;
} ed[maxn << 1];
int hd[maxn];
int tt;
int fa[maxn], dep[maxn], siz[maxn], son[maxn], rk[maxn], top[maxn], id[maxn];
int cc, N;
int t[maxn << 2];
int lzy[maxn << 2];
void init1() {
  memset((hd), -1, sizeof((hd)));
  tt = 0;
  cc = 0;
}
void aed(int u, int v) {
  ed[tt] = {v, hd[u]};
  hd[u] = tt++;
  ed[tt] = {u, hd[v]};
  hd[v] = tt++;
}
void dfs1(int u) {
  dep[u] = dep[fa[u]] + 1;
  siz[u] = 1;
  for (int i = hd[u]; ~i; i = ed[i].nxt) {
    int v = ed[i].to;
    if (v == fa[u]) continue;
    fa[v] = u;
    dfs1(v);
    siz[u] += siz[v];
    if (siz[v] > siz[son[u]]) son[u] = v;
  }
}
void dfs2(int u, int t) {
  top[u] = t;
  id[u] = ++cc;
  rk[cc] = u;
  if (!son[u]) return;
  dfs2(son[u], t);
  for (int i = hd[u]; ~i; i = ed[i].nxt) {
    int v = ed[i].to;
    if (v != son[u] && v != fa[u]) dfs2(v, v);
  }
}
void bd(int l, int r, int rt) {
  lzy[rt] = 0;
  if (l == r) {
    t[rt] = 0;
    return;
  }
  int m = (l + r) >> 1;
  bd(l, m, (rt << 1));
  bd(m + 1, r, (rt << 1) | 1);
  t[rt] = (t[(rt << 1)] + t[(rt << 1) | 1]);
}
void pushdown(int l, int r, int rt) {
  if (lzy[rt]) {
    int m = (l + r) >> 1;
    (lzy[(rt << 1)] += lzy[rt]);
    (lzy[(rt << 1) | 1] += lzy[rt]);
    (t[(rt << 1)] += lzy[rt] * (m - l + 1));
    (t[(rt << 1) | 1] += lzy[rt] * (r - m));
    lzy[rt] = 0;
  }
}
void update(int L, int R, long long k, int l, int r, int rt) {
  if (L <= l && r <= R) {
    t[rt] += k * (r - l + 1);
    lzy[rt] += k;
    return;
  }
  pushdown(l, r, rt);
  int m = (l + r) >> 1;
  if (m >= R)
    update(L, R, k, l, m, (rt << 1));
  else if (m < L)
    update(L, R, k, m + 1, r, (rt << 1) | 1);
  else {
    update(L, m, k, l, m, (rt << 1));
    update(m + 1, R, k, m + 1, r, (rt << 1) | 1);
  }
  t[rt] = (t[(rt << 1)] + t[(rt << 1) | 1]);
}
long long query(int L, int R, int l, int r, int rt) {
  if (L <= l && r <= R) return t[rt];
  pushdown(l, r, rt);
  int m = (l + r) >> 1;
  if (m >= R)
    return query(L, R, l, m, (rt << 1));
  else if (m < L)
    return query(L, R, m + 1, r, (rt << 1) | 1);
  else
    return (query(L, m, l, m, (rt << 1)) +
            query(m + 1, R, m + 1, r, (rt << 1) | 1));
}
long long sum(int x, int y) {
  long long ans = 0;
  int fx = top[x], fy = top[y];
  while (fx != fy) {
    if (dep[fx] >= dep[fy]) {
      (ans += query(id[fx], id[x], 1, N, 1));
      x = fa[fx];
    } else {
      (ans += query(id[fy], id[y], 1, N, 1));
      y = fa[fy];
    }
    fx = top[x];
    fy = top[y];
  }
  if (id[x] <= id[y])
    (ans += query(id[x], id[y], 1, N, 1));
  else
    (ans += query(id[y], id[x], 1, N, 1));
  return ans;
}
void update_t(int x, int y, long long v) {
  int fx = top[x], fy = top[y];
  while (fx != fy) {
    if (dep[fx] >= dep[fy]) {
      update(id[fx], id[x], v, 1, N, 1);
      x = fa[fx];
    } else {
      update(id[fy], id[y], v, 1, N, 1);
      y = fa[fy];
    }
    fx = top[x];
    fy = top[y];
  }
  if (id[x] <= id[y])
    update(id[x], id[y], v, 1, N, 1);
  else
    update(id[y], id[x], v, 1, N, 1);
}
char s[maxn];
int res[maxn];
vector<pair<int, int> > que[maxn];
void go(int u, int p) {
  for (auto i : que[u]) res[i.first] = sum(i.second + 1, i.second + 1);
  for (int i = head[u]; ~i; i = e[i].nxt) {
    int v = e[i].to;
    int now = a[p].to[c[v] - 'a'];
    update_t(1, now + 1, 1);
    go(v, now);
    update_t(1, now + 1, -1);
  }
}
int main() {
  int n;
  while (~scanf("%d", &n)) {
    init();
    for (int i = 1; i <= n; ++i) {
      que[i].clear();
      int t, f;
      scanf("%d", &t);
      if (t == 1) {
        scanf("%s", s);
        c[i] = s[0];
        ae(0, i);
      } else {
        scanf("%d", &f);
        scanf("%s", s);
        c[i] = s[0];
        ae(f, i);
      }
    }
    int m;
    scanf("%d", &m);
    for (int i = 1; i <= m; ++i) {
      int u;
      scanf("%d%s", &u, s);
      int id = add(s);
      que[u].push_back({i, id});
    }
    build();
    init1();
    for (int i = 0; i < cnt; ++i)
      if (i != a[i].fail) aed(i + 1, a[i].fail + 1);
    N = cnt;
    bd(1, N, 1);
    dfs1(1);
    dfs2(1, 1);
    go(0, 0);
    for (int i = 1; i <= m; ++i) printf("%d\n", res[i]);
  }
}