#include<bits/stdc++.h>
using namespace std;

typedef vector<int> vi;

#define ll long long
#define sz(v) (int)(v).size()
#define ar array
#define fi first
#define se second
#define pb push_back
#define endl "\n"
#define dbg(x) cerr << #x << " = " << x << endl

const int mxN = 1e6;
ll dp[mxN][2];
ll l[mxN],r[mxN];
vector<vector<int>> adj;

void dfs(int u = 0, int par = -1) {
    ll sum1,sum2;
    sum1 = sum2 = 0;
    for(int v : adj[u])
        if(v != par) {
            dfs(v,u);
            sum1 += max(abs(l[u] - r[v]) + dp[v][1],abs(l[u]-l[v])+dp[v][0]);
            sum2 += max(abs(r[u] - l[v]) + dp[v][0],abs(r[u] - r[v]) + dp[v][1]);
        }
    dp[u][0] = sum1;
    dp[u][1] = sum2;
}

void solve() {
    int n;
    cin>>n;
    for(int i = 0 ; i<n ; ++i) cin>>l[i]>>r[i];
    adj.clear();
    adj.resize(n);
    for(int i = 0 ; i<n-1 ; ++i) {
        int u,v;
        cin>>u>>v,--u,--v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    dfs();
    cout << max(dp[0][0],dp[0][1]) << "\n";
}


int main() {
    // freopen("teleport.in","r",stdin); 
    // freopen("teleport.out","w",stdout);
    cin.tie(NULL)->sync_with_stdio(false);
    int t=1;
    cin>>t;
    while(t--) {
        solve();
    }
    return 0;
}