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;
const int base = 987654323;
const int maxn = 100005;
struct ljb {
  int num, dest;
  ljb *next;
} * p, *head[maxn], *tail[maxn];
long long bzhash[maxn][18], powc[maxn], hasha[maxn];
int bzfa[maxn][18], dis1[maxn], dis2[maxn], sa[maxn], bak[maxn], f1[maxn],
    f2[maxn], flag[maxn], which[maxn], fa1[maxn], fa2[maxn], fac1[maxn],
    fac2[maxn];
pair<int, int> qj[maxn];
int q, lc, rc, o1, o2, o, e, father;
char ch;
bool cmp(int xc, int yc) {
  for (int i = 17; i >= 0; i--) {
    if ((bzfa[xc][i] != 0) && (bzfa[yc][i] != 0))
      if (bzhash[xc][i] == bzhash[yc][i]) {
        xc = bzfa[xc][i];
        yc = bzfa[yc][i];
      }
  }
  if (fa2[xc] + fa2[yc] == 0) return false;
  if (fa2[xc] == 0) return true;
  if (fa2[yc] == 0) return false;
  return fac2[xc] < fac2[yc];
}
void mpush(int x, int y, int z) {
  ljb *p;
  p = new ljb;
  p->dest = y;
  p->num = z;
  p->next = 0;
  tail[x]->next = p;
  tail[x] = p;
}
int getfa(int s, int k) {
  for (int i = 17; i >= 0; i--)
    if (k >= (1 << i)) {
      s = bzfa[s][i];
      k -= 1 << i;
    }
  return s;
}
void dfs(int s, int left, int right) {
  qj[s] = make_pair(left, right);
  int c = 0, ll, rr, mm, lc, rc, nowc;
  ljb *p = head[s]->next;
  while (p != 0) {
    ll = left;
    rr = right;
    while (ll <= rr) {
      mm = (ll + rr) / 2;
      nowc = getfa(sa[mm], dis1[s] - 1);
      if (fac2[nowc] >= p->num)
        rr = mm - 1;
      else
        ll = mm + 1;
    }
    lc = ll;
    ll = left;
    rr = right;
    while (ll <= rr) {
      mm = (ll + rr) / 2;
      nowc = getfa(sa[mm], dis1[s] - 1);
      if (fac2[nowc] <= p->num)
        ll = mm + 1;
      else
        rr = mm - 1;
    }
    rc = rr;
    dfs(p->dest, lc, rc);
    p = p->next;
  }
}
void plus1(int s) {
  while (s <= o2) {
    f1[s]++;
    s += s & (-s);
  }
}
int gs1(int s) {
  int ss = 0;
  while (s > 0) {
    ss += f1[s];
    s -= s & (-s);
  }
  return ss;
}
int getsum1(int l, int r) { return gs1(r) - gs1(l - 1); }
void pl2(int s, int flag) {
  while (s <= o2) {
    f2[s] += flag;
    s += s & (-s);
  }
}
void plus2(int l, int r) {
  pl2(l, 1);
  pl2(r + 1, -1);
}
int gettime2(int s) {
  int ss = 0;
  while (s > 0) {
    ss += f2[s];
    s -= s & (-s);
  }
  return ss;
}
int main() {
  powc[0] = 1;
  for (int i = 1; i <= maxn; i++) powc[i] = powc[i - 1] * base;
  o1 = 1;
  o2 = 1;
  scanf("%d\n", &q);
  for (int i = 1; i <= q; i++) {
    scanf("%d %d %c\n", &flag[i], &father, &ch);
    if (flag[i] == 1) {
      ++o1;
      fa1[o1] = father;
      fac1[o1] = ch - 96;
      which[i] = o1;
    } else {
      ++o2;
      fa2[o2] = father;
      fac2[o2] = ch - 96;
      which[i] = o2;
    }
  }
  for (int i = 1; i <= o2; i++) {
    if (i == 1)
      bzhash[i][0] = 0;
    else
      bzhash[i][0] = fac2[i];
    if (i == 1)
      bzfa[i][0] = 0;
    else
      bzfa[i][0] = fa2[i];
    o = 1;
    e = fa2[i];
    while (bzfa[e][o - 1] != 0) {
      bzhash[i][o] = bzhash[i][o - 1] * powc[1 << (o - 1)] + bzhash[e][o - 1];
      bzfa[i][o] = bzfa[e][o - 1];
      e = bzfa[i][o];
      o++;
    }
  }
  dis1[1] = 1;
  for (int i = 2; i <= o1; i++) dis1[i] = dis1[fa1[i]] + 1;
  dis2[1] = 1;
  for (int i = 2; i <= o2; i++) dis2[i] = dis2[fa2[i]] + 1;
  for (int i = 1; i <= o2; i++) sa[i] = i;
  sort(sa + 1, sa + o2 + 1, cmp);
  for (int i = 1; i <= o2; i++) bak[sa[i]] = i;
  for (int i = 0; i <= o1; i++) {
    p = new ljb;
    p->dest = 0;
    p->num = 0;
    p->next = 0;
    head[i] = p;
    tail[i] = p;
  }
  for (int i = 2; i <= o1; i++) mpush(fa1[i], i, fac1[i]);
  dfs(1, 1, o2);
  memset(f1, 0, sizeof(f1));
  memset(f2, 0, sizeof(f2));
  long long ans = 1, nt;
  plus2(1, o2);
  plus1(1);
  for (int i = 1; i <= q; i++) {
    if (flag[i] == 1) {
      lc = qj[which[i]].first;
      rc = qj[which[i]].second;
      ans += getsum1(lc, rc);
      plus2(lc, rc);
    } else {
      lc = bak[which[i]];
      ans += gettime2(lc);
      plus1(lc);
    }
    printf("%I64d\n", ans);
  }
  return 0;
}