#include <bits/stdc++.h>
using namespace std;
const int MAXN = 4 * 1e5 + 5;
int n, m;
int r[2 * MAXN];
int t[2 * MAXN][26];
int fail[2 * MAXN];
vector<int> q[2 * MAXN];
int cnt;
char s[MAXN];
int len;
vector<int> to[2 * MAXN];
void insert_a(int x) {
  if (!t[r[x]][s[1] - 'a']) t[r[x]][s[1] - 'a'] = cnt;
  r[cnt] = t[r[x]][s[1] - 'a'];
}
void insert_b(int x, int i) {
  int id = 0;
  int k = 1;
  while (1) {
    if (!t[id][s[k] - 'a']) cnt++, t[id][s[k] - 'a'] = cnt;
    id = t[id][s[k] - 'a'];
    if (k == len) {
      to[r[x]].push_back(id);
      to[r[x]].push_back(i);
      break;
    }
    k++;
  }
}
queue<int> que;
void build_fail(int st) {
  for (int i = 0; i < 26; i++) {
    if (!t[st][i]) continue;
    q[st].push_back(t[st][i]);
    que.push(t[st][i]);
  }
  while (!que.empty()) {
    int x = que.front();
    que.pop();
    for (int i = 0; i < 26; i++) {
      if (!t[x][i]) continue;
      int nx = fail[x];
      while (t[nx][i] == 0 && nx != 0) nx = fail[nx];
      fail[t[x][i]] = t[nx][i];
      q[t[nx][i]].push_back(t[x][i]);
      que.push(t[x][i]);
    }
  }
}
void init() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    cnt++;
    int c, x = 0;
    scanf("%d", &c);
    if (c == 2) scanf("%d", &x);
    scanf("%s", s + 1);
    len = strlen(s + 1);
    insert_a(x);
  }
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    int x;
    scanf("%d", &x);
    scanf("%s", s + 1);
    len = strlen(s + 1);
    insert_b(x, i);
  }
  build_fail(0);
}
int num;
int dfn[2 * MAXN];
int w[2 * MAXN];
void dfs(int x, int pr) {
  num++;
  dfn[x] = num;
  for (int i = 0; i < q[x].size(); i++) {
    int nx = q[x][i];
    dfs(nx, x);
    w[x] += w[nx];
  }
  w[x]++;
}
int sum[2 * MAXN];
void update(int x, int v) {
  while (x <= 800000) sum[x] += v, x += (x & (-x));
}
int get(int x) {
  int an = 0;
  while (x > 0) an += sum[x], x -= (x & (-x));
  return an;
}
int ans[MAXN];
void run(int x) {
  update(dfn[x], 1);
  for (int i = 0; i < to[x].size(); i += 2) {
    int nx = to[x][i];
    int id = to[x][i + 1];
    ans[id] = get(dfn[nx] + w[nx] - 1) - get(dfn[nx] - 1);
  }
  for (int i = 0; i < 26; i++)
    if (t[x][i]) run(t[x][i]);
  update(dfn[x], -1);
}
int main() {
  init();
  dfs(0, 0);
  run(0);
  for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
  return 0;
}
