#include <bits/stdc++.h>

using namespace std;

long long dp[2][300000];

long long dfs(int previous,bool previousMax,int N, vector<vector<int> > & adj, vector<pair<long long,long long> > &vals) {
	if(dp[previousMax][N]!=-1) return dp[previousMax][N];
	long long res1 = 0;
	long long res2 = 0;
	if(previous != -1) {
		res1 = abs((previousMax?vals[previous].second:vals[previous].first)-vals[N].first);
		res2 = abs((previousMax?vals[previous].second:vals[previous].first)-vals[N].second);
	}

	for(auto &z:adj[N]) {
		if(z != previous) {
			res1 += dfs(N,false,z,adj,vals);
			res2 += dfs(N,true,z,adj,vals);
		}
	}
	dp[previousMax][N] = max(res1,res2);
	return max(res1,res2);
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int T;
	cin>>T;
	
	while(T--) {
		int N;
		cin>>N;
		vector<pair<long long int,long long int> > vals(N);
		for(int c=0;c<N;c++) cin>>vals[c].first>>vals[c].second;

		vector<vector<int> > adj(N);

		for(int c=0;c<2;c++) for(int c2=0;c2<N;c2++) dp[c][c2] = -1;

		for(int c=1;c<N;c++) {
			int f,t;
			cin>>f>>t;
			f--;
			t--;
			adj[f].push_back(t);
			adj[t].push_back(f);
		}

		cout<<max(dfs(-1,false,0,adj,vals),dfs(-1,false,0,adj,vals))<<"\n";
	}
}
