#include <bits/stdc++.h>
using namespace std;
const int maxn = 400050;
const int maxm = 400050;
struct edge {
  int nxt, to;
};
vector<int> g[maxn];
edge e[maxm];
int ch1[maxn][26], ch2[maxn][26];
int head[maxn], st[maxn], ed[maxn];
int pos1[maxn], pos2[maxn];
int f[maxn], val[maxn], cur[maxn];
int c[maxn];
int res[maxn];
char s[maxn];
int n, m, cnt, tot1, tot2, dfn;
void init() {
  memset(head, -1, sizeof(head));
  cnt = tot1 = tot2 = dfn = 0;
}
void add(int u, int v) {
  e[cnt].to = v;
  e[cnt].nxt = head[u];
  head[u] = cnt++;
}
int lowbit(int x) { return x & -x; }
void update(int k, int val) {
  while (k < maxn) {
    c[k] += val;
    k += lowbit(k);
  }
}
int query(int k) {
  int sum = 0;
  while (k) {
    sum += c[k];
    k -= lowbit(k);
  }
  return sum;
}
void insert(char *s, int len_s, int num) {
  int u = 0;
  for (int i = 0; i < len_s; i++) {
    int id = s[i] - 'a';
    if (!ch2[u][id]) ch2[u][id] = ++tot2;
    u = ch2[u][id];
  }
  pos2[num] = u;
}
void build_ac() {
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    int r = f[u];
    for (int i = 0; i < 26; i++) {
      int v = ch2[u][i];
      if (!v) {
        ch2[u][i] = ch2[r][i];
        continue;
      }
      q.push(v);
      f[v] = u ? ch2[r][i] : 0;
      add(f[v], v);
    }
  }
}
void dfs1(int u) {
  st[u] = ++dfn;
  for (int i = head[u]; i != -1; i = e[i].nxt) {
    int v = e[i].to;
    dfs1(v);
  }
  ed[u] = dfn;
}
void dfs2(int u) {
  for (int i = 0, sz = g[u].size(); i < sz; i++) {
    int x = g[u][i];
    res[x] = query(ed[pos2[x]]) - query(st[pos2[x]] - 1);
  }
  for (int i = 0; i < 26; i++) {
    int v = ch1[u][i];
    if (v) {
      cur[v] = ch2[cur[u]][i];
      update(st[cur[v]], 1);
      dfs2(v);
      update(st[cur[v]], -1);
    }
  }
}
void solve() {
  init();
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int op;
    scanf("%d", &op);
    if (op == 1) {
      scanf("%s", s);
      int id = s[0] - 'a';
      if (!ch1[0][id]) ch1[0][id] = ++tot1;
      pos1[i] = ch1[0][id];
    } else if (op == 2) {
      int x;
      scanf("%d %s", &x, s);
      int id = s[0] - 'a';
      if (!ch1[pos1[x]][id]) ch1[pos1[x]][id] = ++tot1;
      pos1[i] = ch1[pos1[x]][id];
    }
  }
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    int x;
    scanf("%d %s", &x, s);
    int len_s = strlen(s);
    insert(s, len_s, i);
    g[pos1[x]].push_back(i);
  }
  build_ac();
  dfs1(0);
  dfs2(0);
  for (int i = 1; i <= m; i++) printf("%d\n", res[i]);
}
int main() {
  solve();
  return 0;
}
