#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")
#include <bits/stdc++.h>
#include <chrono>
#include <array>
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = 1; i <= n; ++i)
#define ff first
#define ss second
using namespace std;
typedef long long ll;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ", ";
__print(x.second);
cerr << '}';
}
template <typename T> void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x)
cerr << (f++ ? ", " : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
__print(t);
if (sizeof...(v))
cerr << ", ";
_print(v...);
}
#ifdef LOCAL
#define dbg(x...) \
cerr << "\e[91m" << __func__ << ":" << __LINE__ << " [" << #x << "] = ["; \
_print(x); \
cerr << "\e[39m" << endl;
#else
#define dbg(x...)
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll randint(ll lb, ll ub) { return uniform_int_distribution<ll>(lb, ub)(rng); }
const int MAXN = 200100;
ll a[MAXN],b[MAXN],d[MAXN],parr[MAXN];
int par[MAXN];
ll chk[MAXN];
vector<int> adj[MAXN];
int sssss;
bool shit;
int find(int x){
if(x==parr[x]) return x;
return parr[x] = find(parr[x]);
}
void merge(int x, int y){
parr[x] = y;
a[y] = a[y] + max(0ll,a[x]-b[y]);
d[y] = d[y] + d[x];
b[y] = a[y] + d[y];
}
void dfs(int v, int p){
for(auto ne : adj[v]) if(ne!=p){
par[ne] = v; dfs(ne,v);
}
}
bool vis[MAXN];
void solve(){
int n; cin >> n;
sssss=0;
ll kk = 0;
for(int i=2; i<=n; i++){
cin >> a[i] >> b[i];
d[i] = b[i] - a[i];
kk += d[i];
}
rrep(i,n) parr[i] = i;
rrep(i,n) adj[i].clear();
rrep(i,n) chk[i] = 0;
rep(i,n-1){
int a,b; cin>>a>>b; adj[a].pb(b); adj[b].pb(a);
}
rrep(i,n) par[i] = 0;
par[1] = 1;
dfs(1,1);
set<array<ll,2>> s;
rrep(i,n){
if(d[i]>=0 && i!=1) s.insert({a[i],i});
}
// chk -> 내가 바뀔때 1 추가
set<int> fuck;
while(!s.empty()){
auto [ax,x] = *s.begin();
s.erase({ax,x});
if(fuck.find(x)!=fuck.end()) continue;
fuck.insert(x);
int y = par[x];
y = find(y);
merge(x,y);
if(d[y]>=0 && y!=1) s.insert({a[y],y}); //pq.push({a[y],y,chk[y]});
}
rrep(i,n) vis[i]= 0;
s.clear();
rrep(i,n){
int x = find(i);
if(vis[x] || x==1) continue;
vis[x] = 1;
assert(d[x]<0);
//pq.push({-b[x],x,chk[x]});
s.insert({-b[x],x});
}
while(!s.empty()){
auto [mbx,x] = *s.begin();
s.erase({mbx,x});
if(fuck.find(x)!=fuck.end()) continue;
fuck.insert(x);
int y = par[x];
y = find(y);
merge(x,y);
if(y!=1) s.insert({-b[y],y}); //pq.push({-b[y],y,chk[y]});
}
cout << a[1] << "\n";
assert(fuck.size()==n-1);
for(int i=2; i<=n; i++) assert(fuck.find(i)!=fuck.end());
assert(d[1]==kk);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t = 1;
cin >> t;
while(t--) solve();
return 0;
}