#include <bits/stdc++.h>
using namespace std;
const int K = 26;
const int N = 4e5 + 111;
int n, m;
int triePos[N];
vector<int> trieId[N];
vector<int> trieQuery[N];
int queryPos[N];
vector<int> G[N];
int res[N];
class ACAutomaton {
 public:
  class Node {
   public:
    int next[K];
    bool leaf = 0;
    int parent = -1;
    char pch;
    int link = -1;
    int go[K];
    Node(int parent = -1, char pch = '#') : parent(parent), pch(pch) {
      memset(next, -1, sizeof(next));
      memset(go, -1, sizeof(go));
    }
    int& operator[](int k) { return next[k]; }
  };
  vector<Node> Tree;
  ACAutomaton() { Tree.emplace_back(); }
  unsigned int size() { return Tree.size(); }
  int add_string(const string& s) {
    int v = 0;
    for (char ch : s) {
      int c = ch - 'a';
      if (Tree[v][c] == -1) {
        Tree[v][c] = (int)Tree.size();
        Tree.emplace_back(v, ch);
      }
      v = Tree[v][c];
    }
    Tree[v].leaf = 1;
    return v;
  }
  int go(int v, char ch) {
    int c = ch - 'a';
    if (Tree[v].go[c] == -1) {
      if (Tree[v][c] != -1)
        Tree[v].go[c] = Tree[v][c];
      else
        Tree[v].go[c] = (v == 0 ? 0 : go(get_link(v), ch));
    }
    return Tree[v].go[c];
  }
  int get_link(int v) {
    if (Tree[v].link == -1) {
      if (v == 0 || Tree[v].parent == 0)
        Tree[v].link = 0;
      else
        Tree[v].link = go(get_link(Tree[v].parent), Tree[v].pch);
    }
    return Tree[v].link;
  }
};
class Trie {
 public:
  class Node {
   public:
    int next[K];
    Node() { memset(next, -1, sizeof(next)); }
    int& operator[](int k) { return next[k]; }
  };
  vector<Node> Tree;
  Trie() { Tree.emplace_back(); }
  int add_letter(int v, char ch) {
    int c = ch - 'a';
    if (Tree[v][c] == -1) {
      Tree[v][c] = (int)Tree.size();
      Tree.emplace_back();
    }
    return Tree[v][c];
  }
};
class FenwichTree {
 private:
  int bit[N];
  int tm, tin[N], tout[N];

 public:
  FenwichTree() {
    tm = 1;
    memset(bit, 0, sizeof(bit));
    memset(tin, 0, sizeof(tin));
    memset(tout, 0, sizeof(tout));
  }
  void visit(int u) {
    tin[u] = tm++;
    for (auto v : G[u]) visit(v);
    tout[u] = tm;
  }
  void update(int u, int val) {
    for (int i = tin[u]; i < N; i += i & -i) bit[i] += val;
  }
  int get(int x) {
    int sum = 0;
    for (int i = x; i > 0; i -= i & -i) sum += bit[i];
    return sum;
  }
  int sum(int u) { return get(tout[u] - 1) - get(tin[u] - 1); }
};
ACAutomaton ac = ACAutomaton();
Trie trie = Trie();
FenwichTree ft = FenwichTree();
void update(int trieNode, int acNode) {
  ft.update(acNode, 1);
  for (auto id : trieId[trieNode]) {
    for (auto q : trieQuery[id]) {
      res[q] = ft.sum(queryPos[q]);
    }
  }
  for (char ch = 'a'; ch <= 'z'; ++ch) {
    int c = ch - 'a';
    if (trie.Tree[trieNode][c] != -1) {
      update(trie.Tree[trieNode][c], ac.go(acNode, ch));
    }
  }
  ft.update(acNode, -1);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    int type, u = 0;
    char ch;
    cin >> type;
    if (type == 1)
      cin >> ch;
    else
      cin >> u >> ch;
    triePos[i] = trie.add_letter(triePos[u], ch);
    trieId[triePos[i]].push_back(i);
  }
  cin >> m;
  for (int i = 1; i <= m; ++i) {
    int u;
    string s;
    cin >> u >> s;
    trieQuery[u].push_back(i);
    queryPos[i] = ac.add_string(s);
  }
  for (int i = 1; i < (int)ac.size(); ++i) G[ac.get_link(i)].push_back(i);
  ft.visit(0);
  update(0, 0);
  for (int i = 1; i <= m; ++i) cout << res[i] << '\n';
  return 0;
}
