#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef pair<LL,LL> pii;
typedef pair<LL,pii> ppi;
typedef pair<pii,pii> ppp;
#define FOR(i, n) for(int i = 1; i<=n; i++)
#define F0R(i, n) for(int i = 0; i<n; i++)
#define mp make_pair
#define pb push_back
#define f first
#define s second

template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; }
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) {cout << "["; F0R(i, v.size()) {if (i) cout << ", "; cout << v[i];} return cout << "]";}

//var 
LL T, dp[100005][2], a[100005][2], n;
vector<LL> e[100005];

void dfs(LL g, LL p){

	for(auto u : e[g]){
		if(u == p) continue;
		dfs(u, g);
		F0R(z, 2){
			dp[g][z] += max(dp[u][0] + abs(a[g][z] - a[u][0]),
							dp[u][1] + abs(a[g][z] - a[u][1]));
		}
	}
}

void solve(){
	cin >> n;
	FOR(i, n){
		e[i].clear();
		dp[i][0] = 0; dp[i][1] = 0;
	}

	FOR(i, n) cin >> a[i][0] >> a[i][1];

	LL t1, t2;
	FOR(i, n-1){
		cin >> t1 >> t2;
		e[t1].pb(t2);
		e[t2].pb(t1);
	}

	dfs(1, 0);

	cout << max(dp[1][0], dp[1][1]) << "\n";
}

int main(){
	ios_base::sync_with_stdio(0); 
	cin.tie(0);

	T = 1;
	cin >> T;
	FOR(t, T)
		solve();

	cout.flush();
	return 0;
}