#include <bits/stdc++.h>
int n, m;
struct edgeType {
  int to, nxt;
} e[400005 << 2];
int cntE;
int head[400005 << 1], qhead[400005 << 1];
int dfn[400005 << 1], siz[400005 << 1];
int depth[400005 << 1];
struct trieType {
  int nxt[26], fail;
} trie[400005 << 1];
int cntTrie;
int location[400005];
int qhy[400005 << 1], closed, open;
int bit[400005 << 1];
int ans[400005];
void add_edge(int& u, int v, int x) {
  e[x].to = v;
  e[x].nxt = u;
  u = x;
}
void add_bit(int x, int v) {
  for (; x <= cntTrie; x += (x & (-x))) bit[x] += v;
}
int orz;
int sum_bit(int x) {
  for (orz = 0; x > 0; x -= (x & (-x))) orz += bit[x];
  return orz;
}
void dfs(int x, int p) {
  int i;
  dfn[x] = ++dfn[0];
  siz[x] = 1;
  for (i = head[x]; i; i = e[i].nxt) {
    dfs(e[i].to, x);
    siz[x] += siz[e[i].to];
  }
}
void standing_by() {
  int i, j, k;
  int opt;
  char c;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &opt);
    if (opt == 1)
      j = 0;
    else
      scanf("%d", &j);
    j = location[j];
    c = getchar();
    while (c < 'a' || c > 'z') c = getchar();
    if (trie[j].nxt[c - 'a'] == 0) {
      trie[j].nxt[c - 'a'] = ++cntTrie;
    }
    j = trie[j].nxt[c - 'a'];
    location[i] = j;
  }
  scanf("%d", &m);
  for (i = 1; i <= m; i++) {
    scanf("%d", &k);
    c = getchar();
    while (c < 'a' || c > 'z') c = getchar();
    j = 0;
    while (c >= 'a' && c <= 'z') {
      if (trie[j].nxt[c - 'a'] == 0) trie[j].nxt[c - 'a'] = ++cntTrie;
      j = trie[j].nxt[c - 'a'];
      c = getchar();
    }
    add_edge(qhead[location[k]], j, ++cntE);
  }
  for (i = 0; i < 26; i++)
    if (trie[0].nxt[i]) {
      qhy[++open] = trie[0].nxt[i];
      depth[qhy[open]] = 1;
      add_edge(head[0], qhy[open], ++cntE);
    }
  do {
    closed++;
    for (i = 0; i < 26; i++)
      if (trie[qhy[closed]].nxt[i]) {
        qhy[++open] = trie[qhy[closed]].nxt[i];
        depth[qhy[open]] = depth[qhy[closed]] + 1;
        trie[qhy[open]].fail = trie[trie[qhy[closed]].fail].nxt[i];
        add_edge(head[trie[qhy[open]].fail], qhy[open], ++cntE);
      } else
        trie[qhy[closed]].nxt[i] = trie[trie[qhy[closed]].fail].nxt[i];
  } while (closed < open);
  for (i = head[0]; i; i = e[i].nxt) dfs(e[i].to, 0);
}
void dfs_extra(int x) {
  int i;
  add_bit(dfn[x], 1);
  for (i = qhead[x]; i; i = e[i].nxt)
    ans[i] =
        sum_bit(dfn[e[i].to] + siz[e[i].to] - 1) - sum_bit(dfn[e[i].to] - 1);
  for (i = 0; i < 26; i++)
    if (depth[trie[x].nxt[i]] > depth[x]) dfs_extra(trie[x].nxt[i]);
  add_bit(dfn[x], -1);
}
void complete() {
  int i;
  for (i = 0; i < 26; i++)
    if (trie[0].nxt[i]) dfs_extra(trie[0].nxt[i]);
  for (i = 1; i <= m; i++) printf("%d\n", ans[i]);
}
int main() {
  standing_by();
  complete();
  return 0;
}
