#include <bits/stdc++.h>
using namespace std;
//#define int ll
#define ll long long
#define rep(i,s,f) for(i=s;i<f;i++)
#define print(v) for(auto &z:v) cout<<z<<' ';cout<<'\n'
#define db cout<<"db: "
#define _ <<' '<<
#define pb push_back
#define pii pair<int,int>
#define F first
#define S second
#define B begin()
#define E end()
#define all(v) v.B,v.E
#define sz(v) (int)((v).size())
#define vi vector<int>
#define vii vector<pair<int,int>>
#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define clk1 clock_t start_time=clock()
#define clk2 cout<<(clock()-start_time)/(double)CLOCKS_PER_SEC
#define clean(arr) memset(arr,0,sizeof(arr))
#define mod 1000000007
#define mod2 998244353
#define space 100005
//<<fixed << setprecision(9)
pii arr[space];
vi G[space];
ll dp[space][2];

void dfs(int n, int p)
{
    dp[n][0]=dp[n][1]=0;

    for(auto z:G[n])
    {
        if(z!=p) dfs(z,n);
    }
    
    for(auto z:G[n])
    {
        if(z!=p)
        {
            dp[n][0]+= max(abs(arr[n].F-arr[z].F)+dp[z][0], abs(arr[n].F-arr[z].S)+dp[z][1]);
            dp[n][1]+= max(abs(arr[n].S-arr[z].F)+dp[z][0], abs(arr[n].S-arr[z].S)+dp[z][1]);
        }
    }
}
int main()
{
    int n,i,j,t;
    boost;

    cin>>t;
    while(t--)
    {
        cin>>n;
        rep(i,1,n+1)
        {
            cin>>arr[i].F>>arr[i].S;
            G[i].clear();
        }

        rep(i,0,n-1)
        {
            int u,v;
            cin>>u>>v;
            G[u].pb(v);
            G[v].pb(u);
        }

        dfs(1,-1);
        cout<<max(dp[1][0],dp[1][1])<<'\n';
    }
    

    return 0;
}
### Prompt

Please create a solution in cpp to the following problem:

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

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
//#define int ll
#define ll long long
#define rep(i,s,f) for(i=s;i<f;i++)
#define print(v) for(auto &z:v) cout<<z<<' ';cout<<'\n'
#define db cout<<"db: "
#define _ <<' '<<
#define pb push_back
#define pii pair<int,int>
#define F first
#define S second
#define B begin()
#define E end()
#define all(v) v.B,v.E
#define sz(v) (int)((v).size())
#define vi vector<int>
#define vii vector<pair<int,int>>
#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define clk1 clock_t start_time=clock()
#define clk2 cout<<(clock()-start_time)/(double)CLOCKS_PER_SEC
#define clean(arr) memset(arr,0,sizeof(arr))
#define mod 1000000007
#define mod2 998244353
#define space 100005
//<<fixed << setprecision(9)
pii arr[space];
vi G[space];
ll dp[space][2];

void dfs(int n, int p)
{
    dp[n][0]=dp[n][1]=0;

    for(auto z:G[n])
    {
        if(z!=p) dfs(z,n);
    }
    
    for(auto z:G[n])
    {
        if(z!=p)
        {
            dp[n][0]+= max(abs(arr[n].F-arr[z].F)+dp[z][0], abs(arr[n].F-arr[z].S)+dp[z][1]);
            dp[n][1]+= max(abs(arr[n].S-arr[z].F)+dp[z][0], abs(arr[n].S-arr[z].S)+dp[z][1]);
        }
    }
}
int main()
{
    int n,i,j,t;
    boost;

    cin>>t;
    while(t--)
    {
        cin>>n;
        rep(i,1,n+1)
        {
            cin>>arr[i].F>>arr[i].S;
            G[i].clear();
        }

        rep(i,0,n-1)
        {
            int u,v;
            cin>>u>>v;
            G[u].pb(v);
            G[v].pb(u);
        }

        dfs(1,-1);
        cout<<max(dp[1][0],dp[1][1])<<'\n';
    }
    

    return 0;
}
```