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>
const int MAX_N = 4e5 + 7;
inline long long gilbert_order(int x, int y, int pow, int rotate) {
  if (pow == 0) {
    return 0;
  }
  int hpow = 1 << (pow - 1);
  int seg = (x < hpow) ? ((y < hpow) ? 0 : 3) : ((y < hpow) ? 1 : 2);
  seg = (seg + rotate) & 3;
  const int rotateDelta[4] = {3, 0, 0, 1};
  int nx = x & (x ^ hpow), ny = y & (y ^ hpow);
  int nrot = (rotate + rotateDelta[seg]) & 3;
  long long subSquareSize = 1LL << (2 * pow - 2);
  long long ans = seg * subSquareSize;
  long long add = gilbert_order(nx, ny, pow - 1, nrot);
  ans += (seg == 1 || seg == 2) ? add : (subSquareSize - add - 1);
  return ans;
}
struct xy_query {
  int first, second, id;
  long long ord;
  xy_query(void) : xy_query(-1, -1, -1) {}
  xy_query(const int& l, const int& r, const int& ii)
      : first(l), second(r), id(ii) {
    ord = gilbert_order(first, second, 21, 0);
  }
  bool operator<(const xy_query& o) const { return ord < o.ord; }
} queries[MAX_N];
template <typename T>
struct BIT {
  const int n;
  T* arr;
  BIT(const int& _n) : n(_n), arr(new T[_n + 2]()) {}
  ~BIT(void) { delete[] arr; }
  void add(const int& i, const int& v) {
    for (int j = i; j <= n; j += (j & -j)) arr[j] += v;
  }
  T qry(const int& hi) {
    int ret = 0;
    for (int j = hi; j > 0; j -= (j & -j)) ret += arr[j];
    return ret;
  }
  T qry(const int& lo, const int& hi) { return qry(hi) - qry(lo - 1); }
};
const int ALPHA = 26;
struct aho_corasicc {
  int node_count;
  std::vector<std::vector<int>> trans;
  std::vector<char> term;
  std::vector<std::vector<int>> ending_bois;
  std::vector<int> ends_at;
  std::vector<std::vector<int>> gr;
  std::vector<std::vector<std::pair<int, int>>> has_qry;
  std::vector<std::vector<int>> queries;
  std::vector<int> parentless;
  aho_corasicc(void) {}
  aho_corasicc(const std::vector<std::string>& strs) {
    int max_nodes = 1;
    for (const auto& s : strs) max_nodes += s.length();
    std::vector<std::vector<int>> trie(ALPHA, std::vector<int>(max_nodes));
    std::vector<char> leaf(max_nodes);
    ending_bois.resize(max_nodes);
    ends_at.resize(strs.size());
    node_count = 1;
    int idx = 0;
    for (const auto& s : strs) {
      int node = 0;
      for (const auto& ch : s) {
        int c = ch - 'a';
        if (trie[c][node] == 0) {
          trie[c][node] = node_count++;
        }
        node = trie[c][node];
      }
      leaf[node] = true;
      ends_at[idx++] = node;
    }
    trans.assign(ALPHA, std::vector<int>(node_count));
    term.assign(node_count, 0);
    gr.resize(node_count);
    has_qry.resize(node_count);
    std::queue<int> order;
    order.push(0);
    order.push(0);
    while (!order.empty()) {
      int node = order.front();
      order.pop();
      int slink = order.front();
      order.pop();
      term[node] = leaf[node] || term[slink];
      if (node != slink) {
        gr[slink].push_back(node);
      } else {
        parentless.push_back(node);
      }
      for (int ch = 0; ch < ALPHA; ++ch) {
        if (trie[ch][node] != 0) {
          trans[ch][node] = trie[ch][node];
          order.push(trie[ch][node]);
          order.push(node == 0 ? 0 : trans[ch][slink]);
        } else
          trans[ch][node] = trans[ch][slink];
      }
    }
    range.resize(node_count);
  }
  std::vector<std::pair<int, int>> arr;
  std::vector<std::pair<int, int>> range;
  void dfs(const int& cur) {
    range[cur].first = arr.size();
    arr.insert(arr.end(), has_qry[cur].begin(), has_qry[cur].end());
    arr.push_back({MAX_N, MAX_N + 1});
    for (const auto& nei : gr[cur]) dfs(nei);
    range[cur].second = arr.size();
  }
};
std::vector<std::pair<int, char>> gr[MAX_N];
int pre[2][MAX_N];
int po = 1;
void go(const int& cur) {
  pre[0][cur] = po++;
  for (const auto& e : gr[cur]) go(e.first);
  pre[1][cur] = po;
}
int main(void) {
  std::ios::sync_with_stdio(0);
  std::cin.tie(0);
  int n;
  std::cin >> n;
  for (int i = 1; i <= n; ++i) {
    int type;
    std::cin >> type;
    char c;
    if (type == 1) {
      std::cin >> c;
      gr[0].push_back({i, c});
    } else {
      int idx;
      std::cin >> idx >> c;
      gr[idx].push_back({i, c});
    }
  }
  go(0);
  int m;
  std::cin >> m;
  std::vector<std::string> strs(m);
  std::vector<int> qs(m);
  for (int i = 0; i < m; ++i) std::cin >> qs[i] >> strs[i];
  aho_corasicc aho(strs);
  std::queue<int> ord;
  ord.push(0);
  ord.push(0);
  while (ord.size()) {
    int cur = ord.front();
    ord.pop();
    int node = ord.front();
    ord.pop();
    aho.has_qry[node].push_back({pre[0][cur], pre[1][cur]});
    for (const auto& nei : gr[cur]) {
      ord.push(nei.first);
      ord.push(aho.trans[nei.second - 'a'][node]);
    }
  }
  aho.dfs(0);
  for (int i = 0; i < m; ++i)
    queries[i] = xy_query(aho.range[aho.ends_at[i]].first,
                          aho.range[aho.ends_at[i]].second - 1, i);
  BIT<int> freq(MAX_N + 10);
  std::sort(queries, queries + m);
  std::vector<int> ans(m);
  int earliest_in = 0, latest_in = -1;
  std::function<void(const int&)> add = [&](const int& i) {
    freq.add(aho.arr[i].first, 1);
    freq.add(aho.arr[i].second, -1);
  };
  std::function<void(const int&)> rem = [&](const int& i) {
    freq.add(aho.arr[i].first, -1);
    freq.add(aho.arr[i].second, 1);
  };
  for (int i = 0; i < m; ++i) {
    for (; earliest_in < queries[i].first; ++earliest_in) rem(earliest_in);
    for (; earliest_in > queries[i].first; --earliest_in) add(earliest_in - 1);
    for (; latest_in < queries[i].second; ++latest_in) add(latest_in + 1);
    for (; latest_in > queries[i].second; --latest_in) rem(latest_in);
    ans[queries[i].id] = freq.qry(pre[0][qs[queries[i].id]]);
  }
  for (int i = 0; i < m; ++i) std::cout << ans[i] << "\n";
  return 0;
}