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;
int rd() { return RAND_MAX == 32767 ? ((rand() << 15) ^ rand()) : rand(); }
const int maxn = 100010;
const int md = 1000000007;
int a[20], sa[maxn], rk[maxn], c1[maxn], c2[maxn], h2[maxn], s1[maxn], s2[maxn],
    pw[maxn], dep[maxn], h[maxn][20], d2[maxn][20];
pair<int, int> b[maxn];
struct Rec {
  int t, v, c;
} opt[maxn];
bool cmp(int x, int y) {
  for (int i = 19; i >= 0; i--)
    if (d2[x][i] != -1 && d2[y][i] != -1 && h[x][i] == h[y][i]) {
      x = d2[x][i];
      y = d2[y][i];
    }
  return c2[x] < c2[y];
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &opt[i].t, &opt[i].v);
    while ((opt[i].c = getchar()) && (opt[i].c < 'a' || opt[i].c > 'z'))
      ;
    opt[i].v--;
  }
  pw[0] = 1;
  for (int i = 1; i < n; i++) pw[i] = (long long)pw[i - 1] * 26 % md;
  int m = 1;
  c2[0] = -1;
  h2[0] = 0;
  memset(d2, -1, sizeof(d2));
  for (int i = 0; i < n; i++)
    if (opt[i].t == 2) {
      c2[m] = opt[i].c;
      h2[m] = ((long long)h2[opt[i].v] * 26 + c2[m]) % md;
      d2[m][0] = opt[i].v;
      for (int j = 0;; j++) {
        if (d2[m][j] != -1)
          d2[m][j + 1] = d2[d2[m][j]][j];
        else
          break;
      }
      m++;
    }
  for (int i = 0; i < m; i++) sa[i] = i;
  for (int i = 0; i < m; i++)
    for (int j = 0; j < 20; j++)
      if (d2[i][j] != -1) {
        h[i][j] = (h2[i] - (long long)h2[d2[i][j]] * pw[1 << j]) % md;
        if (h[i][j] < 0) h[i][j] += md;
      }
  sort(sa, sa + m, cmp);
  for (int i = 0; i < m; i++) rk[sa[i]] = i;
  int t = 1, tt = 1;
  c1[0] = -1;
  dep[0] = 0;
  long long res = 1;
  memset(s1, 0, sizeof(s1));
  memset(s2, 0, sizeof(s2));
  for (int k = 1; k <= m; k += k & -k) s2[k]++;
  b[0].first = 0;
  b[0].second = m;
  for (int i = 0; i < n; i++) {
    if (opt[i].t == 1) {
      c1[t] = opt[i].c;
      dep[t] = dep[opt[i].v] + 1;
      int k, lt, rt, mid, head, tail, tot = 0;
      for (int j = 19, cnt = dep[t] - 1; j >= 0; j--)
        if (cnt >= (1 << j)) {
          a[tot++] = j;
          cnt -= 1 << j;
        }
      for (lt = b[opt[i].v].first, rt = b[opt[i].v].second; lt < rt;) {
        mid = (lt + rt) >> 1;
        k = sa[mid];
        for (int j = 0; j < tot; j++) k = d2[k][a[j]];
        if (k == 0)
          lt = mid + 1;
        else
          c1[t] <= c2[k] ? rt = mid : lt = mid + 1;
      }
      head = lt;
      for (rt = b[opt[i].v].second; lt < rt;) {
        mid = (lt + rt) >> 1;
        k = sa[mid];
        for (int j = 0; j < tot; j++) k = d2[k][a[j]];
        if (k == 0)
          lt = mid + 1;
        else
          c1[t] < c2[k] ? rt = mid : lt = mid + 1;
      }
      tail = lt;
      b[t] = make_pair(head, tail);
      for (int k = m - head; k; k -= k & -k) res += s1[k];
      for (int k = m - tail; k; k -= k & -k) res -= s1[k];
      for (int k = head + 1; k <= m; k += k & -k) s2[k]++;
      for (int k = tail + 1; k <= m; k += k & -k) s2[k]--;
      t++;
    } else {
      int whi = rk[tt++];
      for (int k = whi + 1; k; k -= k & -k) res += s2[k];
      for (int k = m - whi; k <= m; k += k & -k) s1[k]++;
    }
    printf("%I64d\n", res);
  }
  return 0;
}