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

#define rep(i,a,b) for(auto i=(a); i<(b); ++i)
#define trav(a,x) for(auto& a: x)
#define all(x) begin(x),end(x)
#define sz(x) (int)size(x)
#define PB push_back
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using vi = vector<int>;
using vll = vector<ll>;
using vpii = vector<pii>;

struct edge{int to;};
using graph = vector<vector<edge>>;

ll dfs(int u, int p, int high, const vector<vi>& g, const vi& L, const vi& R, vector<vll>& dp) {
    if (dp[u][high] != -1) return dp[u][high];
    ll curr = high ? R[u] : L[u];
    ll score = 0;

    trav(v, g[u]) if (v != p) {
        ll L_score = dfs(v, u, 0, g, L, R, dp);
        ll H_score = dfs(v, u, 1, g, L, R, dp);
        score += max(L_score + abs(L[v]-curr), H_score+abs(R[v]-curr));
    }
    return dp[u][high] = score;
}

int main() {
    cin.sync_with_stdio(0); cin.tie(0);
    cin.exceptions(cin.failbit);

    int t;
    cin >> t;
    while (t--) {
        int n; cin >> n;
        vector<vi> g(n);

        vi L(n), R(n);
        rep(i, 0, n) cin >> L[i] >> R[i];
        
        vector<vll> dp(n, vll(2, -1));
        rep(i, 0, n-1) {
            int u, v; cin >> u >> v;
            u--, v--;
            g[u].PB(v);
            g[v].PB(u);
        }

        ll ans = max(dfs(0, -1, 0, g, L, R, dp), dfs(0, -1, 1, g, L, R, dp));
        cout << ans << "\n";
        
    }
    
}
