Problem: The Smart Beaver from ABBYY has come up with a new developing game for children. The Beaver thinks that this game will help children to understand programming better.

The main object of the game is finite rooted trees, each of their edges contains some lowercase English letter. Vertices on any tree are always numbered sequentially from 1 to m, where m is the number of vertices in the tree. Before describing the actual game, let's introduce some definitions.

We'll assume that the sequence of vertices with numbers v1, v2, ..., vk (k ≥ 1) is a forward path, if for any integer i from 1 to k - 1 vertex vi is a direct ancestor of vertex vi + 1. If we sequentially write out all letters from the the edges of the given path from v1 to vk, we get some string (k = 1 gives us an empty string). We'll say that such string corresponds to forward path v1, v2, ..., vk.

We'll assume that the sequence of tree vertices with numbers v1, v2, ..., vk (k ≥ 1) is a backward path if for any integer i from 1 to k - 1 vertex vi is the direct descendant of vertex vi + 1. If we sequentially write out all the letters from the edges of the given path from v1 to vk, we get some string (k = 1 gives us an empty string). We'll say that such string corresponds to backward path v1, v2, ..., vk.

Now let's describe the game that the Smart Beaver from ABBYY has come up with. The game uses two rooted trees, each of which initially consists of one vertex with number 1. The player is given some sequence of operations. Each operation is characterized by three values (t, v, c) where: 

  * t is the number of the tree on which the operation is executed (1 or 2); 
  * v is the vertex index in this tree (it is guaranteed that the tree contains a vertex with this index); 
  * c is a lowercase English letter. 



The actual operation is as follows: vertex v of tree t gets a new descendant with number m + 1 (where m is the current number of vertices in tree t), and there should be letter c put on the new edge from vertex v to vertex m + 1.

We'll say that an ordered group of three integers (i, j, q) is a good combination if: 

  * 1 ≤ i ≤ m1, where m1 is the number of vertices in the first tree; 
  * 1 ≤ j, q ≤ m2, where m2 is the number of vertices in the second tree; 
  * there exists a forward path v1, v2, ..., vk such that v1 = j and vk = q in the second tree; 
  * the string that corresponds to the forward path in the second tree from vertex j to vertex q equals the string that corresponds to the backward path in the first tree from vertex i to vertex 1 (note that both paths are determined uniquely). 



Your task is to calculate the number of existing good combinations after each operation on the trees.

Input

The first line contains integer n — the number of operations on the trees. Next n lines specify the operations in the order of their execution. Each line has form "t v c", where t is the number of the tree, v is the vertex index in this tree, and c is a lowercase English letter.

To get the full points for the first group of tests it is sufficient to solve the problem with 1 ≤ n ≤ 700.

To get the full points for the second group of tests it is sufficient to solve the problem with 1 ≤ n ≤ 7000.

To get the full points for the third group of tests it is sufficient to solve the problem with 1 ≤ n ≤ 100000.

Output

Print exactly n lines, each containing one integer — the number of existing good combinations after the corresponding operation from the input.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

Examples

Input

5
1 1 a
2 1 a
1 2 b
2 1 b
2 3 a


Output

1
3
3
4
7

Note

After the first operation the only good combination was (1, 1, 1). After the second operation new good combinations appeared, (2, 1, 2) and (1, 2, 2). The third operation didn't bring any good combinations. The fourth operation added good combination (1, 3, 3). Finally, the fifth operation resulted in as much as three new good combinations — (1, 4, 4), (2, 3, 4) and (3, 1, 4).

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
struct qry {
  int id, fa, x;
  char ch;
} q[100000];
struct nod {
  int ch[26], ct, mp;
  nod() {
    for (int i = 0; i < 26; i++) ch[i] = -1;
    ct = 0;
    mp = -1;
  }
} cr[100100];
const long long M = 2000000063LL, R = 1629629681LL;
vector<int> tt[100100], tc[100100];
vector<pair<long long, int> > hp;
long long hsh[100000], pw[100000], rv[100000], tr[400400], tg[400400],
    ct[100100], ans[100100];
int sz[100100], fd[100100], fa[100100][17], fa1[100000], tot = 0, lst[100100],
                                                         rt[100100];
int m1 = 1, m2 = 1, dp[100100], dep[100100], dfn[100100], fnd[100100], cnt = 1,
    org[100100];
