#include <bits/stdc++.h>
using namespace std;
vector<int> g[800021], op[800021];
vector<pair<int, int> > lz[800021];
int ch[800021][26], fail[800021], n, m, ans[800021], in[800021], out[800021];
int sum[800021], tot, cnt, now[800021], vis[800021], w[800021];
queue<int> q;
void update(int x, int add) {
  for (; x <= cnt; x += x & (-x)) sum[x] += add;
}
int query(int x) {
  int ans = 0;
  for (; x > 0; x -= x & (-x)) ans += sum[x];
  return ans;
}
int insert(char *s) {
  int len = strlen(s), rt = 0;
  for (int i = 0; i < len; i++) {
    if (!ch[rt][s[i] - 'a']) ch[rt][s[i] - 'a'] = ++tot;
    rt = ch[rt][s[i] - 'a'];
  }
  return rt;
}
void build() {
  q.push(0);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < 26; i++) {
      if (!ch[u][i]) {
        ch[u][i] = u == 0 ? 0 : ch[fail[u]][i];
      } else {
        fail[ch[u][i]] = u == 0 ? 0 : ch[fail[u]][i];
        q.push(ch[u][i]);
      }
    }
  }
  for (int i = 1; i <= tot; i++) op[fail[i]].push_back(i);
}
void dfs1(int u) {
  in[u] = ++cnt;
  for (auto v : op[u]) dfs1(v);
  out[u] = cnt;
}
void dfs2(int u, int pos) {
  vis[u] = 1;
  update(in[pos], 1);
  for (auto v : lz[u])
    ans[v.first] = query(out[v.second]) - query(in[v.second] - 1);
  for (auto v : g[u]) {
    dfs2(v, ch[pos][w[v]]);
  }
  update(in[pos], -1);
}
char s[800021];
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int a, b, i = 1; i <= n; i++) {
    cin >> a;
    if (a == 1) {
      cin >> s;
      w[i] = s[0] - 'a';
    } else {
      cin >> b >> s;
      w[i] = s[0] - 'a';
      g[b].push_back(i);
    }
  }
  cin >> m;
  for (int a, i = 1; i <= m; i++) {
    cin >> a >> s;
    lz[a].push_back(make_pair(i, insert(s)));
  }
  build();
  dfs1(0);
  for (int i = 1; i <= n; i++) {
    if (vis[i]) continue;
    dfs2(i, ch[0][w[i]]);
  }
  for (int i = 1; i <= m; i++) cout << ans[i] << endl;
  return 0;
}
