#include <bits/stdc++.h>
#define pii pair<int,int>
#define ac cout<<ans<<endl
#define rep(i,n) for(int (i)=0;(i)<(n);i++)
#define rep1(i,n) for(int (i)=1;(i)<=(n);i++)
#define int long long
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)

//https://codeforces.com/contest/1528/problem/A ���⣬����dp

using namespace std;
const int N=1e5+10;
int a[N],vis[N],f[N][2],head[N],dp[N][2],cnt;

struct node
{
    int next,to;
}b[2*N];

void dfs(int x,int y)
{
    for(int i=head[y];i;i=b[i].next)
    {
        int to=b[i].to;
        if(to==x) continue;
        dfs(y,to);
        dp[y][0]+=max(dp[to][0]+abs(f[y][0]-f[to][0]),dp[to][1]+abs(f[y][0]-f[to][1]));
        dp[y][1]+=max(dp[to][0]+abs(f[y][1]-f[to][0]),dp[to][1]+abs(f[y][1]-f[to][1]));
    }
}

void add(int l,int r)
{
    b[++cnt].next=head[l];
    head[l]=cnt;
    b[cnt].to=r;
}

signed main()
{
    IOS;
    int t;
    cin>>t;
    while(t--)
    {
        cnt=0;
        memset(vis,0,sizeof(vis));
        memset(head,0,sizeof(head));
        memset(dp,0,sizeof(dp));
        memset(f,0,sizeof(f));
        int n;
        cin>>n;
        rep1(i,n) cin>>f[i][0]>>f[i][1];
        rep1(i,n-1)
        {
            int l,r;
            cin>>l>>r;
            add(l,r);
            add(r,l);
        }
        dfs(1,1);
        cout<<max(dp[1][0],dp[1][1])<<endl;
    }
    return 0;
}
