#include <bits/stdc++.h>
     
#define all(A) begin(A), end(A)
#define rall(A) rbegin(A), rend(A)
#define sz(A) int(A.size())
#define pb push_back
#define mp make_pair
     
using namespace std;
     
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <pii> vpii;
typedef vector <pll> vpll;

int main () {
  ios::sync_with_stdio(false); cin.tie(0);
  int tt;
  cin >> tt;
  while (tt--) {
    int n;
    cin >> n;
    vi l(n + 1);
    vi r(n + 1);
    for (int i = 1; i <= n; i++) cin >> l[i] >> r[i];
    vector <vi> g(n + 1);
    for (int i = 1; i < n; i++) {
      int u, v;
      cin >> u >> v;
      g[u].pb(v);
      g[v].pb(u);
    }
    vll dp_l(n + 1, 0);
    vll dp_r(n + 1, 0);
    function <void(int,int)> dfs = [&] (int u, int p) {
      for (int v: g[u]) {
        if (v == p) continue;
        dfs(v, u);
        dp_l[u] += max(abs(l[u] - l[v]) + dp_l[v], abs(l[u] - r[v]) + dp_r[v]);
        dp_r[u] += max(abs(r[u] - l[v]) + dp_l[v], abs(r[u] - r[v]) + dp_r[v]);
      }
    };
    dfs(1, -1);
    cout << max(dp_l[1], dp_r[1]) << '\n';
  }
  return (0);
}
