The input consists of a single test case.
The first line contains a single integer n (1≤n≤2×105), the number of nodes in the tree. The nodes are numbered 1 to n.
Each of the next n lines describes one node, in order of node number. Line i contains two integers vi and pi, where vi (0≤vi≤109) is the value of the node and pi (0≤pi<i) is the index of its parent. Every node's index is strictly greater than its parent's index. Only node 1, the root, has p1=0, since it has no parent. For every other node (i=2,…,n), 1≤pi<i.