#include "bits/stdc++.h"

using namespace std;
using ll = long long;
const int MAX_N = 2e5 + 9;

ll dp[MAX_N][2],arr[MAX_N][2]; vector<int>adj[MAX_N];
void dfs(int u , int par = -1){
    dp[u][1] = dp[u][0] = 0;
    for(auto i : adj[u]){
        if(i == par)
            continue;
        dfs(i,u);
        dp[u][1] += max(dp[i][0] + abs(arr[i][0] - arr[u][1]) , dp[i][1] + abs(arr[i][1] - arr[u][1]));
        dp[u][0] += max(dp[i][0] + abs(arr[i][0] - arr[u][0]) , dp[i][1] + abs(arr[i][1] - arr[u][0]));
    }
}
int32_t main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
   int t,n; cin >> t;
   while(t--){
      cin >> n; fill(adj + 1 , adj + 1 + n , vector<int>());
      for(int i = 1; i <= n ; i++)
          cin >> arr[i][0] >> arr[i][1];
      for(int i = 0 ,u ,v; i < n - 1 ;i++){
          cin >> u >> v;
          adj[u].push_back(v);
          adj[v].push_back(u);
      }
      dfs(1);
      cout << max(dp[1][0] , dp[1][1]) << '\n';
   }
}

