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.util.*;
import java.io.*;

public class Main {

    public static void main(String[] args) throws IOException {
        long start=System.currentTimeMillis();
        FastScanner fs=new FastScanner();
        int t=1; t=fs.nextInt();
        while(t--!=0){
            int n=fs.nextInt();
            ArrayList<pair<Integer,Integer>> range=new ArrayList<>();
            for(int i=0;i<n;i++){
                int l=fs.nextInt(),r=fs.nextInt();
                range.add(new pair<>(l,r));
            }
            ArrayList<Integer> graph[] = new ArrayList[n];
            for(int i=0;i<n;i++) graph[i]=new ArrayList<>();
            for(int i=0;i<n-1;i++){
                int u=fs.nextInt(),v=fs.nextInt();
                u--; v--;
                graph[u].add(v);
                graph[v].add(u);
            }
            ArrayList<Integer> visit=new ArrayList<>();
            for(int i=0;i<n;i++) visit.add(0);
            long dp[][] = new long[n][2];
            for(int i=0;i<n;i++){
                for(int j=0;j<2;j++){
                    dp[i][j]=0;
                }
            }
            // now ready to run dfs.

            dfs(graph,visit,range,dp,0);

            System.out.println(Math.max(dp[0][0],dp[0][1]));

        }

//    long end=(System.currentTimeMillis()-start);
//    System.err.println("Time taken is "+end+" ms");
    }

    public static void dfs(ArrayList<Integer>[] graph,ArrayList<Integer> visit,ArrayList<pair<Integer,Integer>> range,long[][] dp,int start){
        visit.set(start,1);

        for(var child: graph[start]){
            if(visit.get(child)==0){
                dfs(graph,visit,range,dp,child);
                var rg_parent=range.get(start);
                var rg_child=range.get(child);
                dp[start][0]+=(Math.max(Math.abs(rg_parent.ff-rg_child.ff)+dp[child][0],Math.abs(rg_parent.ff-rg_child.ss)+dp[child][1]));
                dp[start][1]+=(Math.max(Math.abs(rg_parent.ss-rg_child.ff)+dp[child][0],Math.abs(rg_parent.ss-rg_child.ss)+dp[child][1]));
            }
        }
    }






    public static long binexp(long a, long b,long md){
        if(b==0) return 1;
        if(b==1) return a%md;
        long xx=binexp(a,b/2,md);
        if(b%2==0){
            return (xx%md * xx%md)%md;
        }
        else {
            return (xx%md * xx%md * a)%md;
        }
    }


    public static int gcd(int a,int b){
        if(b==0) return a;
        if(a==0) return b;
        if(a>b) return gcd(b,a);
        return gcd(a,b%a);

    }




    static class FastScanner {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer("");
        String next() {
            while (!st.hasMoreTokens())
                try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }
        int[] readArray(int n) {
            int[] a=new int[n];
            for (int i=0; i<n; i++) a[i]=nextInt();
            return a;
        }
        long nextLong() {
            return Long.parseLong(next());
        }

    }
    static class pair<t1,t2>{
        public t1 ff;
        public t2 ss;
        pair(){}
        pair(t1 first,t2 second){
            this.ff=first;
            this.ss=second;
        }

        @Override
        public boolean equals(Object obj) {
            if(obj==null) return false;
            else if(obj.getClass()!=this.getClass()) return false;
            pair<?,?> pp = (pair<?,?>) obj;
            return pp.ff.equals(this.ff) && pp.ss.equals(this.ss);
        }

        @Override
        public int hashCode() {
            return Objects.hash(ff,ss);
            //return 31 * ff.hashCode() + ss.hashCode();
        }
    }
}





