#include <bits/stdc++.h>
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;
#define ep emplace
#define eb emplace_back
#define all(x) x.begin(), x.end()
FILE *fin, *fout, *ferr;
i32 read() {
i32 t = 0, f = 0;
char ch = fgetc(fin);
for (; !isdigit(ch); ch = fgetc(fin)) f ^= (ch == '-');
for (; isdigit(ch); ch = fgetc(fin)) t = (t << 1) + (t << 3) + (ch ^ 48);
return f ? -t : t;
}
template<typename T1, typename T2>
constexpr bool chkmax(T1 &a, T2 b) { return a > b ? false : (a = b, true); }
template<typename T1, typename T2>
constexpr bool chkmin(T1 &a, T2 b) { return a > b ? (a = b, true) : false; }
namespace Solution_Of_C24204 {
bool _1;
static const i32 N = 1000005;
i32 n;
struct Node {
i64 a, b, c, d;
Node() = default;
Node(i64 a, i64 b, i64 c, i32 d): a(a), b(b), c(c), d(d) {}
bool operator<(const Node &a) const {
if ((this->c <= 0) != (a.c <= 0)) return this->c > a.c;
if (this->c < 0) return this->b != a.b ? this->b > a.b : (this->a != a.a ? this->a < a.a : this->d < a.d);
if (this->c >= 0) return this->a != a.a ? this->a < a.a : (this->b != a.b ? this->b > a.b : this->d < a.d);
assert(0);
return true;
}
};
i64 a[N], b[N];
i32 fa[N], fat[N], vis[N];
std::vector<i32> e[N];
std::set<Node> s;
bool flag;
bool _2;
i32 find(i32 x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
void dfs(i32 x, i32 f) {
fat[x] = f;
for (i32 y : e[x])
if (y != f)
dfs(y, x);
return void();
}
void solve() {
n = read();
for (i32 i = 2; i <= n; ++i) a[i] = read(), b[i] = read();
for (i32 i = 1; i <= n; ++i) vis[i] = false;
vis[1] = true;
for (i32 i = 1; i <= n; ++i) fa[i] = i;
for (i32 i = 1; i < n; ++i) {
static i32 u, v;
u = read(), v = read();
e[u].eb(v), e[v].eb(u);
}
dfs(1, 0);
for (i32 i = 2; i <= n; ++i) s.ep(Node(a[i], b[i], b[i] - a[i], i));
i64 now = 0, ans = 0;
if (flag) exit(0);
for (i32 i = 1; i < n; ++i) {
auto [A, B, C, D] = *s.begin();
s.erase(s.begin());
i32 u = find(fat[D]);
if (vis[u]) {
now += A;
ans = std::max(ans, now);
now -= B;
vis[D] = true;
} else {
fa[D] = u;
assert(s.find({a[u], b[u], b[u] - a[u], u} != s.end());
s.erase(s.find({a[u], b[u], b[u] - a[u], u}));
if (A > b[u]) {
a[u] = a[u] + A - b[u];
b[u] = B;
} else {
a[u] = a[u];
b[u] = B + b[u] - A;
}
s.ep(a[u], b[u], b[u] - a[u], u);
}
}
fprintf(fout, "%lld\n", ans);
return void();
}
void main() {
fin = stdin, fout = stdout, ferr = stderr;
fprintf(ferr, "%.2lf MB memory\n", 1.0 * (&_1 - &_2) / 1024 / 1024);
i64 Start_Time = clock();
i32 t = read();
while (t--) {
solve();
flag = true;
}
i64 End_Time = clock();
fprintf(ferr, "%lld ms time\n", End_Time - Start_Time);
return void();
}
}
signed main() { return Solution_Of_C24204::main(), 0; }