#include<bits/stdc++.h>
#define ll long long int
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define sep(i,b,a) for(ll i=b-1;i>=a;i--)
#define hell (ll)(1e9+7)
using namespace std;
vector <ll> adj[100005];
vector <bool> visited(100005,false);
vector <pair<ll,ll> > vp(100005);
ll ans=0;
ll help(vector <ll> &v)
{
vector < pair <ll,ll> > a;
rep(i,0,v.size())
a.push_back(vp[v[i]]);
sort(a.begin(),a.end());
vector <ll> d(a.size()),t(a.size());
rep(i,0,a.size())
{
d[i]=a[i].first;
t[i]=a[i].second;
}
rep(i,1,a.size())
d[i]+=d[i-1];
ll s=0;
s+=d[0];
rep(i,1,a.size())
{
s+=t[i-1];
if(s<d[i])
s=d[i];
}
s+=t.back();
return s;
}
void bfs()
{
queue <ll> q;
q.push(1);
visited[1]=true;
while(!q.empty())
{
int n=q.size();
vector <ll> v;
rep(i,0,n)
{
ll s=q.front();
q.pop();
v.push_back(s);
rep(j,0,adj[s].size())
{
if(!visited[adj[s][j]])
{
q.push(adj[s][j]);
visited[adj[s][j]]=true;
}
}
}
ll x=help(v);
//cout<<x<<"\n";
ans+=x;
}
}
void init()
{
rep(i,0,100005)
{
adj[i].clear();
visited[i]=false;
}
ans=0;
}
void solve()
{
ll n,u,v;
cin>>n;
init();
rep(i,0,n-1)
{
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
rep(i,1,n+1)
{
cin>>u>>v;
vp[i]={u,v};
}
bfs();
cout<<ans<<"\n";
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll t;
cin>>t;
//t=1;
while(t--)
{
solve();
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KCiNkZWZpbmUgbGwgbG9uZyBsb25nIGludAojZGVmaW5lIHJlcChpLGEsYikgZm9yKGxsIGk9YTtpPGI7aSsrKQojZGVmaW5lIHNlcChpLGIsYSkgZm9yKGxsIGk9Yi0xO2k+PWE7aS0tKQojZGVmaW5lIGh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AgIHYucHVzaF9iYWNrKHMpOwogICAgICAgICAgICByZXAoaiwwLGFkaltzXS5zaXplKCkpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGlmKCF2aXNpdGVkW2FkaltzXVtqXV0pCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgcS5wdXNoKGFkaltzXVtqXSk7CiAgICAgICAgICAgICAgICAgICAgdmlzaXRlZFthZGpbc11bal1dPXRydWU7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgbGwgeD1oZWxwKHYpOwogICAgICAgIC8vY291dDw8eDw8IlxuIjsKICAgICAgICBhbnMrPXg7CiAgICB9Cn0KCnZvaWQgaW5pdCgpCnsKICAgIHJlcChpLDAsMTAwMDA1KQogICAgewogICAgICAgIGFkaltpXS5jbGVhcigpOwogICAgICAgIHZpc2l0ZWRbaV09ZmFsc2U7CiAgICB9CiAgICBhbnM9MDsKfQoKdm9pZCBzb2x2ZSgpCnsKICAgIGxsIG4sdSx2OwogICAgY2luPj5uOwogICAgaW5pdCgpOwogICAgcmVwKGksMCxuLTEpCiAgICB7CiAgICAgICAgY2luPj51Pj52OwogICAgICAgIGFkalt1XS5wdXNoX2JhY2sodik7CiAgICAgICAgYWRqW3ZdLnB1c2hfYmFjayh1KTsKICAgIH0KICAgIHJlcChpLDEsbisxKQogICAgewogICAgICAgIGNpbj4+dT4+djsKICAgICAgICB2cFtpXT17dSx2fTsKICAgIH0KICAgIGJmcygpOwogICAgY291dDw8YW5zPDwiXG4iOwp9CgppbnQgbWFpbigpCnsKCWlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCk7CgljaW4udGllKDApOwoJY291dC50aWUoMCk7CglsbCB0OwoJY2luPj50OwoJLy90PTE7Cgl3aGlsZSh0LS0pCgl7CgkJc29sdmUoKTsKCX0KCXJldHVybiAwOwp9Cg==