#include<bits/stdc++.h>
using namespace std;

#define int int64_t

vector<int>adj[100100];
int a[100100], b[100100];

pair<int, int> dfs(int u, int p){
	int f, s;
   	f = s = 0;
	for(auto x: adj[u]){
		if(x == p){
			continue;
		}
		pair<int, int> now = dfs(x, u);
		f += max(now.first + abs(a[u] - a[x]), now.second + abs(a[u] - b[x]));
		s += max(now.first + abs(b[u] - a[x]), now.second + abs(b[u] - b[x]));
	}	
	return make_pair(f, s);
}

void solve(){
	int n;
	cin >> n;
	for(int i=1; i<=n; ++i){
		cin >> a[i] >> b[i];
	}
	for(int i=1, u, v; i<n; ++i){
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	pair<int, int> ans = dfs(1, -1);
	cout << max(ans.first, ans.second);
	for(int i=0; i<=n; ++i){
		adj[i].clear();
	}
	return ;
}

int32_t main(){
	cin.tie(nullptr)->ios::sync_with_stdio(false);
	int t;
	cin >> t;
	while(t--){
		solve();
		cout << "\n";
	}
	return 0;
}
