#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ff first
#define ss second
#define all(A) (A).begin(), (A).end()
#define SinghIsKinng ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define oo 1000000010
#define mod 1000000007
#define ll long long int
#define sz(A) (int)(A.size())
const double PI = 3.14159265358979323846;
const double e = 2.71828182845904523;
vector<bool> prime(100010, true);

void DFS(ll node, ll parent, vector<pair<ll, ll>> &A, vector<vector<ll>> &adj, vector<pair<ll, ll>> &dp){
    
    for(auto child : adj[node]){
        if(child == parent)
            continue;
        DFS(child, node, A, adj, dp);
        dp[node].ff += max(dp[child].ff + abs(A[child].ff - A[node].ff), dp[child].ss + abs(A[child].ss - A[node].ff));
        dp[node].ss += max(dp[child].ff + abs(A[child].ff - A[node].ss), dp[child].ss + abs(A[child].ss - A[node].ss));
    }
}

void solve(){
	ll n, m, a, b, c, d, k, l, r;
    
    string s;
    cin>>n;
    
    vector<pair<ll, ll>> A(n + 1);
    vector<vector<ll>> adj(n + 1);
    
    for(ll i = 1; i <= n; i++)
        cin>>A[i].ff>>A[i].ss;
    
    for(ll i = 0; i < n - 1; i++){
        ll u, v;
        cin>>u>>v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    
    vector<pair<ll, ll>> dp(n + 1);
    
    DFS(1, -1, A, adj, dp);
    
    cout<<max(dp[1].ff, dp[1].ss);
    
    
}    

int main() {

    SinghIsKinng;
    
    // for (int p = 2; p * p <= 100005; p++){
    //     if (prime[p] == true){
    //         for (int i = p * p; i <= 100005; i += p)
    //             prime[i] = false;
    //     }
    // }
    
	int t;
 	cin>>t;
 	
	while(t--){
	  solve();
 	  cout<<endl;
	}

	return 0;
}