#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
#define rep(i, l, r) for(int i = int(l); i < int(r); i++)
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define MAX 1e5
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;

vector <int> l(MAX+1),r(MAX+1);

void dfs(int src, vector <vector<ll>> &dp, vector <bool> &vis, vector <vector<int>> &adj){
    
    vis[src]=true;
    
    for(int nbr:adj[src]){
        if(!vis[nbr]){
           
            dfs(nbr,dp,vis,adj);
            dp[src][0] += max(abs(l[src]-l[nbr])+dp[nbr][0] ,abs(l[src]-r[nbr])+dp[nbr][1] );
            dp[src][1] += max(abs(r[src]-l[nbr])+dp[nbr][0] ,abs(r[src]-r[nbr])+dp[nbr][1] );
            
            
        }
    }
}

ll solve(){
    int n;
    cin>>n;
    
  
    
    for(int i=1;i<=n;i++){
        cin>>l[i]>>r[i];
        //cout<<l[i]<<r[i]<<"\n";
    }
    
    vector <vector<int>> adj(n+1);
    
    int u,v;
    for(int i=1;i<=n-1;i++){
        cin>>u>>v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    
    
    vector <vector<ll>> dp(n+1,vector <ll> (2));
    vector <bool> vis(n+1,false);
    
    dfs(1,dp,vis,adj);
    
    return max(dp[1][0],dp[1][1]);
}

int main(){
    FIO;

    int t;
	cin>>t; 
	
	while(t--){
        cout<<solve()<<"\n";
	}
}