C. Leyline Resonance
time limit per test
2 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

You are given a tree consisting of $$$n$$$ vertices, rooted at vertex $$$1$$$. Each vertex $$$i$$$ has an initial value $$$a_i$$$. For each vertex $$$i$$$ from $$$2$$$ to $$$n$$$, there is a directed edge from its parent $$$p_i$$$ to $$$i$$$ with a weight $$$w_i$$$.

You need to assign a real number $$$x_i$$$ to each vertex $$$i$$$ to minimize the total cost. The total cost is defined as the sum of the adjustment costs for all vertices and the penalty costs for all edges:

$$$$$$Cost = \sum_{i=1}^n |x_i - a_i| + \sum_{i=2}^n w_i \cdot \max(0, x_{p_i} - x_i)$$$$$$

Print the minimum possible total cost. It can be proven that the minimum cost is always an integer.

Input

The first line contains a single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — the number of test cases.

The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of vertices in the tree.

The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the initial values of the vertices.

Then $$$n - 1$$$ lines follow. The $$$i$$$-th of these lines ($$$1 \le i \le n - 1$$$) contains two integers $$$p_{i+1}$$$ and $$$w_{i+1}$$$ ($$$1 \le p_{i+1} \le i$$$, $$$0 \le w_{i+1} \le 10^5$$$) — the parent of vertex $$$i+1$$$ and the weight of the edge connecting them.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

Output

For each test case, print a single integer on a new line — the minimum possible total cost.

Example
Input
3
1
5
4
5 1 6 2
1 3
2 0
3 5
7
3 -5 8 1 -2 10 -7
1 4
1 4
2 1
2 9
3 0
3 6
Output
0
8
23