#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef unsigned long long int ull;
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> pll;
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define FORE(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,b,a) for(int i=b;i>a;i--)
#define FORDE(i,b,a) for(ll i=b;i>=a;i--)
#define debug(x) cout<< '>'<<#x<<" : "<<x<<"\n";
#define debug2(x,y) cout<< '>'<<#x<<" : "<<x<<"\n"; cout<< '>'<<#y<<" : "<<y<<"\n";
#define debug3(x,y,z) cout<< '>'<<#x<<" : "<<x<<"\n"; cout<< '>'<<#y<<" : "<<y<<"\n";cout<< '>'<<#z<<" : "<<z<<"\n";
#define umap unordered_map
#define uset unordered_set
#define lb lower_bound
#define ub upper_bound
#define mp make_pair
#define in insert
#define s second
#define f first
#define nn cout<<"\n"
#define pb push_back
#define testcase int t;cin>>t;while(t--)
#define gcd(a,b) __gcd(a,b)
#define maxv INT_MAX
#define minv INT_MIN
#define MOD 1000000007
#define SACHITJAGGI ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(0);
#define here cout<<"I'm here\n";
#define flush cout.flush();
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define setcount(x) __builtin_popcountll(x)
#define ordered_set_single tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
typedef tree<
pair<ll, ll>,
null_type,
less<pair<ll,ll>>,
rb_tree_tag,
tree_order_statistics_node_update> ordered_set_pair;
inline int add(int a,int b) { return (a%MOD + b%MOD + MOD)%MOD; }
inline int mul(int a,int b) { return (a%MOD * b%MOD + MOD)%MOD; }
inline int sub(int a,int b) { return (a%MOD - b%MOD + MOD)%MOD; }
template<class T> void dispvector(vector<T> v){ for(int i=0;i<v.size();i++) cout<<v[i]<<" "; cout << "\n"; }
ll power(ll x, ll y, ll p)
{
ll res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = (res*x) % p;
// y must be even now
y = y>>1; // y = y/2
x = (x*x) % p;
}
return res;
}
ll modInverse(ll n, ll p)
{
return power(n, p-2, p);
}
vector<ll> solve(ll curr,ll parent, vector<vector<ll>>& graph, vector<ll>& mini,vector<ll>& maxi)
{
vector<ll> answ(16,0);
ll m1=maxi[curr];
ll m2=mini[curr];
ll m3=(m1+m2)/2ll;
ll m4=(m1+m2+1ll)/2ll;
// m1
// m2
// m3
vector<ll> strg{m1,m2,m3,m4};
for(auto u:graph[curr])
{
if(u!=parent)
{
vector<ll> tmp=solve(u,curr,graph,mini,maxi);
ll tm1=maxi[u];
ll tm2=mini[u];
ll tm3=(tm1+tm2)/2ll;
ll tm4=(tm1+tm2+1ll)/2ll;
vector<ll> tmp3{tm1,tm2,tm3,tm4};
FOR(i,0,4)
{
FOR(j,0,4)
{
answ[4*i+j]+=(abs(strg[i]-tmp3[j])+tmp[j]);
}
}
// cout<<maxi[curr]<<" "<<mini[curr]<<endl;
// cout<<maxi[u]<<" "<<mini[u]<<endl;
}
}
vector<ll> ans(4,0);
FOR(i,0,16)
{
ans[i/4]=max(answ[i],ans[i/4]);
}
// cout<<curr<<" "<<endl;
// dispvector<ll>(strg);
// dispvector<ll>(ans);
return ans;
}
signed main(int argc, char** argv)
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
SACHITJAGGI
long t=1;
cin>>t;
while(t--)
{
ll n;
cin>>n;
vector<vector<ll>> graph(n+1,vector<ll>());
vector<ll> mini(n+1);
vector<ll> maxi(n+1);
FOR(i,1,n+1) cin>>mini[i]>>maxi[i];
FOR(i,0,n-1)
{
ll a,b;
cin>>a>>b;
graph[a].pb(b);
graph[b].pb(a);
}
vector<ll> tmp= solve(1,-1,graph,mini,maxi);
// dispvector<ll>(tmp);
cout<<max(max(tmp[0],tmp[3]),max(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AgICB1bm9yZGVyZWRfbWFwCiNkZWZpbmUgICAgIHVzZXQgICAgICAgICAgICAgdW5vcmRlcmVkX3NldAojZGVmaW5lICAgICBsYiAgICAgICAgICAgICAgIGxvd2VyX2JvdW5kCiNkZWZpbmUgICAgIHViICAgICAgICAgICAgICAgdXBwZXJfYm91bmQKI2RlZmluZSAgICAgbXAgICAgICAgICAgICAgICBtYWtlX3BhaXIKI2RlZmluZSAgICAgaW4gICAgICAgICAgICAgICBpbnNlcnQKI2RlZmluZSAgICAgcyAgICAgICAgICAgICAgICBzZWNvbmQKI2RlZmluZSAgICAgZiAgICAgICAgICAgICAgICBmaXJzdAojZGVmaW5lICAgICBubiAgICAgICAgICAgICAgIGNvdXQ8PCJcbiIKI2RlZmluZSAgICAgcGIgICAgICAgICAgICAgICBwdXNoX2JhY2sKI2RlZmluZSAgICAgdGVzdGNhc2UgICAgICAgICBpbnQgdDtjaW4+PnQ7d2hpbGUodC0tKQojZGVmaW5lICAgICBnY2QoYSxiKSAgICAgICAgIF9fZ2NkKGEsYikKI2RlZmluZSAgICAgbWF4diAgICAgICAgICAgICBJTlRfTUFYCiNkZWZpbmUgICAgIG1pbnYgICAgICAgICAgICAgSU5UX01JTgojZGVmaW5lICAgICBNT0QgICAgICAgICAgICAgIDEwMDAwMDAwMDcKI2RlZmluZSAgICAgU0FDSElUSkFHR0kgICAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTtjaW4udGllKE5VTEwpO2NvdXQudGllKDApOwojZGVmaW5lICAgICBoZXJlICAgICAgICAgICAgIGNvdXQ8PCJJJ20gaGVyZVxuIjsKI2RlZmluZSAgICAgZmx1c2ggICAgICAgICAgICBjb3V0LmZsdXNoKCk7CiNkZWZpbmUgZW5kbCAnXG4nICAgICAgICAgCiNkZWZpbmUgICAgIGFsbCh4KSAgICAgICAgICAgKHgpLmJlZ2luKCksKHgpLmVuZCgpCiNkZWZpbmUgICAgIHNldGNvdW50KHgpICAgICAgX19idWl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CgogICAgICAgICAgICAgICAgRk9SKGksMCw0KQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIEZPUihqLDAsNCkKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIGFuc3dbNCppK2pdKz0oYWJzKHN0cmdbaV0tdG1wM1tqXSkrdG1wW2pdKTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CgoKICAgICAgICAgICAgLy8gY291dDw8bWF4aVtjdXJyXTw8IiAiPDxtaW5pW2N1cnJdPDxlbmRsOwogICAgICAgICAgICAvLyBjb3V0PDxtYXhpW3VdPDwiICI8PG1pbmlbdV08PGVuZGw7CgogICAgICAgIH0KICAgIH0KICAgIHZlY3RvcjxsbD4gYW5zKDQsMCk7CiAgICBGT1IoaSwwLDE2KQogICAgewogICAgICAgIGFuc1tpLzRdPW1heChhbnN3W2ldLGFuc1tpLzRdKTsKICAgIH0KICAgIC8vIGNvdXQ8PGN1cnI8PCIgIjw8ZW5kbDsKICAgIC8vIGRpc3B2ZWN0b3I8bGw+KHN0cmcpOwogICAgLy8gZGlzcHZlY3RvcjxsbD4oYW5zKTsKCgogICAgcmV0dXJuIGFuczsKCn0KCgpzaWduZWQgbWFpbihpbnQgYXJnYywgY2hhcioqIGFyZ3YpCnsKICAgICNpZm5kZWYgT05MSU5FX0pVREdFCiAgICBmcmVvcGVuKCJpbnB1dC50eHQiLCAiciIsIHN0ZGluKTsKICAgIGZyZW9wZW4oIm91dHB1dC50eHQiLCAidyIsIHN0ZG91dCk7CiAgICAjZW5kaWYKICAgIFNBQ0hJVEpBR0dJCiAgICBsb25nIHQ9MTsKICAgIGNpbj4+dDsKICAgIHdoaWxlKHQtLSkKICAgIHsKICAgICAgICBsbCBuOwogICAgICAgIGNpbj4+bjsKICAgICAgICB2ZWN0b3I8dmVjdG9yPGxsPj4gZ3JhcGgobisxLHZlY3RvcjxsbD4oKSk7CiAgICAgICAgdmVjdG9yPGxsPiB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