#include <bits/stdc++.h>
using namespace std;
const int oo = 2139063143;
const int N = 1010000;
const int P = 1000000007;
const double eps = 1e-7;
template <typename T>
inline void sc(T &x) {
  x = 0;
  static int p;
  p = 1;
  static char c;
  c = getchar();
  while (!isdigit(c)) {
    if (c == '-') p = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c - 48);
    c = getchar();
  }
  x *= p;
}
template <typename T>
inline void print(T x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}
template <typename T>
inline void pr(T x) {
  print(x), putchar('\n');
}
template <typename T1, typename T2>
void chkmax(T1 &A, T2 B) {
  A = A >= B ? A : B;
}
template <typename T1, typename T2>
void chkmin(T1 &A, T2 B) {
  A = A <= B ? A : B;
}
struct ACAM {
  int son[27], fa;
} t[N];
int totp = 1;
int pos[N];
void ins(char *s, int n, int id) {
  int p = 1;
  for (int i = 1; i <= n; i++) {
    int &v = t[p].son[s[i] - 'a'];
    if (!v) v = ++totp;
    p = v;
  }
  pos[id] = p;
}
vector<int> F[N];
void build() {
  queue<int> q;
  for (int c = 0; c < 26; c++) {
    int &v = t[1].son[c];
    if (!v)
      v = 1;
    else
      t[v].fa = 1, q.push(v);
  }
  while (q.size()) {
    int u = q.front(), f = t[u].fa;
    q.pop();
    F[f].push_back(u);
    for (int c = 0; c < 26; c++) {
      int &v = t[u].son[c];
      if (!v)
        v = t[f].son[c];
      else
        t[v].fa = t[f].son[c], q.push(v);
    }
  }
}
int dfn[N], low[N];
void dfs0(int u) {
  dfn[u] = ++dfn[0];
  for (int v : F[u]) dfs0(v);
  low[u] = dfn[0];
}
struct EDGE {
  int v, w, nx;
} lb[N];
int top[N], tote = 1;
void adde(int u, int v, int w) {
  lb[++tote] = (EDGE){v, w, top[u]}, top[u] = tote;
}
int bit[N];
void add(int x, int d) {
  for (; x <= totp; x += x & -x) bit[x] += d;
}
int query(int x) {
  int ans = 0;
  for (; x; x -= x & -x) ans += bit[x];
  return ans;
}
vector<int> Q[N];
int ans[N];
void dfs1(int u, int p) {
  add(dfn[p], 1);
  for (int i : Q[u]) ans[i] = query(low[pos[i]]) - query(dfn[pos[i]] - 1);
  for (int kb = top[u]; kb; kb = lb[kb].nx) dfs1(lb[kb].v, t[p].son[lb[kb].w]);
  add(dfn[p], -1);
}
char s[N];
int main() {
  int n;
  sc(n);
  for (int i = 1; i <= n; i++) {
    int op, f = n + 1;
    sc(op);
    if (op == 2) sc(f);
    char w[3];
    scanf("%s", w);
    adde(f, i, w[0] - 'a');
  }
  int m;
  sc(m);
  for (int i = 1; i <= m; i++) {
    int id;
    sc(id);
    scanf("%s", s + 1);
    ins(s, strlen(s + 1), i);
    Q[id].push_back(i);
  }
  build();
  dfs0(1);
  dfs1(n + 1, 1);
  for (int i = 1; i <= m; i++) pr(ans[i]);
  return 0;
}
