#include <vector>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <map>

using namespace std;

#define N 100007
#define M 1000007
#define mod 1000000007
#define LL long long

int l[N], r[N];
vector<int> E[N];

pair<LL, LL> dfs(int u, int pre)
{
    LL L = 0, R = 0;
    for (int i = 0; i < E[u].size(); i++)
    {
        int v = E[u][i];
        if (v == pre) continue;
        pair<LL, LL> p = dfs(v, u);
        L += max(p.first + abs(l[v] - l[u]), p.second + abs(r[v] - l[u]));
        R += max(p.first + abs(l[v] - r[u]), p.second + abs(r[v] - r[u]));
    }
    return make_pair(L, R);
}

int main()
{
    int n, ans, T;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d", &n);
        for (int i = 1; i <= n; i++)
        {
            scanf("%d%d", &l[i], &r[i]);
            E[i].clear();
        }
        for (int i = 1; i < n; i++)
        {
            int u, v;
            scanf("%d%d", &u, &v);
            E[u].push_back(v);
            E[v].push_back(u);
        }
        pair<LL, LL> p = dfs(1, 0);
        printf("%lld\n", max(p.first, p.second));
    }
    return 0;
}