#include<bits/stdc++.h>
using namespace std;
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL); 
#define int long long
#define PI 3.141592653589793238
#define endl '\n'
#define mod 1000000007
#define um unordered_map
#define umm unordered_multimap
#define pb push_back
#define fr first
#define sc second

template <class T>
T max3(T a,T b,T c)
{
	return max(a,max(b,c));
}
const int N=1e5+5;
vector<int> v[N];
int l[N], r[N], dp[2][N];
 //dp[0] means left and dp[1] means right and dp[1] means right side
void dfs(int z,int p = 0){
	dp[0][z] = 0;
	dp[1][z] = 0;
	for(int x:v[z]){
		if(x == p) continue;
		dfs(x,z);
		dp[0][z] += max(dp[0][x] + abs(l[z] - l[x]),dp[1][x] + abs(l[z] - r[x]));
		dp[1][z] += max(dp[0][x] + abs(r[z] - l[x]),dp[1][x] + abs(r[z] - r[x]));
	}
	//left side ka max solution dp[0][x] and right side ka maximum is dp[1][x]
}
void dfs(int c,vector<vector<int>>&v,int p=0)
{
	dp[0][0]=0;
	dp[1][0]=0;
	for(auto child:v[c])
	{
		if(child==c)
			continue;
		dfs(child,v,c);
		dp[0][p]=max(abs(l[c]-l[child]),(abs(l[c]-r[child])));
		dp[1][p]=max(abs(r[c]-l[child]),(abs(r[c]-r[child])));


	}

}

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

}

signed main()
{
	IOS;
	int t;
	cin>>t;
	while(t--)
	{
		solve();
	}
	return 0;
}