#include <bits/stdc++.h>

#define int long long
#define ft first
#define sd second
const int maxn = 1e5;

using namespace std;

vector<int> g[maxn];
int l[maxn], r[maxn];
int dp[maxn][2];

void dfs(int v, int p){
    dp[v][0] = 0, dp[v][1] = 0;
    for(auto to : g[v]){
        if(to == p) continue;
        dfs(to, v);
        dp[v][0] += max(dp[to][0] + abs(l[v] - l[to]), dp[to][1] + abs(l[v] - r[to]));
        dp[v][1] += max(dp[to][0] + abs(r[v] - l[to]), dp[to][1] + abs(r[v] - r[to]));
    }
}

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

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int q = 1;
    cin >> q;
    while (q--)
        solve();
}