Firm

No attempts yetTime limit2sMemory limit512 MB

Problem

In a fast-growing firm, new employees are hired all the time. Every new employee pp is given exactly one direct superior. That superior's own superiors (direct and indirect) automatically become indirect superiors of pp as well.

We call the direct superior of pp a superior of degree 00. The superior of a degree-00 superior is a superior of degree 11, and in general the superior of a degree-kk superior is a superior of degree k+1k+1. So each employee is a subordinate of their direct superior and of every higher-degree superior above them. Together this forms a single hierarchy of all employees, with the company's founder at the very top.

The company keeps a full history of everyone who has joined since it was founded. From time to time an employee wants to know, for a given degree kk, how many current employees have them as a superior of exactly degree kk. Write a program that answers these questions.

Input

The first line contains an integer nn (1n1051 \le n \le 10^5), the number of events. The next nn lines each describe one event, in chronological order.

A hiring event is the character Z followed by two integers pp and ss (2p1052 \le p \le 10^5, and pp is distinct across all hires): pp is the number of the new employee and ss is the number of the direct superior. ss is always the number of an employee who is already working at the company. The founder has number 11.

A question event is the character P followed by two integers qq and kk (1q1051 \le q \le 10^5, 0k1050 \le k \le 10^5): employee qq asks how many current employees have qq as a superior of exactly degree kk.

Before the first event, the founder is the only person at the firm.

Output

For each question event, print a single line with one integer: the number of current employees for whom qq is a superior of exactly degree kk.

Hint