#include <bits/stdc++.h>
using namespace std;
int tr[800005][26], cnt;
int fail[800005], ed[800005], d[800005];
vector<int> v[800005];
vector<pair<int, int> > g[800005];
void build() {
  queue<int> q;
  memset(fail, 0, sizeof(fail));
  for (int i = 0; i < 26; i++)
    if (tr[0][i]) {
      v[fail[tr[0][i]]].push_back(tr[0][i]);
      q.push(tr[0][i]);
    }
  while (!q.empty()) {
    int k = q.front();
    q.pop();
    for (int i = 0; i < 26; i++) {
      if (tr[k][i]) {
        fail[tr[k][i]] = tr[fail[k]][i];
        v[fail[tr[k][i]]].push_back(tr[k][i]);
        q.push(tr[k][i]);
      } else
        tr[k][i] = tr[fail[k]][i];
    }
  }
}
struct treearray {
  int tre[800005], kk;
  int lwbt(int x) { return x & (-x); }
  int ask(int i) {
    int ans = 0;
    for (; i; i -= lwbt(i)) ans += tre[i];
    return ans;
  }
  void add(int i, int k) {
    for (; i <= kk; i += lwbt(i)) tre[i] += k;
  }
} t;
int in[800005], out[800005], tot, ans[800005];
void dfs1(int x) {
  in[x] = ++tot;
  for (auto y : v[x]) dfs1(y);
  out[x] = tot;
}
void dfs2(int x) {
  t.add(in[x], 1);
  for (auto now : g[x]) {
    ans[now.second] = t.ask(out[now.first]) - t.ask(in[now.first] - 1);
  }
  for (int i = 0; i < 26; i++)
    if (d[tr[x][i]] > d[x]) dfs2(tr[x][i]);
  t.add(in[x], -1);
}
char s[800005];
int main() {
  int n, x, op;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &op);
    x = 0;
    if (op == 2) scanf("%d", &x);
    x = ed[x];
    scanf("%s", s);
    char c = s[0] - 'a';
    if (!tr[x][c]) tr[x][c] = ++cnt, d[cnt] = d[x] + 1;
    ;
    ed[i] = tr[x][c];
  }
  int m;
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d", &x);
    scanf("%s", s);
    int now = 0;
    int len = strlen(s);
    for (int j = 0; j < len; j++) {
      int id = s[j] - 'a';
      if (!tr[now][id]) {
        tr[now][id] = ++cnt;
        d[cnt] = d[now] + 1;
      }
      now = tr[now][id];
    }
    g[ed[x]].push_back(pair<int, int>(now, i));
  }
  t.kk = cnt + 1;
  build();
  dfs1(0);
  dfs2(0);
  for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
}
