#include <bits/stdc++.h>
#define forr(i,a,b) for(int i=(a);i<(b);i++)
#define forn(i,n) forr(i,0,n)
#define dforn(i,n) for(int i=n-1;i>=0;i--)
#define forall(it,v) for(auto it=v.begin();it!=v.end();it++)
#define sz(c) ((int)c.size())
#define rsz resize
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define fst first
#define snd second

#ifdef ANARAP
#else
#endif

using namespace std;

typedef long long ll;
typedef pair<int,int> ii;

vector<vector<int>> g;
vector<int> vl,vr;
vector<ll> dp[2];

ll solve(int node, int p, int r)
{
	if(dp[r][node] != -1) return dp[r][node];
	ll ret1 = 0, ret2 = 0;
	forall(it, g[node]) if(*it != p) ret1 += solve(*it, node, 0), ret2 += solve(*it, node, 1);
	if(p != -1)
	{
		if(r) ret1 += abs(vr[p]-vl[node]), ret2 += abs(vr[p]-vr[node]);
		else ret1 += abs(vl[p]-vl[node]), ret2 += abs(vl[p]-vr[node]);
	}
	return dp[r][node] = max(ret1,ret2);
}

int main()
{
	#ifdef ANARAP
		freopen("input.in", "r", stdin);
		//freopen("output.out","w", stdout);
	#endif
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int t;
	cin >> t;
	forn(T,t)
	{
		int n;
		cin >> n;
		vl = vector<int>(n);
		vr = vector<int>(n);
		g = vector<vector<int>>(n);
		forn(i,n) cin >> vl[i] >> vr[i];
		forn(i,n-1)
		{
			int a,b;
			cin >> a >> b;
			a--;b--;
			g[a].pb(b);
			g[b].pb(a);
		}
		forn(i,2) dp[i] = vector<ll>(n,-1);
		cout << solve(0,-1,0) << '\n';
	}
	return 0;
}
