#include<bits/stdc++.h>
#define int long long
#define MOD         1000000007
using namespace std;
int arr[100005][2];
int dp[100005][2];
void dfs(int t, int p, vector<int>g[]) {
	dp[t][0] = dp[t][1] = 0;
	for(int k : g[t]) {
		if(k == p)	continue;

		dfs(k, t, g);

		dp[t][0] += max(abs(arr[t][0] - arr[k][0]) + dp[k][0], abs(arr[t][0] - arr[k][1]) + dp[k][1]);
		dp[t][1] += max(abs(arr[t][1] - arr[k][0]) + dp[k][0], abs(arr[t][1] - arr[k][1]) + dp[k][1]);
	}
}
void solve() {
	
	int n;
	cin >> n;
	vector<int>g[n+1];
	for(int i = 1; i <= n; i++) cin >> arr[i][0] >> arr[i][1];

	for(int i = 0; i < n-1; i++) {
		int a, b;
		cin >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}


	dfs(1, -1, g);

	cout << max(dp[1][0], dp[1][1]) << endl;


	// for(int i = 0; i <= 100005; i++)	g[i].clear();
	
}



signed main() {
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(20);
	cin.tie(0);  //for interactive problem comment this
	cout.tie(0); // and this
	int t;
	t = 1;
	cin >> t;
	while(t--)	solve();
}