Parsa has a humongous tree on n vertices.

On each vertex v he has written two integers l_v and r_v.

To make Parsa's tree look even more majestic, Nima wants to assign a number a_v (l_v ≤ a_v ≤ r_v) to each vertex v such that the beauty of Parsa's tree is maximized.

Nima's sense of the beauty is rather bizarre. He defines the beauty of the tree as the sum of |a_u - a_v| over all edges (u, v) of the tree.

Since Parsa's tree is too large, Nima can't maximize its beauty on his own. Your task is to find the maximum possible beauty for Parsa's tree.

Input

The first line contains an integer t (1≤ t≤ 250) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer n (2≤ n≤ 10^5) — the number of vertices in Parsa's tree.

The i-th of the following n lines contains two integers l_i and r_i (1 ≤ l_i ≤ r_i ≤ 10^9).

Each of the next n-1 lines contains two integers u and v (1 ≤ u , v ≤ n, u≠ v) meaning that there is an edge between the vertices u and v in Parsa's tree.

It is guaranteed that the given graph is a tree.

It is guaranteed that the sum of n over all test cases doesn't exceed 2 ⋅ 10^5.

Output

For each test case print the maximum possible beauty for Parsa's tree.

Example

Input


3
2
1 6
3 8
1 2
3
1 3
4 6
7 9
1 2
2 3
6
3 14
12 20
12 19
2 12
10 17
3 17
3 2
6 5
1 5
2 6
4 6


Output


7
8
62

Note

The trees in the example:

<image>

In the first test case, one possible assignment is a = \{1, 8\} which results in |1 - 8| = 7.

In the second test case, one of the possible assignments is a = \{1, 5, 9\} which results in a beauty of |1 - 5| + |5 - 9| = 8
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class C {

    private static final boolean TEST_MODE = true;
    private static Node[] tree;
    private static long[] lWt, rWt;

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int T = getInt(br);
        for (int t=0; t<T; t++) {
            int N = getInt(br);

            tree = new Node[N+1];
            for (int v = 1; v <= N; v++) {
                String s = br.readLine();
                tree[v] = new Node(s);
            }

            for (int e=1; e<=N-1; e++) {
                int[] arr = getIntArray(2, br);
                int u = arr[0];
                int v = arr[1];
                tree[u].addNbr(v);
                tree[v].addNbr(u);
            }
            rootTheTree();

            lWt = new long[N + 1];
            rWt = new long[N + 1];
            Arrays.fill(lWt, -1);
            Arrays.fill(rWt, -1);
            getMaxBeauty();
        }
    }

    private static void rootTheTree() {
       // Arbitrarily pick the root as #1.
        Node root = tree[1];
        root.parent = 0;
        Queue<Integer> q = new LinkedList<>();
        q.add(1);

        // Constraint: Every node that is in the queue has parent already assigned.
        while (!q.isEmpty()) {
            int currId = q.poll();
            Node currNode = tree[currId];
            // Add all neighbours as children.
            for (int nbrId : currNode.nbrs) {
                if (nbrId == currNode.parent) {
                    continue;
                }
                tree[nbrId].parent = currId;
                currNode.addChild(nbrId);
                q.add(nbrId);
            }
//            currNode.nbrs.clear();
        }
    }

    private static void getMaxBeauty() throws Exception {
        Stack<Integer> stack = new Stack<>();
        stack.add(1);

        while (!stack.isEmpty()) {
            int currId = stack.pop();
            Node curr = tree[currId];

            // Is leaf ?
            if (curr.children.size() == 0) {
                lWt[currId] = 0;
                rWt[currId] = 0;
                continue;
            }
            // Are all children processed ?
            boolean allChildrenProcessed = (lWt[curr.children.get(0)] >= 0);
            if (allChildrenProcessed) {
                lWt[currId] = 0;
                rWt[currId] = 0;
                // Compute lwt and rwt maximized against each child.
                for (int childId : curr.children) {
                    Node childNode = tree[childId];
                    long ll = Math.abs(curr.left - childNode.left) + lWt[childId];
                    long lr = Math.abs(curr.left - childNode.right) + rWt[childId];
                    lWt[currId] += Math.max(ll, lr);

                    long rl = Math.abs(curr.right - childNode.left) + lWt[childId];
                    long rr = Math.abs(curr.right - childNode.right) + rWt[childId];
                    rWt[currId] += Math.max(rl, rr);
                }
            } else { // Add all unvisited children.
                stack.add(currId);
                stack.addAll(curr.children);
            }
        }
        long res = Math.max(lWt[1], rWt[1]);
        System.out.println(res);
    }

    private static int getLeftId(int nodeId) {
        return tree[nodeId].left;
    }

    private static int getRightId(int nodeId) {
        return tree[nodeId].right;
    }

    private static Integer getInt(BufferedReader br) throws Exception {
        return Integer.parseInt(br.readLine());
    }

    private static Long getLong(BufferedReader br) throws Exception {
        return Long.parseLong(br.readLine());
    }

    private static int[] getIntArray(int N, BufferedReader br) throws Exception {
        String s = br.readLine();
        String[] tokens = s.split(" ");
        int[] result = new int[N];

        for (int i=0; i<N; i++) {
            result[i] = Integer.parseInt(tokens[i]);
        }
        return result;
    }

    private static long[] getLongArray(int N, BufferedReader br) throws Exception {
        String s = br.readLine();
        String[] tokens = s.split(" ");
        long[] result = new long[N+1];

        for (int i=0; i<N; i++) {
            result[i+1] = Long.parseLong(tokens[i]);
        }
        return result;
    }

    private static void printArray(String message, Object[] arr) {
        if (!TEST_MODE) {
            return;
        }
        System.out.print(message + " : ");
        for (int i=0; i<arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }
}

class Node {
    int parent = -1;
    List<Integer> nbrs = new ArrayList<>();
    List<Integer> children = new ArrayList<>();
    int left, right;

    public Node(int l, int r) {
        this.left = l;
        this.right = r;
    }

    public Node(String s) {
        String[] tokens = s.split(" ");
        this.left = Integer.parseInt(tokens[0]);
        this.right = Integer.parseInt(tokens[1]);
    }

    public void addNbr(int nbr) {
        nbrs.add(nbr);
    }

    public void addChild(int child) {
        children.add(child);
    }
}