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 JAVA):

Solution:
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.util.HashMap;
import java.util.Set;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.math.BigInteger;
import java.util.HashSet;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Egor Kulikov (egor@egork.net)
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		OutputWriter out = new OutputWriter(outputStream);
		TaskC solver = new TaskC();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskC {
	long[] power;

	public void solve(int testNumber, InputReader in, OutputWriter out) {
		int count = in.readInt();
		int[] treeIndex = new int[count];
		int[] vertex = new int[count];
		char[] label = new char[count];
		for (int i = 0; i < count; i++) {
			treeIndex[i] = in.readInt() - 1;
			vertex[i] = in.readInt() - 1;
			label[i] = in.readCharacter();
		}
		power = new long[count + 1];
		power[0] = 1;
		for (int i = 1; i <= count; i++) {
			power[i] = power[i - 1] * 43;
		}
		int firstCount = 0;
		int secondCount = 0;
		for (int i : treeIndex) {
			if (i == 0)
				firstCount++;
			else
				secondCount++;
		}
		long[] hashFirst = new long[firstCount + 1];
		int[] lengthFirst = new int[firstCount + 1];
		long[] hashSecond = new long[secondCount + 1];
		int[] lengthSecond = new int[secondCount + 1];
		int[] parent = new int[secondCount + 1];
		int firstIndex = 0;
		int secondIndex = 0;
		Set<Long> goodHash = new HashSet<Long>();
		for (int i = 0; i < count; i++) {
			if (treeIndex[i] == 0) {
				firstIndex++;
				lengthFirst[firstIndex] = lengthFirst[vertex[i]] + 1;
				hashFirst[firstIndex] = hashFirst[vertex[i]] + power[lengthFirst[vertex[i]]] * label[i];
				goodHash.add(hashFirst[firstIndex]);
			} else {
				secondIndex++;
				lengthSecond[secondIndex] = lengthSecond[vertex[i]] + 1;
				parent[secondIndex] = vertex[i];
				hashSecond[secondIndex] = hashSecond[vertex[i]] * 43 + label[i];
			}
		}
		Counter<Long> firstCounter = new Counter<Long>();
		Counter<Long> secondCounter = new Counter<Long>();
		firstIndex = 0;
		secondIndex = 0;
		long answer = 1;
		for (int i = 0; i < count; i++) {
			if (treeIndex[i] == 0) {
				firstIndex++;
				answer += secondCounter.get(hashFirst[firstIndex]);
				firstCounter.add(hashFirst[firstIndex]);
			} else {
				secondIndex++;
				answer++;
				int current = secondIndex;
				do {
					current = parent[current];
					long curHash = hashSecond[secondIndex] - hashSecond[current] * power[lengthSecond[secondIndex] - lengthSecond[current]];
					if (goodHash.contains(curHash)) {
						answer += firstCounter.get(curHash);
						secondCounter.add(curHash);
					}
				} while (current != 0);
			}
			out.printLine(answer);
		}
	}
}

class InputReader {

	private InputStream stream;
	private byte[] buf = new byte[1024];
	private int curChar;
	private int numChars;

	public InputReader(InputStream stream) {
		this.stream = stream;
	}

	public int read() {
		if (numChars == -1)
			throw new InputMismatchException();
		if (curChar >= numChars) {
			curChar = 0;
			try {
				numChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (numChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}

	public int readInt() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			if (c < '0' || c > '9')
				throw new InputMismatchException();
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public static boolean isSpaceChar(int c) {
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}

	public char readCharacter() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		return (char) c;
	}

	}

class OutputWriter {
	private final PrintWriter writer;

	public OutputWriter(OutputStream outputStream) {
		writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
	}

	public OutputWriter(Writer writer) {
		this.writer = new PrintWriter(writer);
	}

	public void print(Object...objects) {
		for (int i = 0; i < objects.length; i++) {
			if (i != 0)
				writer.print(' ');
			writer.print(objects[i]);
		}
	}

	public void printLine(Object...objects) {
		print(objects);
		writer.println();
	}

	public void close() {
		writer.close();
	}

	}

class Counter<K> extends HashMap<K, Long> {
	public Counter() {
		super();
	}

	public void add(K key) {
		put(key, get(key) + 1);
	}

	public Long get(Object key) {
		if (containsKey(key))
			return super.get(key);
		return 0L;
	}
}