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

#define pb push_back
#define mp make_pair

#define int long long
#define IOS ios_base::sync_with_stdio(0);cin.tie(0)
#define PRECISION cout.precision(3); cout.setf(ios::fixed);

#define infinite 9123456789
#define db cout << "Debug" << "\n";

#define ler_entrada freopen("test_input.txt", "r", stdin);

int n, l[200010], r[200010], memo[200010][2];
vector<int> vertice[200010];

void dfs(int x, int pai){
	for(auto u : vertice[x]){
		if(u == pai) continue;
		
		dfs(u, x);
		
		memo[x][0] += max(memo[u][0] + abs(l[u] - l[x]), memo[u][1] + abs(r[u] - l[x]));
		memo[x][1] += max(memo[u][0] + abs(l[u] - r[x]), memo[u][1] + abs(r[u] - r[x]));
	}
}

void solve(){
	cin >> n;
	for(int i = 1; i <= n; i++){
		vertice[i].clear();
		memo[i][0] = memo[i][1] = 0;
	}
	for(int i = 1; i <= n; i++) cin >> l[i] >> r[i];
	
	for(int i = 1; i < n; i++){
		int a, b; cin >> a >> b;
		vertice[a].pb(b);
		vertice[b].pb(a);
	}
	dfs(1, -1);
	cout << max(memo[1][0], memo[1][1]) << "\n";
}

signed main(){
	IOS; int t; cin >> t;
	for(int i = 1; i <= t; i++) solve();
}
		 	  			 	  	   		 			 		 	 		