Tree
Consider a rooted tree that initially consists of only a single root. The children of each vertex are ordered from left to right. You can perform the following operations on the tree:
Add a leaf to the tree.
Remove a leaf from the tree.
Determine the number of vertices on the path between two leaves.
Determine the number of vertices "under the path" between two leaves.
The vertices "under the path" between leaves u and v are defined as follows. Consider the path u=w_0-w_1-...-w_k=v between them. Identify the vertex w_c where both edges of the path lead to its children. The left edge moves towards vertex u, and the right edge moves towards vertex v. The vertices considered "under the path" include:
All children of w_c that lie between w_{c-1} and w_{c+1}, along with all vertices in their subtrees.
For i = 1, 2, ..., c-1, all children of w_i that are to the right of the child of w_{i-1}, along with all vertices in their subtrees.
For i = c+1, c+2, ..., k-1, all children of w_i that are to the left of the child of w_{i+1}, along with all vertices in their subtrees.
Write a program that processes a sequence of queries to modify the tree by adding and removing vertices, and calculates the answers to queries about the number of vertices on the path and "under the path".
Input
The first line of the input contains a single integer n - the number of queries (0 ≤ n ≤ 300000). Each of the following n lines contains one query. The possible types of queries are:
l x - add a new leaf as the leftmost child of vertex x.
r x - add a new leaf as the rightmost child of vertex x.
a x y - add a new leaf as a child of vertex x, positioned immediately to the right of vertex y; all children of x that were previously to the right of y will be to the right of the new vertex after addition; it is guaranteed that y is a child of x.
d x - delete vertex x. It is guaranteed that at this moment, vertex x is not deleted and is a leaf.
p x y - find the number of vertices on the path between x and y, including these vertices themselves; it is guaranteed that x and y are leaves.
q x y - find the number of vertices "under the path" between x and y, including these vertices themselves; it is guaranteed that x and y are leaves.
Vertices are numbered starting from one in the order they are added through the queries. The root of the tree is numbered 0 and is never considered a leaf.
Output
For each query of type p or q, output the answer on a separate line as you process the queries in order.