#include <bits/stdc++.h>

using namespace std;

#ifdef NeverBeRed
#include "debug.h"
#else
#define debug(...) 9715
#endif
typedef long long ll;
typedef long double ld;
typedef complex<ld> point;
#define F first
#define S second



int main()
{
	ios_base::sync_with_stdio(0), cin.tie(0);

	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		vector<int> l(n), r(n);
		for (int i = 0; i < n; ++i)
			cin >> l[i] >> r[i];

		vector<vector<int>> adj(n);
		for (int i = 1, u, v; i < n; ++i)
		{
			cin >> u >> v;
			--u, --v;
			adj[u].push_back(v);
			adj[v].push_back(u);
		}

		vector<vector<ll>> dp(2, vector<ll>(n));
		function<void(int, int)> dfs = [&](int u, int p)
		{
			for (auto v : adj[u])
				if (v != p)
				{
					dfs(v, u);
					dp[0][u] += max(dp[0][v] + abs(l[v] - l[u]), dp[1][v] + abs(r[v] - l[u]));
					dp[1][u] += max(dp[0][v] + abs(l[v] - r[u]), dp[1][v] + abs(r[v] - r[u]));
				}
		};
		dfs(0, -1);
		cout << max(dp[0][0], dp[1][0]) << "\n";
	}

	return 0;
}
