#include <iostream>
#include <vector>
#include <algorithm>

#define MAXN (200007)

std::vector<int> g[MAXN];
int l[MAXN], r[MAXN];

void dfs(int v, int p, long long &used_l, long long &used_r)
{
    long long used_ll = 0;
    long long used_rr = 0;
    
    for (int to: g[v])
    {
        if (to != p)
        {
            long long to_used_l, to_used_r;
            dfs(to, v, to_used_l, to_used_r);
            
            used_ll += std::max(to_used_l + std::abs(l[v] - l[to]), to_used_r + std::abs(l[v] - r[to]));
            used_rr += std::max(to_used_l + std::abs(r[v] - l[to]), to_used_r + std::abs(r[v] - r[to]));
        }
    }
    
    used_l = used_ll;
    used_r = used_rr;
}

long long dfs(int v, int p)
{
    long long x, y;
    dfs(v, p, x, y);
    return std::max(x, y);
}

long long solve()
{
    int n;
    std::cin >> n;
    
    for (int i = 0; i < n; i++) g[i].clear();
    for (int i = 0; i < n; i++) std::cin >> l[i] >> r[i];
    
    for (int i = 0; i < n - 1; i++) 
    {
        int a, b;
        std::cin >> a >> b;
        a--;
        b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    
    return dfs(0, -1);
}

int main()
{
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);
    
    int t;
    std::cin >> t;
    for (int i = 0; i < t; i++) std::cout << solve() << "\n";
    return 0;
}