#include <bits/stdc++.h>
#define LL long long

using namespace std;

const int Maxn = 2e5 + 5;

template <typename Temp> inline void read(Temp & res) {
	Temp fh = 1; res = 0; char ch = getchar();
	for(; !isdigit(ch); ch = getchar()) if(ch == '-') fh = -1;
	for(; isdigit(ch); ch = getchar()) res = (res << 3) + (res << 1) + (ch ^ '0');
	res = res * fh;
}

std::vector<int> g[Maxn];

LL f[Maxn][2];

int l[Maxn], r[Maxn], n, x, y;

inline LL Abs(int A) {
	return 1ll * (A < 0 ? -A : A);
}

void dfs(int u, int fa) {
	if(g[u].size() == 0) return;
	for(int i = 0; i < g[u].size(); ++i) {
		int v = g[u][i];
		if(v == fa) continue; dfs(v, u);
		f[u][1] += std::max(f[v][0] + Abs(r[u] - l[v]), f[v][1] + Abs(r[u] - r[v]));
		f[u][0] += std::max(f[v][0] + Abs(l[u] - l[v]), f[v][1] + Abs(l[u] - r[v]));
	}
}

void Main() {
	read(n);
	for(int i = 1; i <= n; ++i) f[i][0] = f[i][1] = 0, g[i].clear();
	for(int i = 1; i <= n; ++i) read(l[i]), read(r[i]);
	for(int i = 2; i <= n; ++i) {
		read(x); read(y);
		g[x].push_back(y); g[y].push_back(x); 
	}
	dfs(1, 0);
	printf("%lld\n", std::max(f[1][0], f[1][1]));
}

int main() {
	int T; read(T);
	while(T--) Main();
	return 0;
}