#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 5;
char s[N];
int t[N][26], tot;
int ma[N];
vector<pair<int, int> > qe[N];
int ch[N][26], fa[N], ct;
int q[N], l, r;
int pos[N], ed[N], tim;
vector<int> G[N];
void dfs0(int);
int tr[N];
void add(int i, int k) {
  while (i < N) tr[i] += k, i += i & -i;
}
int ask(int i) {
  int re = 0;
  while (i) re += tr[i], i -= i & -i;
  return re;
}
int ans[N];
void dfs(int, int);
void init();
void work();
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  work();
  return 0;
}
void work() {
  int n;
  cin >> n;
  char c;
  int p;
  for (int i = 1; i <= n; i++) {
    int op;
    cin >> op;
    if (op == 1)
      p = 0;
    else {
      cin >> p;
      p = ma[p];
    }
    cin >> c;
    c -= 'a';
    if (!t[p][c]) t[p][c] = ++tot;
    p = t[p][c];
    ma[i] = p;
  }
  int m;
  cin >> m;
  int k;
  for (int i = 1; i <= m; i++) {
    cin >> k >> s + 1;
    int p = 0;
    for (int j = 1; s[j]; j++) {
      c = s[j] - 'a';
      if (!ch[p][c]) ch[p][c] = ++ct;
      p = ch[p][c];
    }
    qe[ma[k]].push_back(pair<int, int>(p, i));
  }
  for (int i = 0; i < 26; i++)
    if (ch[0][i]) q[r++] = ch[0][i];
  while (l < r) {
    int u = q[l++];
    for (int i = 0; i < 26; i++)
      if (ch[u][i]) {
        fa[ch[u][i]] = ch[fa[u]][i];
        q[r++] = ch[u][i];
      } else
        ch[u][i] = ch[fa[u]][i];
  }
  for (int i = 1; i <= ct; i++) G[fa[i]].push_back(i);
  dfs0(0);
  dfs(0, 0);
  for (int i = 1; i <= m; i++) cout << ans[i] << '\n';
}
void dfs(int u, int p) {
  add(pos[p], 1);
  for (auto& q : qe[u])
    ans[q.second] = ask(ed[q.first]) - ask(pos[q.first] - 1);
  for (int i = 0; i < 26; i++)
    if (t[u][i]) {
      dfs(t[u][i], ch[p][i]);
    }
  add(pos[p], -1);
}
void dfs0(int u) {
  pos[u] = ++tim;
  for (auto v : G[u]) dfs0(v);
  ed[u] = tim;
}
