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;
int N;
vector<pair<int, char>> E[402020];
int Q;
vector<int> ev[404040];
vector<string> S;
string RS;
int ind[404040];
signed long long ret[404040];
struct SuffixArray {
  int N;
  vector<int> rank, lcp, sa, rsa;
  string S;
  void build(string S) {
    this->S = S;
    int i, h = 0;
    vector<int> tmp;
    N = S.size();
    rank.resize(N + 1);
    sa.resize(N + 1);
    tmp.resize(N + 1);
    for (i = 0; i < (N + 1); i++) sa[i] = i, rank[i] = i == N ? -1 : S[i];
    for (int k = 1; k <= N; k <<= 1) {
      auto pred2 = [k, this](int& a, int& b) -> bool {
        return (((a + k <= N) ? rank[a + k] : -1) <
                ((b + k <= N) ? rank[b + k] : -1));
      };
      auto pred = [pred2, k, this](int& a, int& b) -> bool {
        return (rank[a] != rank[b]) ? (rank[a] < rank[b]) : pred2(a, b);
      };
      int x = 0;
      if (k != 1)
        for (i = 1; i < N + 1; i++)
          if (rank[sa[i]] != rank[sa[x]])
            sort(sa.begin() + x, sa.begin() + i, pred2), x = i;
      sort(sa.begin() + x, sa.end(), pred);
      for (i = 0; i < (N + 1); i++)
        tmp[sa[i]] = (i == 0) ? 0 : tmp[sa[i - 1]] + pred(sa[i - 1], sa[i]);
      swap(rank, tmp);
    }
    lcp.resize(N + 1);
    rsa.resize(N + 1);
    for (i = 0; i < (N + 1); i++) rsa[sa[i]] = i;
    for (i = 0; i < (N); i++) {
      int j = sa[rsa[i] - 1];
      for (h = max(h - 1, 0); i + h < N && j + h < N; h++)
        if (S[j + h] != S[i + h]) break;
      lcp[rsa[i] - 1] = h;
    }
  }
};
SuffixArray sa;
const int NUMC = 26;
class Trie {
 public:
  vector<vector<int>> V;
  int find(string s) {
    int cur = 0;
    for (__typeof(s.begin()) it = s.begin(); it != s.end(); it++)
      if ((cur = V[cur][*it + 1]) == 0) return -1;
    return cur;
  }
  void create(vector<string> S) {
    V.clear();
    V.push_back(vector<int>(NUMC + 1));
    sort(S.begin(), S.end());
    for (__typeof(S.begin()) it = S.begin(); it != S.end(); it++) {
      int cur = 0;
      for (__typeof((*it).begin()) c = (*it).begin(); c != (*it).end(); c++) {
        if (V[cur][*c + 1] == 0)
          V.push_back(vector<int>(NUMC + 1)), V[cur][*c + 1] = V.size() - 1;
        cur = V[cur][*c + 1];
      }
    }
  }
};
template <class V, int NV>
class SegTree_1 {
 public:
  vector<V> val;
  static V const def = 1 << 20;
  V comp(V l, V r) { return min(l, r); };
  SegTree_1() { val = vector<V>(NV * 2, 0); };
  V getval(int x, int y, int l = 0, int r = NV, int k = 1) {
    if (r <= x || y <= l) return def;
    if (x <= l && r <= y) return val[k];
    return comp(getval(x, y, l, (l + r) / 2, k * 2),
                getval(x, y, (l + r) / 2, r, k * 2 + 1));
  }
  void update(int entry, V v) {
    entry += NV;
    val[entry] = v;
    while (entry > 1)
      entry >>= 1, val[entry] = comp(val[entry * 2], val[entry * 2 + 1]);
  }
};
SegTree_1<int, 1 << 20> st;
template <class V, int ME>
class BIT {
 public:
  V bit[1 << ME];
  V operator()(int e) {
    if (e < 0) return 0;
    V s = 0;
    e++;
    while (e) s += bit[e - 1], e -= e & -e;
    return s;
  }
  void add(int e, V v) {
    e++;
    while (e <= 1 << ME) bit[e - 1] += v, e += e & -e;
  }
};
BIT<signed long long, 20> bt;
class ACmatch_enum {
 public:
  Trie t;
  vector<vector<int>> acc;
  int ma;
  void create(vector<string> S) {
    int i, j;
    ma = S.size();
    t.create(S);
    acc.clear();
    acc.resize(t.V.size());
    for (i = 0; i < (S.size()); i++) {
      int st = ind[i];
      int cur = 0;
      for (j = 0; j < (S[i].size()); j++) {
        cur = t.V[cur][S[i][j] + 1];
        int tar = sa.rsa[ind[i] + S[i].size() - 1 - j];
        if (acc[cur].empty()) acc[cur].push_back(tar);
      }
    }
    queue<int> Q;
    for (i = 0; i < (NUMC); i++)
      if (t.V[0][i + 1]) t.V[t.V[0][i + 1]][0] = 0, Q.push(t.V[0][i + 1]);
    while (!Q.empty()) {
      int k = Q.front();
      Q.pop();
      for (i = 0; i < (NUMC); i++)
        if (t.V[k][i + 1]) {
          Q.push(t.V[k][i + 1]);
          int pre = t.V[k][0];
          while (pre && t.V[pre][i + 1] == 0) pre = t.V[pre][0];
          t.V[t.V[k][i + 1]][0] = t.V[pre][i + 1];
        }
    }
  }
  void dfs(int cur, int pos) {
    int i;
    for (auto& e : ev[cur]) {
      int L = sa.rsa[ind[e]], R = L;
      for (int j = 18; j >= 0; j--)
        if (R + (1 << j) <= RS.size() &&
            st.getval(L, R + (1 << j)) >= S[e].size())
          R += 1 << j;
      for (int j = 18; j >= 0; j--)
        if (L - (1 << j) >= 0 && st.getval(L - (1 << j), R) >= S[e].size())
          L -= 1 << j;
      ret[e] = bt(R) - bt(L - 1);
    }
    for (auto& e : E[cur]) {
      int prev = pos;
      while (pos && t.V[pos][e.second + 1] == 0) pos = t.V[pos][0];
      pos = t.V[pos][e.second + 1];
      for (auto& r : acc[pos]) bt.add(r, 1);
      dfs(e.first, pos);
      for (auto& r : acc[pos]) bt.add(r, -1);
      pos = prev;
    }
  }
};
ACmatch_enum ac;
void solve() {
  int i, j, k, l, r, x, y;
  string s;
  cin >> N;
  for (i = 0; i < (N); i++) {
    cin >> j;
    x = 0;
    if (j == 2) cin >> x;
    cin >> s;
    E[x].push_back({i + 1, s[0] - 'a'});
  }
  cin >> Q;
  map<string, int> memo;
  for (i = 0; i < (Q); i++) {
    cin >> x >> s;
    ev[x].push_back(i);
    for (auto& c : s) c -= 'a';
    S.push_back(s);
    for (auto& c : s) c += 'a';
    reverse((s.begin()), (s.end()));
    if (memo.count(s)) {
      ind[i] = memo[s];
    } else {
      ind[i] = RS.size();
      memo[s] = ind[i];
      RS += s + "#";
    }
  }
  sa.build(RS);
  for (i = 0; i < (RS.size()); i++) st.update(i, sa.lcp[i]);
  ac.create(S);
  ac.dfs(0, 0);
  for (i = 0; i < (Q); i++) cout << ret[i] << endl;
}
int main(int argc, char** argv) {
  string s;
  int i;
  if (argc == 1) ios::sync_with_stdio(false), cin.tie(0);
  for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s += '\n';
  for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin);
  cout.tie(0);
  solve();
  return 0;
}