#include <bits/stdc++.h>
using namespace std;
const int N = 8e5 + 10;
const int M = 26;
namespace SAM {
struct state {
  int len, link;
  int next[M];
};
state st[N];
int sz, last;
void sa_init() {
  sz = 1, last = 0;
  st[0].len = 0;
  st[0].link = -1;
}
void sa_extend(int c) {
  if (st[last].next[c]) {
    int pp = st[last].next[c];
    if (st[pp].len == st[last].len + 1) {
      last = pp;
      return;
    }
  }
  int r = sz++;
  st[r].len = st[last].len + 1;
  int p = last;
  while (p != -1 && !st[p].next[c]) st[p].next[c] = r, p = st[p].link;
  if (p == -1)
    st[r].link = 0;
  else {
    int q = st[p].next[c];
    if (st[q].len == st[p].len + 1)
      st[r].link = q;
    else {
      int qq = sz++;
      st[qq].link = st[q].link;
      memcpy(st[qq].next, st[q].next, sizeof(st[q].next));
      st[qq].len = st[p].len + 1;
      while (p != -1 && st[p].next[c] == q) st[p].next[c] = qq, p = st[p].link;
      st[q].link = st[r].link = qq;
    }
  }
  last = r;
}
vector<int> g[N];
void build() {
  for (int i = 1; i < sz; i++) g[st[i].link].push_back(i);
}
}  // namespace SAM
namespace BIT {
int bit[N];
int lowbit(int x) { return x & (-x); }
void add(int x, int p) {
  while (x < N) {
    bit[x] += p;
    x += lowbit(x);
  }
}
int sum(int x) {
  int ans = 0;
  while (x > 0) {
    ans += bit[x];
    x -= lowbit(x);
  }
  return ans;
}
}  // namespace BIT
char ch[N];
int d[N], p[N];
vector<int> g[N];
void dfs(int u, int id) {
  SAM::last = id;
  SAM::sa_extend(ch[u] - 'a');
  d[u] = SAM::last;
  for (int v : g[u]) {
    dfs(v, d[u]);
  }
}
int dfn, L[N], R[N];
void dfs2(int u) {
  L[u] = ++dfn;
  for (int v : SAM::g[u])
    if (v) dfs2(v);
  R[u] = dfn;
}
int solve(string &s) {
  int n = s.size();
  int u = 0;
  for (int i = 0; i < n; i++) {
    u = SAM::st[u].next[s[i] - 'a'];
    if (u == 0) return 0;
  }
  return BIT::sum(R[u]) - BIT::sum(L[u] - 1);
}
char str[N];
string s[N];
int ans[N];
vector<int> q[N];
void dfs3(int u) {
  BIT::add(L[d[u]], 1);
  for (int i : q[u]) {
    ans[i] = solve(s[i]);
  }
  for (int v : g[u]) {
    dfs3(v);
  }
  BIT::add(L[d[u]], -1);
}
int main() {
  SAM::sa_init();
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int t;
    scanf("%d", &t);
    if (t == 1) {
      char tmp[10];
      scanf("%s", tmp);
      p[i] = 0;
      ch[i] = tmp[0];
    } else {
      char tmp[10];
      scanf("%d%s", &p[i], tmp);
      ch[i] = tmp[0];
      g[p[i]].push_back(i);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (p[i] == 0) dfs(i, 0);
  }
  SAM::build();
  dfs2(0);
  int m;
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    int u;
    scanf("%d", &u);
    q[u].push_back(i);
    scanf("%s", str);
    s[i] = str;
  }
  for (int i = 1; i <= n; i++) {
    if (p[i] == 0) dfs3(i);
  }
  for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
}
