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 double eps = 1e-8;
const double pi = acos(-1.0);
template <class T>
int chkmin(T &a, T b) {
  return a > b ? a = b, 1 : 0;
}
template <class T>
int chkmax(T &a, T b) {
  return a < b ? a = b, 1 : 0;
}
template <class T>
T sqr(T a) {
  return a * a;
}
template <class T>
T mmin(T a, T b) {
  return a < b ? a : b;
}
template <class T>
T mmax(T a, T b) {
  return a > b ? a : b;
}
template <class T>
T aabs(T a) {
  return a < 0 ? -a : a;
}
template <class T>
int dcmp(T a, T b) {
  return a > b;
}
template <int *a>
int cmp_a(int first, int second) {
  return a[first] < a[second];
}
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c,
                                       qu[55];
int f, qr;
inline void flush() {
  fwrite(obuf, 1, oS - obuf, stdout);
  oS = obuf;
}
inline void putc(char first) {
  *oS++ = first;
  if (oS == oT) flush();
}
inline bool read(signed &first) {
  for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                              (iS == iT ? EOF : *iS++))
                            : *iS++);
       c < '0' || c > '9';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    if (c == '-')
      f = -1;
    else if (c == EOF)
      return 0;
  for (first = 0; c <= '9' && c >= '0';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    first = first * 10 + (c & 15);
  first *= f;
  return 1;
}
inline bool read(long long &first) {
  for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                              (iS == iT ? EOF : *iS++))
                            : *iS++);
       c < '0' || c > '9';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    if (c == '-')
      f = -1;
    else if (c == EOF)
      return 0;
  for (first = 0; c <= '9' && c >= '0';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    first = first * 10 + (c & 15);
  first *= f;
  return 1;
}
inline bool read(char &first) {
  while ((first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                               (iS == iT ? EOF : *iS++))
                            : *iS++)) == '\n' ||
         first == ' ' || first == '\r')
    if (first == EOF) return 0;
  return 1;
}
inline bool read(char *first) {
  while ((*first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                                (iS == iT ? EOF : *iS++))
                             : *iS++)) == '\n' ||
         *first == ' ' || *first == '\r')
    if (*first == EOF) return 0;
  while (!(*first == '\n' || *first == ' ' || *first == '\r' || *first == EOF))
    *(++first) = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                              (iS == iT ? EOF : *iS++))
                           : *iS++);
  *first = 0;
  return 1;
}
template <typename A, typename... B>
inline bool read(A &first, B &...second) {
  return read(first) && read(second...);
}
inline bool write(signed first) {
  if (!first) putc('0');
  if (first < 0) putc('-'), first = -first;
  while (first) qu[++qr] = first % 10 + '0', first /= 10;
  while (qr) putc(qu[qr--]);
  return 0;
}
inline bool write(long long first) {
  if (!first) putc('0');
  if (first < 0) putc('-'), first = -first;
  while (first) qu[++qr] = first % 10 + '0', first /= 10;
  while (qr) putc(qu[qr--]);
  return 0;
}
inline bool write(char first) {
  putc(first);
  return 0;
}
inline bool write(const char *first) {
  while (*first) {
    putc(*first);
    ++first;
  }
  return 0;
}
inline bool write(char *first) {
  while (*first) {
    putc(*first);
    ++first;
  }
  return 0;
}
template <typename A, typename... B>
inline bool write(A first, B... second) {
  return write(first) || write(second...);
}
struct Flusher_ {
  ~Flusher_() { flush(); }
} io_flusher_;
}  // namespace io
using io ::putc;
using io ::read;
using io ::write;
int inf;
struct _init_ {
  _init_() { memset(&inf, 0x3f, sizeof(inf)); }
};
_init_ ___INIT__;
int to[400005][26], is[400005], t = 1;
char s[400005];
vector<pair<int, int> > q[400005];
int to2[400005][26], fail[400005], tt = 1;
vector<int> son[400005];
int ans[400005];
bool hv[400005];
int insert(char *s) {
  int at = 1;
  while (*s) {
    if (!to2[at][*s - 'a']) to2[at][*s - 'a'] = ++tt;
    at = to2[at][*s - 'a'];
    ++s;
  }
  hv[at] = 1;
  return at;
}
int qu[400005], *l, *r;
int sum[400005];
int dfn[400005], clk, low[400005];
void Add(int first, int second) {
  for (; first <= tt; first += first & -first) sum[first] += second;
}
int Sum(int first) {
  int second = 0;
  for (; first; first ^= first & -first) second += sum[first];
  return second;
}
void dfs(int first) {
  dfn[first] = clk;
  ++clk;
  for (auto i : son[first]) dfs(i);
  low[first] = clk;
}
void Draw_ACAM(int first, int id) {
  s[id] = 0;
  for (int i = 0; i < 26; ++i)
    if (to2[first][i]) {
      s[id] = i + 'a';
      Draw_ACAM(to2[first][i], id + 1);
    }
}
void Build_ACAM() {
  l = (r = qu) + 1;
  for (int i = 0; i < 26; ++i)
    if (to2[1][i]) {
      *(++r) = to2[1][i];
      fail[to2[1][i]] = 1;
    }
  for (; l <= r; ++l) {
    int cnt = 0;
    for (int j = 0; j < 26; ++j)
      if (to2[*l][j]) ++cnt;
    for (int i = fail[*l]; i && cnt; i = fail[i]) {
      for (int j = 0; j < 26; ++j)
        if (to2[i][j] && !fail[to2[*l][j]] && to2[*l][j]) {
          --cnt;
          fail[to2[*l][j]] = to2[i][j];
        }
    }
    for (int j = 0; j < 26; ++j)
      if (to2[*l][j] && !fail[to2[*l][j]]) fail[to2[*l][j]] = 1;
    for (int j = 0; j < 26; ++j)
      if (to2[*l][j]) *(++r) = to2[*l][j];
  }
  for (int i = 0; i < 26; ++i)
    if (!to2[1][i]) to2[1][i] = 1;
  for (l = qu + 1; l <= r; ++l) {
    son[fail[*l]].push_back(*l);
    for (int i = 0; i < 26; ++i)
      if (!to2[*l][i]) to2[*l][i] = to2[fail[*l]][i];
    hv[*l] |= hv[fail[*l]];
  }
  dfs(1);
}
int at = 1;
void Solve(int first) {
  for (auto i : q[first]) ans[i.second] = Sum(low[i.first]) - Sum(dfn[i.first]);
  for (int i = 0; i < 26; ++i)
    if (to[first][i]) {
      int oat = at;
      at = to2[at][i];
      if (hv[at]) Add(dfn[at] + 1, 1);
      Solve(to[first][i]);
      if (hv[at]) Add(dfn[at] + 1, -1);
      at = oat;
    }
}
signed main() {
  int n, tp, m;
  char c;
  read(n);
  is[0] = 1;
  for (int i = 1; i <= n; ++i) {
    read(tp);
    int fa = 0;
    if (tp == 2) read(fa);
    read(c);
    if (!to[is[fa]][c - 'a']) to[is[fa]][c - 'a'] = ++t;
    is[i] = to[is[fa]][c - 'a'];
  }
  read(m);
  int id;
  for (int i = 1; i <= m; ++i) {
    read(id, s);
    q[is[id]].push_back(make_pair(insert(s), i));
  }
  Build_ACAM();
  Solve(1);
  for (int i = 1; i <= m; ++i) write(ans[i], '\n');
  return 0;
}