#include<bits/stdc++.h>
using namespace std;
vector<int> e[200000];
long long f[200000],g[200000];
int l[200000],r[200000];
inline void ins(int u,int v){
	e[u].push_back(v);
	e[v].push_back(u);
}
void dfs(int u,int fa){
	for (int i=0;i<e[u].size();i++){
		int v=e[u][i];
		if (v==fa) continue;
		dfs(v,u);
		f[u]+=max(f[v]+abs(l[u]-l[v]),g[v]+abs(l[u]-r[v]));
		g[u]+=max(f[v]+abs(r[u]-l[v]),g[v]+abs(r[u]-r[v]));
	}
}
int main(){
	int t;
	scanf("%d",&t);
	while (t--){
		int n;
		scanf("%d",&n);
		for (int i=1;i<=n;i++){
			scanf("%d%d",l+i,r+i);
			e[i].clear();
			f[i]=g[i]=0;
		}
		for (int i=1;i<n;i++){
			int u,v;
			scanf("%d%d",&u,&v);
			ins(u,v);
		}
		dfs(1,0);
		printf("%lld\n",max(f[1],g[1]));
	}
	return 0;
}