#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const double pi = acos(-1.0);
template <class T>
int chkmin(T &a, T b) {
  return a > b ? a = b, 1 : 0;
}
template <class T>
int chkmax(T &a, T b) {
  return a < b ? a = b, 1 : 0;
}
template <class T>
T sqr(T a) {
  return a * a;
}
template <class T>
T mmin(T a, T b) {
  return a < b ? a : b;
}
template <class T>
T mmax(T a, T b) {
  return a > b ? a : b;
}
template <class T>
T aabs(T a) {
  return a < 0 ? -a : a;
}
template <class T>
int dcmp(T a, T b) {
  return a > b;
}
template <int *a>
int cmp_a(int first, int second) {
  return a[first] < a[second];
}
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c,
                                       qu[55];
int f, qr;
inline void flush() {
  fwrite(obuf, 1, oS - obuf, stdout);
  oS = obuf;
}
inline void putc(char first) {
  *oS++ = first;
  if (oS == oT) flush();
}
inline bool read(signed &first) {
  for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                              (iS == iT ? EOF : *iS++))
                            : *iS++);
       c < '0' || c > '9';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    if (c == '-')
      f = -1;
    else if (c == EOF)
      return 0;
  for (first = 0; c <= '9' && c >= '0';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    first = first * 10 + (c & 15);
  first *= f;
  return 1;
}
inline bool read(long long &first) {
  for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                              (iS == iT ? EOF : *iS++))
                            : *iS++);
       c < '0' || c > '9';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    if (c == '-')
      f = -1;
    else if (c == EOF)
      return 0;
  for (first = 0; c <= '9' && c >= '0';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    first = first * 10 + (c & 15);
  first *= f;
  return 1;
}
inline bool read(char &first) {
  while ((first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                               (iS == iT ? EOF : *iS++))
                            : *iS++)) == '\n' ||
         first == ' ' || first == '\r')
    if (first == EOF) return 0;
  return 1;
}
inline bool read(char *first) {
  while ((*first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                                (iS == iT ? EOF : *iS++))
                             : *iS++)) == '\n' ||
         *first == ' ' || *first == '\r')
    if (*first == EOF) return 0;
  while (!(*first == '\n' || *first == ' ' || *first == '\r' || *first == EOF))
    *(++first) = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                              (iS == iT ? EOF : *iS++))
                           : *iS++);
  *first = 0;
  return 1;
}
template <typename A, typename... B>
inline bool read(A &first, B &...second) {
  return read(first) && read(second...);
}
inline bool write(signed first) {
  if (!first) putc('0');
  if (first < 0) putc('-'), first = -first;
  while (first) qu[++qr] = first % 10 + '0', first /= 10;
  while (qr) putc(qu[qr--]);
  return 0;
}
inline bool write(long long first) {
  if (!first) putc('0');
  if (first < 0) putc('-'), first = -first;
  while (first) qu[++qr] = first % 10 + '0', first /= 10;
  while (qr) putc(qu[qr--]);
  return 0;
}
inline bool write(char first) {
  putc(first);
  return 0;
}
inline bool write(const char *first) {
  while (*first) {
    putc(*first);
    ++first;
  }
  return 0;
}
inline bool write(char *first) {
  while (*first) {
    putc(*first);
    ++first;
  }
  return 0;
}
template <typename A, typename... B>
inline bool write(A first, B... second) {
  return write(first) || write(second...);
}
struct Flusher_ {
  ~Flusher_() { flush(); }
} io_flusher_;
}  // namespace io
using io ::putc;
using io ::read;
using io ::write;
int inf;
struct _init_ {
  _init_() { memset(&inf, 0x3f, sizeof(inf)); }
};
_init_ ___INIT__;
int to[400005][26], is[400005], t = 1;
char s[400005];
vector<pair<int, int> > q[400005];
int to2[400005][26], fail[400005], tt = 1;
vector<int> son[400005];
int ans[400005];
bool hv[400005];
int insert(char *s) {
  int at = 1;
  while (*s) {
    if (!to2[at][*s - 'a']) to2[at][*s - 'a'] = ++tt;
    at = to2[at][*s - 'a'];
    ++s;
  }
  hv[at] = 1;
  return at;
}
int qu[400005], *l, *r;
int sum[400005];
int dfn[400005], clk, low[400005];
void Add(int first, int second) {
  for (; first <= tt; first += first & -first) sum[first] += second;
}
int Sum(int first) {
  int second = 0;
  for (; first; first ^= first & -first) second += sum[first];
  return second;
}
void dfs(int first) {
  dfn[first] = clk;
  ++clk;
  for (auto i : son[first]) dfs(i);
  low[first] = clk;
}
void Draw_ACAM(int first, int id) {
  s[id] = 0;
  for (int i = 0; i < 26; ++i)
    if (to2[first][i]) {
      s[id] = i + 'a';
      Draw_ACAM(to2[first][i], id + 1);
    }
}
void Build_ACAM() {
  l = (r = qu) + 1;
  for (int i = 0; i < 26; ++i)
    if (to2[1][i]) {
      *(++r) = to2[1][i];
      fail[to2[1][i]] = 1;
    }
  for (; l <= r; ++l) {
    int cnt = 0;
    for (int j = 0; j < 26; ++j)
      if (to2[*l][j]) ++cnt;
    for (int i = fail[*l]; i && cnt; i = fail[i]) {
      for (int j = 0; j < 26; ++j)
        if (to2[i][j] && !fail[to2[*l][j]] && to2[*l][j]) {
          --cnt;
          fail[to2[*l][j]] = to2[i][j];
        }
    }
    for (int j = 0; j < 26; ++j)
      if (to2[*l][j] && !fail[to2[*l][j]]) fail[to2[*l][j]] = 1;
    for (int j = 0; j < 26; ++j)
      if (to2[*l][j]) *(++r) = to2[*l][j];
  }
  for (int i = 0; i < 26; ++i)
    if (!to2[1][i]) to2[1][i] = 1;
  for (l = qu + 1; l <= r; ++l) {
    son[fail[*l]].push_back(*l);
    for (int i = 0; i < 26; ++i)
      if (!to2[*l][i]) to2[*l][i] = to2[fail[*l]][i];
    hv[*l] |= hv[fail[*l]];
  }
  dfs(1);
}
int at = 1;
void Solve(int first) {
  for (auto i : q[first]) ans[i.second] = Sum(low[i.first]) - Sum(dfn[i.first]);
  for (int i = 0; i < 26; ++i)
    if (to[first][i]) {
      int oat = at;
      at = to2[at][i];
      if (hv[at]) Add(dfn[at] + 1, 1);
      Solve(to[first][i]);
      if (hv[at]) Add(dfn[at] + 1, -1);
      at = oat;
    }
}
signed main() {
  int n, tp, m;
  char c;
  read(n);
  is[0] = 1;
  for (int i = 1; i <= n; ++i) {
    read(tp);
    int fa = 0;
    if (tp == 2) read(fa);
    read(c);
    if (!to[is[fa]][c - 'a']) to[is[fa]][c - 'a'] = ++t;
    is[i] = to[is[fa]][c - 'a'];
  }
  read(m);
  int id;
  for (int i = 1; i <= m; ++i) {
    read(id, s);
    q[is[id]].push_back(make_pair(insert(s), i));
  }
  Build_ACAM();
  Solve(1);
  for (int i = 1; i <= m; ++i) write(ans[i], '\n');
  return 0;
}
