#include <bits/stdc++.h>
#define testCase int T; cin>>T; while(T--)
#define int long long
#define mod 998244353
#define Endl endl
using namespace std;
/**/
const int N = 1e5+13, LMAX  = 3e18+13, IMAX = 1e9+5;
int n, vals[N][2], dp[N][2];
vector<vector<int>> g(N);
bool isVis[N];

int solve(int r = 1, int prv = -1, int pVal = 0) {
    isVis[r] = true;
    if (prv !=-1 and dp[r][prv] != -1)
        return dp[r][prv];

    int ret1 = 0, ret2 = 0;
    if (prv != -1) {
        ret1 += abs(vals[r][0] - pVal);
        ret2 += abs(vals[r][1] - pVal);
    }
    for (auto i: g[r]) {
        if (isVis[i]) continue;
        ret1 += solve(i, 0, vals[r][0]);
        ret2 += solve(i, 1, vals[r][1]);
    }
    dp[r][prv] = max(ret1, ret2);
//    printf("r = %lld, pVal = %lld -- ret1 = %lld, ret2 = %lld => %lld\n", r, pVal, ret1, ret2, dp[r][prv]);
    isVis[r] = false;
    return dp[r][prv];
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    testCase {
        cin >> n;
        for (int i = 0; i < n+1; ++i) {
            isVis[i] = false;
            g[i].clear();
            dp[i][0] = dp[i][1] = -1;
        }

        for (int i = 1; i < n+1; ++i)
            cin >> vals[i][0] >> vals[i][1];

        int u, v;
        for (int i = 1; i < n ; ++i) {
            cin >> u >> v;
            g[u].emplace_back(v);
            g[v].emplace_back(u);
        }

        cout << solve() << "\n";
    }
    return 0;
}