#include <bits/stdc++.h>
using namespace std;
struct Q {
  int vs, i;
};
const int N = 4e5 + 5, P = 700, D = 26;
int to[N][D], tos[N][D], ver[N], ans[N], ct[N], ancv[N], ancc[N], p[N * 2];
int cv = 1, cvs = 1;
vector<Q> qr[N];
int go(int v, int c) {
  if (to[v][c] == -1) {
    ancv[cv] = v;
    ancc[cv] = c;
    to[v][c] = cv++;
  }
  return to[v][c];
}
void dfs(int node) {
  for (int i = 0, v = node, vs = 0; v > 0 && i < P; i++) {
    vs = tos[vs][ancc[v]];
    if (vs == -1) break;
    ct[vs]++;
    v = ancv[v];
  }
  for (Q q : qr[node]) {
    ans[q.i] = ct[q.vs];
  }
  for (int i = 0; i < D; i++) {
    if (to[node][i] != -1) {
      dfs(to[node][i]);
    }
  }
  for (int i = 0, v = node, vs = 0; v > 0 && i < P; i++) {
    vs = tos[vs][ancc[v]];
    if (vs == -1) break;
    ct[vs]--;
    v = ancv[v];
  }
}
int calc(string &s, int sz) {
  int ans = 0;
  fill(p, p + s.size(), 0);
  for (int i = 1; i < s.size(); i++) {
    int x = p[i - 1];
    while (x > 0 && s[x] != s[i]) {
      x = p[x - 1];
    }
    if (s[x] == s[i]) x++;
    p[i] = x;
    if (x == sz) ans++;
  }
  return ans;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n;
  memset(to, -1, sizeof(to));
  memset(tos, -1, sizeof(tos));
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    if (t == 1) {
      char c;
      cin >> c;
      ver[i] = go(0, c - 'a');
    } else {
      int j;
      char c;
      cin >> j >> c;
      ver[i] = go(ver[j - 1], c - 'a');
    }
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    int j;
    string t;
    cin >> j >> t;
    j--;
    if (t.size() < P) {
      reverse(t.begin(), t.end());
      int v = 0;
      for (char c : t) {
        c -= 'a';
        if (tos[v][c] == -1) {
          tos[v][c] = cvs++;
        }
        v = tos[v][c];
      }
      qr[ver[j]].push_back({v, i});
    } else {
      string s;
      int v = ver[j];
      while (v > 0) {
        s.push_back(ancc[v] + 'a');
        v = ancv[v];
      }
      reverse(s.begin(), s.end());
      s = t + "#" + s;
      ans[i] = calc(s, t.size());
    }
  }
  dfs(0);
  for (int i = 0; i < m; i++) {
    cout << ans[i] << "\n";
  }
  return 0;
}
