#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mx = 2e5 + 9;
vector<int>adj[mx];
int dp[mx][2];
bool vis[mx];
int l[mx] , r[mx];
void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif // ONLINE_JUDGE
}


void dfs(int s , int p){

	for(auto u : adj[s]){
		if(u == p) continue;
		dfs(u , s);
		dp[s][0] += max(abs(l[s]-r[u]) + dp[u][1] , abs(l[s]-l[u]) + dp[u][0]);
		dp[s][1] += max(abs(r[s]-l[u]) + dp[u][0] , abs(r[s]-r[u]) + dp[u][1]);
	}
}

signed main(){
	fast();
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		for(int i=0 ; i<n ; i++){
			cin>>l[i]>>r[i];
		}
		for(int i=1 ; i<n ; i++){
			int u , v;
			cin>>u>>v;
			u--;
			v--;
			adj[u].push_back(v);
			adj[v].push_back(u);
		}
		memset(dp , 0 , sizeof dp);
		dfs(0 , -1);
		cout<<max(dp[0][0] , dp[0][1])<<endl;
		for(int i=0 ; i<mx ; i++){
			adj[i].clear();
			l[i] = r[i] = 0;
		}
		
	}
}