#include <bits/stdc++.h>
using namespace std;
const int N = 400005, SGM = 26;
int n, q, ts, ttm, C[N][SGM], F[N], Qu[N], P[N], St[N], Fn[N], FN[N], Res[N];
string T[N];
vector<pair<int, char> > Adj[N];
vector<int> Ad[N], Q[N];
inline void Add(int i, int val) {
  for (i++; i < N; i += i & -i) FN[i] += val;
}
inline int Get(int i) {
  int rt = 0;
  for (i++; i; i -= i & -i) rt += FN[i];
  return rt;
}
inline int Add(string &S) {
  int id = 0;
  for (int j = 0; j < (int)S.size(); j++) {
    if (!C[id][S[j] - 'a']) C[id][S[j] - 'a'] = ++ts;
    id = C[id][S[j] - 'a'];
  }
  return (id);
}
inline void AhoCorasick() {
  int l = 0, r = 0;
  for (int i = 0; i < SGM; i++)
    if (C[0][i]) Qu[r++] = C[0][i];
  while (r - l) {
    int id = Qu[l++];
    for (int j = 0; j < SGM; j++) {
      if (!C[id][j])
        C[id][j] = C[F[id]][j];
      else
        F[C[id][j]] = C[F[id]][j], Qu[r++] = C[id][j];
    }
    Ad[F[id]].push_back(id);
  }
}
void DFS(int v) {
  St[v] = ttm++;
  for (int u : Ad[v]) DFS(u);
  Fn[v] = ttm;
}
void DFS2(int v, int nw = 0) {
  Add(St[nw], 1);
  for (int i : Q[v]) Res[i] = Get(Fn[P[i]] - 1) - Get(St[P[i]] - 1);
  for (auto u : Adj[v]) DFS2(u.first, C[nw][u.second - 'a']);
  Add(St[nw], -1);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    char ch;
    int tp, p;
    cin >> tp;
    if (tp == 1)
      cin >> ch, Adj[0].push_back({i, ch});
    else
      cin >> p >> ch, Adj[p].push_back({i, ch});
  }
  cin >> q;
  for (int i = 1; i <= q; i++) {
    int v;
    cin >> v >> T[i];
    Q[v].push_back(i);
    P[i] = Add(T[i]);
  }
  AhoCorasick();
  DFS(0);
  DFS2(0);
  for (int i = 1; i <= q; i++) printf("%d\n", Res[i]);
  return 0;
}
