#include <bits/stdc++.h>
using namespace std;
const int N = 5e5;
int trie[N][26], tot = 1;
vector<int> g[N];
int in[N], out[N], cur;
void dfsAC(int u) {
  in[u] = ++cur;
  for (auto v : g[u]) dfsAC(v);
  out[u] = cur;
}
int insert(string &s) {
  int p = 1;
  for (char k : s) {
    int ch = k - 'a';
    if (trie[p][ch] == 0) trie[p][ch] = ++tot;
    p = trie[p][ch];
  }
  return p;
}
int fail[N];
void build() {
  for (int i = 0; i < 26; ++i) trie[0][i] = 1;
  queue<int> q;
  q.push(1);
  int u;
  while (!q.empty()) {
    u = q.front(), q.pop();
    for (int i = 0; i < 26; ++i) {
      if (trie[u][i])
        fail[trie[u][i]] = trie[fail[u]][i], q.push(trie[u][i]);
      else
        trie[u][i] = trie[fail[u]][i];
    }
  }
  for (int i = 2; i <= tot; i++) g[fail[i]].push_back(i);
  dfsAC(1);
}
string t[N];
long long cnt[N];
int match[N];
int n, q;
int op[N], x;
char a[N];
vector<int> g1[N];
vector<int> g2[N];
long long BIT[N];
long long sum(int x) {
  long long sum = 0;
  for (; x != 0; x -= x & (-x)) sum += BIT[x];
  return sum;
}
void update(int x, int n, int num) {
  for (; x <= n; x += x & (-x)) BIT[x] += num;
}
void dfs(int u, char k, int p) {
  p = trie[p][k - 'a'];
  update(in[p], cur, 1);
  for (auto v : g1[u]) dfs(v, a[v], p);
  for (auto v : g2[u]) cnt[v] += sum(out[match[v]]) - sum(in[match[v]] - 1);
  update(in[p], N - 1, -1);
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> op[i];
    if (op[i] == 2) {
      cin >> x;
      g1[x].push_back(i);
    }
    cin >> a[i];
  }
  cin >> q;
  for (int i = 1; i <= q; i++) {
    cin >> x >> t[i];
    match[i] = insert(t[i]);
    g2[x].push_back(i);
  }
  build();
  for (int i = 1; i <= n; i++)
    if (op[i] == 1) dfs(i, a[i], 1);
  for (int i = 1; i <= q; i++) cout << cnt[i] << "\n";
}
