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>
namespace ds::segtree {
int _sum[4000005];
void init(int segleft, int segright, int segp) {
  _sum[segp] = 0;
  if (segleft != segright) {
    auto mid = (segleft + segright) / 2;
    init(segleft, mid, segp * 2);
    init(mid + 1, segright, segp * 2 + 1);
  }
}
void update(int pos, int delta, int segleft, int segright, int segp) {
  while (segleft != segright) {
    _sum[segp] += delta;
    auto mid = (segleft + segright) / 2;
    if (pos <= mid) {
      segright = mid;
      segp = segp * 2;
    } else {
      segleft = mid + 1;
      segp = segp * 2 + 1;
    }
  }
  _sum[segp] += delta;
}
int query(int left, int right, int segleft, int segright, int segp) {
  if (left == segleft && right == segright) {
    return _sum[segp];
  } else {
    auto mid = (segleft + segright) / 2;
    if (right <= mid) {
      return query(left, right, segleft, mid, segp * 2);
    } else if (left > mid) {
      return query(left, right, mid + 1, segright, segp * 2 + 1);
    } else {
      return query(left, mid, segleft, mid, segp * 2) +
             query(mid + 1, right, mid + 1, segright, segp * 2 + 1);
    }
  }
}
}  // namespace ds::segtree
namespace strings {
namespace gsam {
constexpr const int alphabet = 26;
constexpr const int pool_size = 1000005;
struct _node {
  int len;
  int cnt;
  int fail;
  int next[alphabet];
  std::vector<int> fail_children;
  int fail_enter, fail_leave;
  void init() {
    len = 0;
    cnt = 0;
    fail = -1;
    memset(next, 0xff, sizeof(next));
    fail_children.clear();
    fail_enter = fail_leave = 0;
  }
};
_node _pool[pool_size];
int _pooltop;
int _root, _maxlen;
int _order[pool_size];
int _alloc() {
  _pool[_pooltop].init();
  return _pooltop++;
}
void _radix_sort() {
  std::vector<int> c(_maxlen + 1, 0);
  for (auto i = 0; i < _pooltop; ++i) {
    ++c[_pool[i].len];
  }
  std::partial_sum(c.begin(), c.end(), c.begin());
  for (int i = 0; i < _pooltop; ++i) {
    _order[--c[_pool[i].len]] = i;
  }
}
void _fail_dfs(int now, int& idbag) {
  _pool[now].fail_enter = idbag++;
  for (auto child : _pool[now].fail_children) {
    _fail_dfs(child, idbag);
  }
  _pool[now].fail_leave = idbag;
}
void init() {
  _pooltop = 0;
  _root = _alloc();
}
int extend(int pnode, char ch) {
  auto mch = ch - 'a';
  auto p = pnode;
  auto last = p;
  if (_pool[p].next[mch] == -1) {
    auto np = _alloc();
    _pool[np].len = _pool[p].len + 1;
    _pool[np].cnt = 1;
    while (p != -1 && _pool[p].next[mch] == -1) {
      _pool[p].next[mch] = np;
      p = _pool[p].fail;
    }
    if (p == -1) {
      _pool[np].fail = _root;
    } else {
      auto q = _pool[p].next[mch];
      if (_pool[q].len == _pool[p].len + 1) {
        _pool[np].fail = q;
      } else {
        auto nq = _alloc();
        _pool[nq].len = _pool[p].len + 1;
        _pool[nq].cnt = 0;
        _pool[nq].fail = _pool[q].fail;
        memcpy(_pool[nq].next, _pool[q].next, sizeof(_pool[nq].next));
        _pool[q].fail = _pool[np].fail = nq;
        while (p != -1 && _pool[p].next[mch] == q) {
          _pool[p].next[mch] = nq;
          p = _pool[p].fail;
        }
      }
    }
    last = np;
  } else {
    auto q = _pool[p].next[mch];
    if (_pool[q].len == _pool[p].len + 1) {
      ++_pool[q].cnt;
      last = q;
    } else {
      auto nq = _alloc();
      _pool[nq].len = _pool[p].len + 1;
      _pool[nq].cnt = 1;
      _pool[nq].fail = _pool[q].fail;
      memcpy(_pool[nq].next, _pool[q].next, sizeof(_pool[nq].next));
      _pool[q].fail = nq;
      while (p != -1 && _pool[p].next[mch] == q) {
        _pool[p].next[mch] = nq;
        p = _pool[p].fail;
      }
      last = nq;
    }
  }
  _maxlen = std::max(_maxlen, _pool[last].len);
  return last;
}
int extend(const char* s) {
  auto p = _root;
  while (*s) {
    p = extend(p, *s++);
  }
  return p;
}
int advance(int& nodep, int& len, char ch) {
  auto mch = ch - 'a';
  if (_pool[nodep].next[mch] == -1) {
    while (_pool[nodep].next[mch] == -1) {
      nodep = _pool[nodep].fail;
    }
    if (nodep == -1) {
      len = 0;
      nodep = _root;
    } else {
      len = _pool[nodep].len + 1;
      nodep = _pool[nodep].next[mch];
    }
  } else {
    nodep = _pool[nodep].next[mch];
    ++len;
  }
  return nodep;
}
int advance(const char* s, int& len) {
  auto p = _root;
  len = 0;
  while (*s) {
    advance(p, len, *s++);
  }
  return p;
}
void freeze() {
  _radix_sort();
  for (int i = _pooltop - 1; i >= 0; --i) {
    int now = _order[i];
    if (_pool[now].fail != -1) {
      _pool[_pool[now].fail].fail_children.push_back(now);
      _pool[_pool[now].fail].cnt += _pool[now].cnt;
    }
  }
  auto idbag = 1;
  _fail_dfs(_root, idbag);
}
int size() { return _pooltop; }
}  // namespace gsam
namespace trie {
constexpr const int alphabet = 26;
constexpr const int pool_size = 400005;
struct _query {
  int sam_node;
  int& ans;
  explicit _query(int sam_node, int& ans) : sam_node(sam_node), ans(ans) {}
};
struct _node {
  int sam_node;
  int children[alphabet];
  std::vector<_query> que;
  void init() {
    sam_node = 0;
    memset(children, 0xff, sizeof(children));
    que.clear();
  }
};
_node _pool[pool_size];
int _pooltop;
int _root;
int _alloc() {
  _pool[_pooltop].init();
  return _pooltop++;
}
void _dfs_solve(int now) {
  auto fail_id = gsam::_pool[_pool[now].sam_node].fail_enter;
  ds::segtree::update(fail_id, 1, 1, gsam::size(), 1);
  for (auto& que : _pool[now].que) {
    auto low = gsam::_pool[que.sam_node].fail_enter;
    auto high = gsam::_pool[que.sam_node].fail_leave - 1;
    que.ans = ds::segtree::query(low, high, 1, gsam::size(), 1);
  }
  for (auto i = 0; i < alphabet; ++i) {
    if (_pool[now].children[i] != -1) {
      _dfs_solve(_pool[now].children[i]);
    }
  }
  ds::segtree::update(fail_id, -1, 1, gsam::size(), 1);
}
void init() {
  _pooltop = 0;
  _root = _alloc();
}
int extend(int last, char ch) {
  auto mch = ch - 'a';
  if (_pool[last].children[mch] == -1) {
    _pool[last].children[mch] = _alloc();
  }
  return _pool[last].children[mch];
}
int extend(const char* s) {
  auto p = _root;
  while (*s) {
    p = extend(p, *s++);
  }
  return p;
}
void add_query(int node, int sam_node, int& ans) {
  _pool[node].que.emplace_back(sam_node, ans);
}
void solve() {
  ds::segtree::init(1, gsam::size(), 1);
  _dfs_solve(_root);
}
}  // namespace trie
}  // namespace strings
int trie_last[400005];
char buffer[400005];
int qans[400005];
int n, m;
int main() {
  scanf("%d", &n);
  strings::gsam::init();
  strings::trie::init();
  int t;
  for (auto i = 1; i <= n; ++i) {
    scanf("%d", &t);
    assert(t == 1 || t == 2);
    if (t == 1) {
      scanf("%s", buffer);
      trie_last[i] = strings::trie::extend(buffer);
      strings::trie::_pool[trie_last[i]].sam_node =
          strings::gsam::extend(buffer);
    } else {
      int j;
      scanf("%d", &j);
      scanf("%s", buffer);
      trie_last[i] = strings::trie::extend(trie_last[j], buffer[0]);
      strings::trie::_pool[trie_last[i]].sam_node = strings::gsam::extend(
          strings::trie::_pool[trie_last[j]].sam_node, buffer[0]);
    }
  }
  strings::gsam::freeze();
  scanf("%d", &m);
  for (auto kase = 1; kase <= m; ++kase) {
    int id;
    scanf("%d", &id), scanf("%s", buffer);
    auto tlen = strlen(buffer);
    int sam_len;
    auto sam_node = strings::gsam::advance(buffer, sam_len);
    if (sam_len >= tlen) {
      strings::trie::add_query(trie_last[id], sam_node, qans[kase]);
    }
  }
  strings::trie::solve();
  for (auto kase = 1; kase <= m; ++kase) {
    printf("%d\n", qans[kase]);
  }
  return 0;
}