Farmer John has conducted an extensive study of the evolution of different cow breeds. The result is a rooted tree with N (2≤N≤105) nodes labeled 1…N, each node corresponding to a cow breed. For each i∈\[2,N], the parent of node i is node p_i (1≤p_i\<i), meaning that breed i evolved from breed p_i. A node j is called an ancestor of node i if j=p_i or j is an ancestor of p_i.
Every node i in the tree is associated with a breed having an integer number of spots s_i. The "imbalance" of the tree is defined to be the maximum of ∣s_i−s_j∣ over all pairs of nodes (i,j) such that j is an ancestor of i.
Farmer John doesn't know the exact value of s_i for each breed, but he knows lower and upper bounds on these values. Your job is to assign an integer value of s_i∈\[l_i,r_i] (0≤l_i≤r_i≤109) to each node such that the imbalance of the tree is minimized.
The first line contains T (1≤T≤10), the number of independent test cases to be solved, and an integer B∈0,1.
Each test case starts with a line containing N, followed by N−1 integers p_2,p_3,…,p_N.
The next N lines each contain two integers l_i and r_i.
It is guaranteed that the sum of N over all test cases does not exceed 105.
For each test case, output one or two lines, depending on the value of B.
The first line for each test case should contain the minimum imbalance.
If B=1, then print an additional line with N space-separated integers s_1,s_2,…,s_N containing an assignment of spots that achieves the above imbalance. Any valid assignment will be accepted.