#include <bits/stdc++.h>
using namespace std;
#define ll long long 
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define debug(x) cout << "[ " << #x  << " : " << x << " ]" << endl;
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

const ll maxn = 1e5+2;
vector<ll>v[maxn];
ll l[maxn],r[maxn];

pair<ll,ll> dfs(ll a,ll p)
{
	ll a1,a2;
	a1=0;a2=0;
	for(ll it:v[a])
	{
		if(it==p)
			continue;
		pair<ll,ll>p1 = dfs(it,a);
		a1+=max(p1.F+abs(l[it]-l[a]),p1.S+abs(r[it]-l[a]));
		a2+=max(p1.F+abs(l[it]-r[a]),p1.S+abs(r[it]-r[a]));
	}
	return {a1,a2}; 
}

int main()
{
	IOS;
	#ifndef ONLINE_JUDGE
	freopen("/home/naman/Desktop/input.txt","r",stdin);
	freopen("/home/naman/Desktop/output.txt","w",stdout);
	#endif

	ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		for (ll i = 0; i < n; ++i)
		{
			cin>>l[i]>>r[i];
			v[i].clear();
		}
		for (ll i = 0; i < n-1; ++i)
		{
			ll a,b;
			cin>>a>>b;
			a--;
			b--;
			v[a].pb(b);
			v[b].pb(a);
		}
		pair<ll,ll>p1 = dfs(0,-1);
		cout<<max(p1.F,p1.S)<<"\n";
	}	
}