void dfs(int x, long long h) {
  h %= M;
  sz[x] = 1;
  hp.push_back(make_pair(h, x));
  for (int i = 0; i < 26; i++)
    if (cr[x].ch[i] != -1) {
      dp[cr[x].ch[i]] = dp[x] + 1;
      dfs(cr[x].ch[i], h * 27 + i + 1);
      sz[x] += sz[cr[x].ch[i]];
      if (cr[x].mp == -1 || sz[cr[x].mp] < sz[cr[x].ch[i]])
        cr[x].mp = cr[x].ch[i];
      fa1[cr[x].ch[i]] = x;
    }
}
void dfs1(int x) {
  for (int i = 0; i < tt[x].size(); i++) {
    dep[tt[x][i]] = dep[x] + 1;
    hsh[tt[x][i]] = (tc[x][i] - 'a' + 1) * pw[dep[x]] + hsh[x];
    hsh[tt[x][i]] %= M;
    dfs1(tt[x][i]);
  }
}
void mpt(int x) {
  org[tot] = x;
  dfn[x] = tot++;
  if (cr[x].mp == -1) return;
  rt[cr[x].mp] = rt[x];
  mpt(cr[x].mp);
  for (int i = 0; i < 26; i++)
    if (cr[x].ch[i] != cr[x].mp && cr[x].ch[i] != -1) {
      rt[cr[x].ch[i]] = cr[x].ch[i];
      mpt(cr[x].ch[i]);
    }
}
void add(int x, int t) {
  while (x <= tot) {
    ct[x] += t;
    x += (x & -x);
  }
}
long long query(int x) {
  long long ans = 0;
  while (x) {
    ans += ct[x];
    x -= (x & -x);
  }
  return ans;
}
void push_down(int i, int l, int r) {
  if (tg[i] == 0) return;
  int mid = (l + r) >> 1;
  tr[i * 2] += tg[i] * (query(mid) - query(l));
  tg[i * 2] += tg[i];
  tr[i * 2 + 1] += tg[i] * (query(r) - query(mid));
  tg[i * 2 + 1] += tg[i];
  tg[i] = 0;
}
void push_up(int i) { tr[i] = tr[i * 2] + tr[i * 2 + 1]; }
void add(int i, int l, int r, int l0, int r0, int w) {
  if (l >= l0 && r <= r0) {
    tr[i] += w * (query(r) - query(l));
    tg[i] += w;
    return;
  }
  push_down(i, l, r);
  int mid = (l + r) >> 1;
  if (mid > l0) add(i * 2, l, mid, l0, r0, w);
  if (mid < r0) add(i * 2 + 1, mid, r, l0, r0, w);
  push_up(i);
}
void add(int i, int l, int r, int x, int w) {
  if (r - l == 1) {
    tr[i] += w;
    return;
  }
  push_down(i, l, r);
  int mid = (l + r) >> 1;
  if (mid > x)
    add(i * 2, l, mid, x, w);
  else
    add(i * 2 + 1, mid, r, x, w);
  push_up(i);
}
long long query(int i, int l, int r, int x) {
  if (r - l == 1) return tr[i];
  push_down(i, l, r);
  int mid = (l + r) >> 1;
  if (mid > x)
    return query(i * 2, l, mid, x);
  else
    return query(i * 2 + 1, mid, r, x);
}
int main() {
  int n;
  scanf("%d", &n);
  pw[0] = 1;
  rv[0] = 1;
  for (int i = 1; i <= n; i++)
    pw[i] = pw[i - 1] * 27LL, pw[i] %= M, rv[i] = rv[i - 1] * R, rv[i] %= M;
  cr[0].ct = 1;
  for (int i = 0; i < n; i++) {
    char c[10];
    scanf("%d%d%s", &q[i].id, &q[i].fa, c);
    q[i].fa--;
    q[i].x = (q[i].id == 1) ? m1++ : m2++;
    q[i].ch = c[0];
    if (q[i].id == 1) {
      q[i].fa = fnd[q[i].fa];
      if (cr[q[i].fa].ch[q[i].ch - 'a'] == -1) {
        cr[q[i].fa].ch[q[i].ch - 'a'] = q[i].x;
        fnd[q[i].x] = q[i].x;
        cr[cr[q[i].fa].ch[q[i].ch - 'a']].ct = 1;
      } else {
        cr[cr[q[i].fa].ch[q[i].ch - 'a']].ct++;
        fnd[q[i].x] = cr[q[i].fa].ch[q[i].ch - 'a'];
      }
    } else {
      tt[q[i].fa].push_back(q[i].x);
      tc[q[i].fa].push_back(q[i].ch);
      fa[q[i].x][0] = q[i].fa;
    }
  }
  fa1[0] = -1;
  dfs(0, 0);
  dfs1(0);
  sort(hp.begin(), hp.end());
  rt[0] = 0;
  mpt(0);
  for (int j = 1; j < 17; j++)
    for (int i = 0; i < m2; i++) fa[i][j] = fa[fa[i][j - 1]][j - 1];
  for (int i = 0; i < tot; i++) {
    int j = i + 1;
    for (int k = 0; k < (j & -j); k++) {
      ct[j] += cr[org[i - k]].ct;
    }
  }
  for (int i = 0; i < m2; i++) {
    int y = i;
    for (int j = 16; j >= 0; j--) {
      int t = fa[y][j];
      long long tmp = (hsh[i] - hsh[t]) * rv[dep[t]];
      tmp %= M;
      tmp += M;
      tmp %= M;
      vector<pair<long long, int> >::iterator it =
          lower_bound(hp.begin(), hp.end(), make_pair(tmp, -1));
      if (it < hp.end() && it->first == tmp) {
        y = t;
        lst[i] = it->second;
      }
    }
    int now = lst[i];
    while (now != -1) {
      int t = rt[now];
      add(1, 0, tot, dfn[t], dfn[now] + 1, 1);
      now = fa1[t];
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    ans[i] = tr[1];
    if (q[i].id == 1) {
      long long t = query(1, 0, tot, dfn[fnd[q[i].x]]) /
                    (query(dfn[fnd[q[i].x]] + 1) - query(dfn[fnd[q[i].x]]));
      add(1, 0, tot, dfn[fnd[q[i].x]], -t);
      add(dfn[fnd[q[i].x]] + 1, -1);
    } else {
      int now = lst[q[i].x];
      while (now != -1) {
        int t = rt[now];
        add(1, 0, tot, dfn[t], dfn[now] + 1, -1);
        now = fa1[t];
      }
    }
  }
  for (int i = 0; i < n; i++) printf("%I64d\n", ans[i]);
  return 0;
}