//#include <ext/pb_ds/assoc_container.hpp> 
//#include <ext/pb_ds/tree_policy.hpp>
//#include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstring>
#include <vector>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <iterator>
#include <functional>
#include <limits>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <cassert>
#include <unordered_map>
#include <unordered_set>
#include <stack>

using namespace std;
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define trace(x) cerr << #x << ": " << x << " " << endl;
 
typedef int64_t ll;
 
#define endl '\n'
#define int ll
ll mod=998244353;
ll mod1=1e9+5;
ll power(ll a,ll b)
{
    if(b==0) return 1;
    else if(b%2==0)
        return power(((((a)%mod)*((a)%mod))%mod),b/2)%mod;
    else return (((a)%mod)*(power(((((a)%mod)*((a)%mod))%mod),b/2)%mod))%mod;
}
inline ll inverse(ll a,ll md) {
  a %= md;
  if (a < 0) a += md;
  ll b = md, u = 0, v = 1;
  while (a) {
    ll t = b / a;
    b -= t * a; swap(a, b);
    u -= t * v; swap(u, v);
  }
  if(b!=1)
    return -1;
  if (u < 0) u += md;
  return u;
}
  
  
 //using namespace __gnu_pbds;
 //#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
//find_by_order(k)  returns iterator to kth element starting from 0;
//order_of_key(k) returns count of elements strictly smaller than k;
//erase,insert same as normal set
//mt19937 mrand(chrono::high_resolution_clock::now().time_since_epoch().count()); //for randomized solution
// int getRand(int l, int r)
// {
//     uniform_int_distribution<int> uid(l, r);
//     return uid(mrand);
// }
 
const int ce=1e9+7;
ll fast_mod(ll input)
{
    return input < ce ? input : input%ce;
}
int n;
int a[100001];
int b[100001];
vector<int> v[100001];
int dp[100001][2];
void dfs(int u, int p)
{
    for(auto i:v[u])
    {
        if(i==p)
            continue;
        dfs(i, u);
    }

    for(auto i: v[u])
    {
        if(i==p)
            continue;
        dp[u][0] += max(dp[i][0] + abs(a[u]- a[i]), dp[i][1] + abs(a[u]- b[i]));
        dp[u][1] += max(dp[i][0] + abs(b[u]- a[i]), dp[i][1] + abs(b[u]- b[i]));
    }
    //cout<<u<<" "<<dp[u][0]<<" "<<dp[u][1]<<endl;
}
int32_t main()
{
    IOS
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    #endif    
    
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(int i = 0; i < n; i++)
            cin>>a[i+1]>>b[i+1];
        for(int i=1;i<=n;i++)
            v[i].clear(),dp[i][0] = 0, dp[i][1]=0;
        for(int i=1;i<=n-1;i++)
        {
            int x, y;
            cin>>x>>y;
            v[x].push_back(y);
            v[y].push_back(x);
            
        }
        dfs(1, 0);
        cout<<max(dp[1][0], dp[1][1])<<endl;
    }
}    