#include <bits/stdc++.h>

using namespace std;
#define FAST		    ios_base::sync_with_stdio(false); cin.tie(NULL);
#define prs(n)		    cout << fixed << setprecision(n)
#define ll			    long long
#define db			    double
#define fo(i,m,n)       for(int i = m; i <= n; i++)
#define fob(i,m,n)      for(int i = n; i >= m; i--)
typedef                 pair<int, int> ii;
typedef                 vector<ii> vii;
typedef                 vector<vii> vvii;
const                   int maxi=INT_MAX;
const                   int _x[]={0,0,1,-1};
const                   int _y[]={1,-1,0,0};

ll n, ans, l[200007], r[200007], dp[200007][2];
vector <ll> a[200007];

void input()
{
    int u, v;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> l[i] >> r[i], a[i].resize(0);
        dp[i][1] = dp[i][0] = 0;
    }
    for (int i = 1; i < n ; i++)
    {
        cin >> u >> v;
        a[u].push_back(v);
        a[v].push_back(u);
    }
}

void dfs(int u, int f)
{
    for (int i:a[u])
    {
        if (i != f)
        {
            dfs(i,u);
            dp[u][0] += max(dp[i][0] + abs(l[u] - l[i]), dp[i][1] + abs(l[u] - r[i]));
            dp[u][1] += max(dp[i][0] + abs(r[u] - l[i]), dp[i][1] + abs(r[u] - r[i]));
        }
    }
}

void solve()
{
    dfs(1,0);
    ans = max(dp[1][0], dp[1][1]);
}

int main()
{
    FAST
    //freopen("a.INP", "r", stdin);
    //freopen("a.OUT", "w", stdout);
    int t;
    cin >> t;
    while (t--)
    {
        input();
        solve();
        cout << ans << '\n';
    }
    return 0;
}
