// In the name of god
#include <bits/stdC++.h>
#pragma GCC optimze("02")
#define F first
#define S second
#define pb push_back
#define all(x) x.begin,x.end()
#define ffiillee freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
using namespace std;
using ll = long long;
const ll maxn =2e5+5,inf=1e18+18,mod=1e9+7;
ll t,n,m,h,a[maxn],b[maxn],p[maxn],c[maxn],d[maxn],par[maxn],yal[maxn],val[maxn],ans;
bool mark[maxn];
vector <ll> adj[maxn],e[maxn];
void dfs(ll u){
	mark[u]=1;
	for(ll i:adj[u]){
		if(!mark[i]){
			d[i]=d[u]+1;
			par[i]=u;
			e[d[i]].pb(i);
			dfs(i);
		}
	}
}
int main(){
	ios::sync_with_stdio(false), cin.tie(0),cout.tie(0);
	cin>>t;
	while(t--){
		cin>>n;
		ans=0;
		for(ll i=1;i<n;i++){
			ll u,v;
			cin>>u>>v;
			u--;v--;
			adj[v].pb(u);
			adj[u].pb(v);
			a[i]=u;b[i]=v;
		}
		cin>>m;
		for(ll i=0;i<m;i++){
			cin>>p[i];
		}
		e[0].pb(0);
		dfs(0);
		h=n;
		while(h>0){
			for(ll i:e[h]){
				c[i]++;
				c[par[i]]+=c[i];
			}
			h--;
		}
		for(ll i=1;i<n;i++){
			ll x=a[i],y=b[i];
			if(d[x]>d[y]){
				swap(x,y);
			}
			yal[i-1]=(c[y]*(n-c[y]));
		}
		sort(yal,yal+(n-1));
		for(int i=0;i<n-1;i++)yal[i]=yal[i]%mod;
		sort(p,p+m);
		if(m>=n-1){
			for(ll i=0;i<n-2;i++){
				val[i]=p[i];
			}
			val[n-2]=1;
			for(ll i=n-2;i<m;i++){
				val[n-2]=(val[n-2]*p[i])%mod;
			}
		}
		else{
			ll x=n-1-m;
			for(ll i=0;i<x;i++){
				val[i]=1;
			}
			for(ll i=x;i<n-1;i++){
				val[i]=p[i-x];
			}
		}
		for(ll i=0;i<n-1;i++){
			ll x=(val[i]*yal[i])%mod;
			ans=(ans+x)%mod;
		}
		cout<<ans%mod<<endl;
		n=max(m,n);
		for(ll i=0;i<=n+10;i++){
			val[i]=0;yal[i]=0;
			a[i]=0;b[i]=0;c[i]=0;d[i]=0;
			mark[i]=0;
			adj[i].clear();
			e[i].clear();
			p[i]=0;
			par[i]=0;
		}
	}
}


// In the name of god
#include <bits/stdC++.h>
#pragma GCC optimze("02")
#define F first
#define S second
#define pb push_back
#define all(x) x.begin,x.end()
#define ffiillee freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
using namespace std;
using ll = long long;
const ll maxn =2e5+5,inf=1e18+18,mod=1e9+7;
ll t,n,m,h,a[maxn],b[maxn],p[maxn],c[maxn],d[maxn],par[maxn],yal[maxn],val[maxn],ans;
bool mark[maxn];
vector <ll> adj[maxn],e[maxn];
void dfs(ll u){
	mark[u]=1;
	for(int i:adj[u]){
		if(!mark[i]){
			d[i]=d[u]+1;
			par[i]=u;
			h=max(h,d[i]);
			e[d[i]].pb(i);
			dfs(i);
		}
	}
}
int main(){
	ios::sync_with_stdio(false), cin.tie(0),cout.tie(0);
	cin>>t;
	while(t--){
		cin>>n;
		h=0;
		ans=0;
		for(int i=1;i<n;i++){
			int u,v;
			cin>>u>>v;
			u--;v--;
			adj[v].pb(u);
			adj[u].pb(v);
			a[i]=u;b[i]=v;
		}
		cin>>m;
		for(int i=0;i<m;i++){
			cin>>p[i];
			p[i]=p[i]%mod;
		}
		e[0].pb(0);
		sort(p,p+m);
		dfs(0);
		while(h>0){
			for(int i:e[h]){
				c[i]++;
				c[par[i]]+=c[i];
			}
			h--;
		}
		for(int i=1;i<n;i++){
			int x=a[i],y=b[i];
			if(d[a[i]]>d[b[i]]){
				swap(x,y);
			}
			yal[i-1]=(c[y]*(n-c[y]))%mod;
		}
		sort(yal,yal+(n-1));
		if(m>=n-1){
			for(int i=0;i<n-2;i++){
				val[i]=p[i];
			}
			val[n-2]=1;
			for(int i=n-2;i<m;i++){
				val[n-2]=(val[n-2]*p[i])%mod;
			}
		}
		else{
			int x=n-1-m;
			for(int i=0;i<x;i++){
				val[i]=1;
			}
			for(int i=x;i<n-1;i++){
				val[i]=p[i-x];
			}
		}
		for(int i=0;i<n-1;i++){
			ll x=(val[i]*yal[i])%mod;
			ans=(ans+x)%mod;
		}
		cout<<ans%mod<<endl;
		n=max(m,n);
		for(int i=0;i<=n;i++){
			val[i]=0;yal[i]=0;
			a[i]=0;b[i]=0;c[i]=0;d[i]=0;
			mark[i]=0;
			adj[i].clear();
			e[i].clear();
			p[i]=0;
			par[i]=0;
		}
	}
}


// In the name of god
#include <bits/stdC++.h>
#pragma GCC optimze("02")
#define F first
#define S second
#define pb push_back
#define all(x) x.begin,x.end()
#define ffiillee freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
using namespace std;
using ll = long long;
const ll maxn=1e5+5,inf=1e18+18,mod=1e9+7;
ll t,n,m,h,a[maxn],b[maxn],p[maxn],c[maxn],d[maxn],par[maxn],yal[maxn],val[maxn],ans;
bool mark[maxn];
vector <ll> adj[maxn],e[maxn];
void dfs(ll u){
	mark[u]=1;
	for(int i:adj[u]){
		if(!mark[i]){
			d[i]=d[u]+1;
			par[i]=u;
			h=max(h,d[i]);
			e[d[i]].pb(i);
			dfs(i);
		}
	}
}
int main(){
	ios::sync_with_stdio(false), cin.tie(0),cout.tie(0);
	cin>>t;
	while(t--){
		cin>>n;
		h=0;
		ans=0;
		for(int i=1;i<n;i++){
			int u,v;
			cin>>u>>v;
			u--;v--;
			adj[v].pb(u);
			adj[u].pb(v);
			a[i]=u;b[i]=v;
		}
		cin>>m;
		for(int i=0;i<m;i++){
			cin>>p[i];
		}
		e[0].pb(0);
		sort(p,p+m);
		dfs(0);
		while(h>0){
			for(int i:e[h]){
				c[i]++;
				c[par[i]]+=c[i];
			}
			h--;
		}
		for(int i=1;i<n;i++){
			int x=a[i],y=b[i];
			if(d[a[i]]>d[b[i]]){
				swap(x,y);
			}
			yal[i-1]=(c[y]*(n-c[y]))%mod;
		}
		sort(yal,yal+(n-1));
		if(m>=n-1){
			for(int i=0;i<n-2;i++){
				val[i]=p[i];
			}
			val[n-2]=1;
			for(int i=n-2;i<m;i++){
				val[n-2]=(val[n-2]*p[i])%mod;
			}
		}
		else{
			int x=n-1-m;
			for(int i=0;i<x;i++){
				val[i]=1;
			}
			for(int i=x;i<n-1;i++){
				val[i]=p[i-x];
			}
		}
		for(int i=0;i<n-1;i++){
			ll x=(val[i]*yal[i])%mod;
			ans=(ans+x)%mod;
		}
		cout<<ans<<endl;
		n=max(m,n);
		for(int i=0;i<=n;i++){
			val[i]=0;yal[i]=0;
			a[i]=0;b[i]=0;c[i]=0;d[i]=0;
			mark[i]=0;
			adj[i].clear();
			e[i].clear();
			p[i]=0;
		}
	}
}


// In the name of god
#include <bits/stdC++.h>
#pragma GCC optimze("02")
#define F first
#define S second
#define pb push_back
#define all(x) x.begin,x.end()
#define ffiillee freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
using namespace std;
using ll = long long;
const ll maxn =2e5+5,inf=1e18+18,mod=1e9+7;
ll t,n,m,h,a[maxn],b[maxn],p[maxn],c[maxn],d[maxn],par[maxn],yal[maxn],val[maxn],ans;
bool mark[maxn];
vector <ll> adj[maxn],e[maxn];
void dfs(ll u){
	mark[u]=1;
	for(ll i:adj[u]){
		if(!mark[i]){
			d[i]=d[u]+1;
			par[i]=u;
			e[d[i]].pb(i);
			dfs(i);
		}
	}
}
int main(){
	ios::sync_with_stdio(false), cin.tie(0),cout.tie(0);
	cin>>t;
	while(t--){
		cin>>n;
		ans=0;
		for(ll i=1;i<n;i++){
			ll u,v;
			cin>>u>>v;
			u--;v--;
			adj[v].pb(u);
			adj[u].pb(v);
			a[i]=u;b[i]=v;
		}
		cin>>m;
		for(ll i=0;i<m;i++){
			cin>>p[i];
		}
		e[0].pb(0);
		sort(p,p+m);
		dfs(0);
		h=n;
		while(h>0){
			for(ll i:e[h]){
				c[i]++;
				c[par[i]]+=c[i];
			}
			h--;
		}
		for(ll i=1;i<n;i++){
			ll x=a[i],y=b[i];
			if(d[x]>d[y]){
				swap(x,y);
			}
			yal[i-1]=(c[y]*(n-c[y]))%mod;
		}
		sort(yal,yal+(n-1));
		sort(p,p+m);
		if(m>=n-1){
			for(ll i=0;i<n-2;i++){
				val[i]=p[i];
			}
			val[n-2]=1;
			for(ll i=n-2;i<m;i++){
				val[n-2]=(val[n-2]*p[i])%mod;
			}
		}
		else{
			ll x=n-1-m;
			for(ll i=0;i<x;i++){
				val[i]=1;
			}
			for(ll i=x;i<n-1;i++){
				val[i]=p[i-x];
			}
		}
		for(ll i=0;i<n-1;i++){
			ll x=(val[i]*yal[i])%mod;
			ans=(ans+x)%mod;
		}
		cout<<ans%mod<<endl;
		n=max(m,n);
		for(ll i=0;i<=n;i++){
			val[i]=0;yal[i]=0;
			a[i]=0;b[i]=0;c[i]=0;d[i]=0;
			mark[i]=0;
			adj[i].clear();
			e[i].clear();
			p[i]=0;
			par[i]=0;
		}
	}
}


// In the name of god
#include <bits/stdC++.h>
#pragma GCC optimze("02")
#define F first
#define S second
#define pb push_back
#define all(x) x.begin,x.end()
#define ffiillee freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
using namespace std;
using ll = long long;
const ll maxn =2e5+5,inf=1e18+18,mod=1e9+7;
ll t,n,m,h,a[maxn],b[maxn],p[maxn],c[maxn],d[maxn],par[maxn],yal[maxn],val[maxn],ans;
bool mark[maxn];
vector <ll> adj[maxn],e[maxn];
void dfs(ll u){
	mark[u]=1;
	for(ll i:adj[u]){
		if(!mark[i]){
			d[i]=d[u]+1;
			par[i]=u;
			e[d[i]].pb(i);
			dfs(i);
		}
	}
}
int main(){
	ios::sync_with_stdio(false), cin.tie(0),cout.tie(0);
	cin>>t;
	while(t--){
		cin>>n;
		ans=0;
		for(ll i=1;i<n;i++){
			ll u,v;
			cin>>u>>v;
			u--;v--;
			adj[v].pb(u);
			adj[u].pb(v);
			a[i]=u;b[i]=v;
		}
		cin>>m;
		for(ll i=0;i<m;i++){
			cin>>p[i];
		}
		e[0].pb(0);
		dfs(0);
		h=n;
		while(h>0){
			for(ll i:e[h]){
				c[i]++;
				c[par[i]]+=c[i];
			}
			h--;
		}
		for(ll i=1;i<n;i++){
			ll x=a[i],y=b[i];
			if(d[x]>d[y]){
				swap(x,y);
			}
			yal[i-1]=(c[y]*(n-c[y]))%mod;
		}
		sort(yal,yal+(n-1));
		sort(p,p+m);
		if(m>=n-1){
			for(ll i=0;i<n-2;i++){
				val[i]=p[i];
			}
			val[n-2]=1;
			for(ll i=n-2;i<m;i++){
				val[n-2]=(val[n-2]*p[i])%mod;
			}
		}
		else{
			ll x=n-1-m;
			for(ll i=0;i<x;i++){
				val[i]=1;
			}
			for(ll i=x;i<n-1;i++){
				val[i]=p[i-x];
			}
		}
		for(ll i=0;i<n-1;i++){
			ll x=(val[i]*yal[i])%mod;
			ans=(ans+x)%mod;
		}
		cout<<ans%mod<<endl;
		n=max(m,n);
		for(ll i=0;i<=n+10;i++){
			val[i]=0;yal[i]=0;
			a[i]=0;b[i]=0;c[i]=0;d[i]=0;
			mark[i]=0;
			adj[i].clear();
			e[i].clear();
			p[i]=0;
			par[i]=0;
		}
	}
}


// In the name of god
#include <bits/stdC++.h>
#pragma GCC optimze("02")
#define F first
#define S second
#define pb push_back
#define all(x) x.begin,x.end()
#define ffiillee freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
using namespace std;
using ll = long long;
const ll maxn =2e5+5,inf=1e18+18,mod=1e9+7;
ll t,n,m,h,a[maxn],b[maxn],p[maxn],c[maxn],d[maxn],par[maxn],yal[maxn],val[maxn],ans;
bool mark[maxn];
vector <ll> adj[maxn],e[maxn];
void dfs(ll u){
	mark[u]=1;
	for(ll i:adj[u]){
		if(!mark[i]){
			d[i]=d[u]+1;
			par[i]=u;
			e[d[i]].pb(i);
			dfs(i);
		}
	}
}
int main(){
	ios::sync_with_stdio(false), cin.tie(0),cout.tie(0);
	cin>>t;
	while(t--){
		cin>>n;
		ans=0;
		for(ll i=0;i<n-1;i++){
			ll u,v;
			cin>>u>>v;
			u--;v--;
			adj[v].pb(u);
			adj[u].pb(v);
			a[i]=u;b[i]=v;
		}
		cin>>m;
		for(ll i=0;i<m;i++){
			cin>>p[i];
		}
		e[0].pb(0);
		dfs(0);
		h=n;
		while(h>0){
			for(ll i:e[h]){
				c[i]++;
				c[par[i]]+=c[i];
			}
			h--;
		}
		for(ll i=0;i<n-1;i++){
			ll x=a[i],y=b[i];
			if(par[x]==y){
				swap(x,y);
			}
			yal[i]=max((c[y]*(n-c[y])),n-1);
		}
		sort(yal,yal+(n-1));
		sort(p,p+m);
		if(m>=n-1){
			for(ll i=0;i<n-2;i++){
				val[i]=p[i];
			}
			val[n-2]=1;
			for(ll i=n-2;i<m;i++){
				val[n-2]=(val[n-2]*p[i])%mod;
			}
		}
		else{
			ll x=n-1-m;
			for(ll i=0;i<x;i++){
				val[i]=1;
			}
			for(ll i=x;i<n-1;i++){
				val[i]=p[i-x];
			}
		}
		for(ll i=0;i<n-1;i++){
			ll x=(val[i]*yal[i])%mod;
			ans=(ans+x)%mod;
		}
		cout<<ans%mod<<endl;
		n=max(m,n);
		for(ll i=0;i<=n+10;i++){
			val[i]=0;yal[i]=0;
			a[i]=0;b[i]=0;c[i]=0;d[i]=0;
			mark[i]=0;
			adj[i].clear();
			e[i].clear();
			p[i]=0;
			par[i]=0;
		}
	}
}


// In the name of god
#include <bits/stdC++.h>
#pragma GCC optimze("02")
#define F first
#define S second
#define pb push_back
#define all(x) x.begin,x.end()
#define ffiillee freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
using namespace std;
using ll = long long;
const ll maxn=1e5+5,inf=1e18+18,mod=1e9+7;
ll t,n,m,h,a[maxn],b[maxn],p[maxn],c[maxn],d[maxn],par[maxn],yal[maxn],val[maxn],ans;
bool mark[maxn];
vector <ll> adj[maxn],e[maxn];
void dfs(ll u){
	mark[u]=1;
	for(int i:adj[u]){
		if(!mark[i]){
			d[i]=d[u]+1;
			par[i]=u;
			h=max(h,d[i]);
			e[d[i]].pb(i);
			dfs(i);
		}
	}
}
int main(){
	ios::sync_with_stdio(false), cin.tie(0),cout.tie(0);
	cin>>t;
	while(t--){
		cin>>n;
		h=0;
		ans=0;
		for(int i=1;i<n;i++){
			int u,v;
			cin>>u>>v;
			u--;v--;
			adj[v].pb(u);
			adj[u].pb(v);
			a[i]=u;b[i]=v;
		}
		cin>>m;
		for(int i=0;i<m;i++){
			cin>>p[i];
		}
		e[0].pb(0);
		sort(p,p+m);
		dfs(0);
		while(h>0){
			for(int i:e[h]){
				c[i]++;
				c[par[i]]+=c[i];
			}
			h--;
		}
		for(int i=1;i<n;i++){
			int x=a[i],y=b[i];
			if(d[x]>d[y]){
				swap(x,y);
			}
			yal[i-1]=(c[y]*(n-c[y]))%mod;
		}
		sort(yal,yal+(n-2));
		if(m>=n-1){
			for(int i=0;i<n-2;i++){
				val[i]=p[i];
			}
			val[n-2]=1;
			for(int i=n-2;i<m;i++){
				val[n-2]=(val[n-2]*p[i])%mod;
			}
		}
		else{
			int x=n-1-m;
			for(int i=0;i<x;i++){
				val[i]=1;
			}
			for(int i=x;i<n-1;i++){
				val[i]=p[i-x];
			}
		}
		for(int i=0;i<n-1;i++){
			ll x=(val[i]*yal[i])%mod;
			ans=(ans+x)%mod;
		}
		cout<<ans<<endl;
		n=max(m,n);
		for(int i=0;i<=n;i++){
			val[i]=0;yal[i]=0;
			a[i]=0;b[i]=0;c[i]=0;d[i]=0;
			mark[i]=0;
			adj[i].clear();
			e[i].clear();
			p[i]=0;
		}
	}
}


// In the name of god
#include <bits/stdC++.h>
#pragma GCC optimze("02")
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(),x.end()
#define fileoi freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
using namespace std;
using ll = long long;
const ll maxn=1e3+5,inf=1e18+18,mod=1e9+7;
ll t,n,m,h[maxn],par[maxn][20],rote;
vector <ll> adj[maxn];
void dfs(int u){
	for(int i=1;i<10;i++){
		int v=par[u][i-1];
		if(par[v][i-1]==0)break;
		par[u][i]=par[v][i-1];
	}
	for(int i:adj[u]){
		if(i!=par[u][0]){
			par[i][0]=u;
			h[i]=h[u]+1;
			dfs(i);
		}
	}
}
int main(){
	ios::sync_with_stdio(false), cin.tie(0) , cout.tie(0);
	cin>>t;
	for(int j=1;j<=t;j++){
		cin>>n;
		for(int i=1;i<=n;i++){
			cin>>m;
			while(m--){
				ll u;
				cin>>u;
				adj[u].pb(i);
				adj[i].pb(u);
				par[u][0]=i;
			}
		}
		for(int i=1;i<=n;i++){
			if(par[i][0]==0){
				rote=i;
				break;
			}
		}
		dfs(rote);
		ll q;
		cout<<"Case "<<j<<':'<<endl;
		while(q--){
			int u,v;
			cin>>u>>v;

		}
	}
}


// In the name of god
#include <bits/stdC++.h>
#pragma GCC optimze("02")
#define F first
#define S second
#define pb push_back
#define all(x) x.begin,x.end()
#define ffiillee freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
using namespace std;
using ll = long long;
const ll maxn =2e5+5,inf=1e18+18,mod=1e9+7;
ll t,n,m,h,a[maxn],b[maxn],p[maxn],c[maxn],d[maxn],par[maxn],yal[maxn],val[maxn],ans;
bool mark[maxn];
vector <ll> adj[maxn],e[maxn];
void dfs(ll u){
	mark[u]=1;
	for(ll i:adj[u]){
		if(!mark[i]){
			d[i]=d[u]+1;
			par[i]=u;
			e[d[i]].pb(i);
			dfs(i);
		}
	}
}
int main(){
	ios::sync_with_stdio(false), cin.tie(0),cout.tie(0);
	cin>>t;
	while(t--){
		cin>>n;
		ans=0;
		for(ll i=1;i<n;i++){
			ll u,v;
			cin>>u>>v;
			u--;v--;
			adj[v].pb(u);
			adj[u].pb(v);
			a[i]=u;b[i]=v;
		}
		cin>>m;
		for(ll i=0;i<m;i++){
			cin>>p[i];
		}
		e[0].pb(0);
		sort(p,p+m);
		dfs(0);
		h=n;
		while(h>0){
			for(ll i:e[h]){
				c[i]++;
				c[par[i]]+=c[i];
			}
			h--;
		}
		for(ll i=1;i<n;i++){
			ll x=a[i],y=b[i];
			if(d[x]>d[y]){
				swap(x,y);
			}
			yal[i-1]=(c[y]*(n-c[y]))%mod;
		}
		sort(yal,yal+(n-1));
		sort(p,p+m);
		if(m>=n-1){
			for(ll i=0;i<n-2;i++){
				val[i]=p[i];
			}
			val[n-2]=1;
			for(ll i=n-2;i<m;i++){
				val[n-2]=(val[n-2]*p[i])%mod;
			}
		}
		else{
			ll x=n-1-m;
			for(ll i=0;i<x;i++){
				val[i]=1;
			}
			for(ll i=x;i<n-1;i++){
				val[i]=p[i-x];
			}
		}
		sort(val,val+(n-1));
		for(ll i=0;i<n-1;i++){
			ll x=(val[i]*yal[i])%mod;
			ans=(ans+x)%mod;
		}
		cout<<ans%mod<<endl;
		n=max(m,n);
		for(ll i=0;i<=n;i++){
			val[i]=0;yal[i]=0;
			a[i]=0;b[i]=0;c[i]=0;d[i]=0;
			mark[i]=0;
			adj[i].clear();
			e[i].clear();
			p[i]=0;
			par[i]=0;
		}
	}
}


/*
    -ensure correct output format
    -ensure printing required output
    -reread the problem statement
  */

#include<bits/stdc++.h>
using namespace std;
typedef long long ull;
typedef unsigned long long ll;
typedef pair<ll,ll>pll;
typedef pair<ll,pair<ll,ll>>plll;
#define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL));
#define vll(v) v.begin(),v.end()
#define all(x) x.rbegin(),x.rend()
#define min3(a, b, c)   min(a, min(b, c))
#define max3(a, b, c)   max(a, max(b, c))
#define F first
#define S second
#define in  freopen("input.txt", "r", stdin)
#define out  freopen("output.txt", "w", stdout)
#define minheap int,vector<int>,greater<int>
#define pb push_back
#define eb emplace_back
#define ischar(x)  (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z'))
#define isvowel(ch) ((ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')||(ch=='A'|| ch=='E' || ch=='I'|| ch=='O'|| ch=='U'))
#define bug cout<<"BUG"<<endl;
const int Max = 1e5 + 10;
const int Mod = 1e9 + 7;
const double PI  =3.141592653589793238463;
bool compare(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
    return (a.first > b.first);
}
ll lcm(ll a,ll b)
{
    if(a==0 || b==0)return 0;

    return a/__gcd(a,b)*b;
}

void input(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)cin>>ara[i];
}

void print(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)
        cout<<ara[i]<<" ";
    cout<<endl;
}

vector<ll>adj[Max+2];

ll dp[Max+2],n;
vector<ll>v;

void clr(ll n)
{
    for(ll i=0; i<=n+1; i++)
    {
        adj[i].clear();
        dp[i]=0;
    }

    v.clear();
}

ll dfs(ll s,ll p)
{
    for(auto x : adj[s])
    {
        if(x!=p)
        {

            dp[s]+=dfs(x,s);
        }
    }

   dp[s]++;

    return dp[s];
}

void Dfs(ll s,ll p)
{
    for(auto x : adj[s])
    {
        if(x!=p)
        {
            Dfs(x,s);

            ll a=n-(dp[x]);

            if(a==0 ||dp[x]==0)v.eb(n-1);

            else v.eb( (a*dp[x])%Mod);

        }
    }

}

int main()
{

    fastread();

    ll i,j,m,p,a,sum=0,k,t,b,c,d,cnt=0,q,l,r;

    bool flag=false;

    string str;

    cin>>t;

    while(t--)
    {
        cin>>n;

        clr(n);

        for(i=1; i<n; i++)
        {
            cin>>a>>b;

            adj[a].eb(b);

            adj[b].eb(a);
        }

        cin>>m;

        ll cost[m+2];

        input(cost,m);

        dfs(1,1);

        Dfs(1,1);

        sort(vll(v),greater<ll>());

        if(n<m)
        {
              sort(cost,cost+m,greater<ll>());

              for(i=n; i<m; i++)
              {
                  cost[0]=(cost[0]*cost[i])%Mod;
              }

              m=n;

        }

       else  sort(cost,cost+m,greater<ll>());

        sum=0;
        for(ll i=0; i<m; i++)
        {
            sum=(sum+(v[i]*cost[i])%Mod)%Mod;

           // cout<<sum<<" "<<i<<endl;
        }

        for(i=m; i<n-1; i++)
        {
            sum=(sum+((n-1)*1ll)%Mod)%Mod;
        }

        cout<<sum%Mod<<endl;
    }
}


/*
    -ensure correct output format
    -ensure printing required output
    -reread the problem statement
  */

#include<bits/stdc++.h>
using namespace std;
typedef long long ull;
typedef unsigned long long ll;
typedef pair<ll,ll>pll;
typedef pair<ll,pair<ll,ll>>plll;
#define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL));
#define vll(v) v.begin(),v.end()
#define all(x) x.rbegin(),x.rend()
#define min3(a, b, c)   min(a, min(b, c))
#define max3(a, b, c)   max(a, max(b, c))
#define F first
#define S second
#define in  freopen("input.txt", "r", stdin)
#define out  freopen("output.txt", "w", stdout)
#define minheap int,vector<int>,greater<int>
#define pb push_back
#define eb emplace_back
#define ischar(x)  (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z'))
#define isvowel(ch) ((ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')||(ch=='A'|| ch=='E' || ch=='I'|| ch=='O'|| ch=='U'))
#define bug cout<<"BUG"<<endl;
const int Max = 1e5 + 10;
const int Mod = 1e9 + 7;
const double PI  =3.141592653589793238463;
bool compare(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
    return (a.first > b.first);
}
ll lcm(ll a,ll b)
{
    if(a==0 || b==0)return 0;

    return a/__gcd(a,b)*b;
}

void input(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)cin>>ara[i];
}

void print(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)
        cout<<ara[i]<<" ";
    cout<<endl;
}

vector<ll>adj[Max+2];

ll dp[Max+2],n;
vector<ll>v;

void clr(ll n)
{
    for(ll i=0; i<=n+1; i++)
    {
        adj[i].clear();
        dp[i]=0;
    }

    v.clear();
}

ll dfs(ll s,ll p)
{
    for(auto x : adj[s])
    {
        if(x!=p)
        {

            dp[s]+=dfs(x,s);
        }
    }

   dp[s]++;

    return dp[s];
}

void Dfs(ll s,ll p)
{
    for(auto x : adj[s])
    {
        if(x!=p)
        {
            Dfs(x,s);

            ll a=n-(dp[x]);

          /*  if(a==0 ||dp[x]==0)v.eb(n-1);

            else */v.eb( (a*dp[x]));

        }
    }

}

int main()
{

    fastread();

    ll i,j,m,p,a,sum=0,k,t,b,c,d,cnt=0,q,l,r;

    bool flag=false;

    string str;

    cin>>t;

    while(t--)
    {
        cin>>n;

        clr(n);

        for(i=1; i<n; i++)
        {
            cin>>a>>b;

            adj[a].eb(b);

            adj[b].eb(a);
        }

        cin>>m;

        ll cost[m+2];

        input(cost,m);

        dfs(1,1);

        Dfs(1,1);

        sort(vll(v),greater<ll>());

        if(n<m)
        {
              sort(cost,cost+m,greater<ll>());

              for(i=n; i<m; i++)
              {
                  cost[0]=(cost[0]*cost[i])%Mod;
              }

        }

       else  sort(cost,cost+m,greater<ll>());

        sum=0;
        for(ll i=0; i<m; i++)
        {
            sum=(sum+(v[i]*cost[i])%Mod)%Mod;

           // cout<<sum<<" "<<i<<endl;
        }

        for(i=m; i<n-1; i++)
        {
            sum=(sum+((n-1)*1ll)%Mod)%Mod;
        }

        cout<<sum%Mod<<endl;
    }
}


/*
    -ensure correct output format
    -ensure printing required output
    -reread the problem statement
  */

#include<bits/stdc++.h>
using namespace std;
typedef long long ull;
typedef unsigned long long ll;
typedef pair<ll,ll>pll;
typedef pair<ll,pair<ll,ll>>plll;
#define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL));
#define vll(v) v.begin(),v.end()
#define all(x) x.rbegin(),x.rend()
#define min3(a, b, c)   min(a, min(b, c))
#define max3(a, b, c)   max(a, max(b, c))
#define F first
#define S second
#define in  freopen("input.txt", "r", stdin)
#define out  freopen("output.txt", "w", stdout)
#define minheap int,vector<int>,greater<int>
#define pb push_back
#define eb emplace_back
#define ischar(x)  (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z'))
#define isvowel(ch) ((ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')||(ch=='A'|| ch=='E' || ch=='I'|| ch=='O'|| ch=='U'))
#define bug cout<<"BUG"<<endl;
const int Max = 1e5 + 10;
const int Mod = 1e9 + 7;
const double PI  =3.141592653589793238463;
bool compare(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
    return (a.first > b.first);
}
ll lcm(ll a,ll b)
{
    if(a==0 || b==0)return 0;

    return a/__gcd(a,b)*b;
}

void input(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)cin>>ara[i];
}

void print(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)
        cout<<ara[i]<<" ";
    cout<<endl;
}

vector<ll>adj[Max+2];

ll dp[Max+2],n;
vector<ll>v;

void clr(ll n)
{
    for(ll i=0; i<=n+1; i++)
    {
        adj[i].clear();
        dp[i]=0;
    }

    v.clear();
}

ll dfs(ll s,ll p)
{
    for(auto x : adj[s])
    {
        if(x!=p)
        {

            dp[s]+=dfs(x,s);
        }
    }

   dp[s]++;

    return dp[s];
}

void Dfs(ll s,ll p)
{
    for(auto x : adj[s])
    {
        if(x!=p)
        {
            Dfs(x,s);

            ll a=n-(dp[x]);

            if(a==0 ||dp[x]==0)v.eb(n-1);

            else v.eb( (a*dp[x])%Mod);

        }
    }

}

int main()
{

    fastread();

    ll i,j,m,p,a,sum=0,k,t,b,c,d,cnt=0,q,l,r;

    bool flag=false;

    string str;

    cin>>t;

    while(t--)
    {
        cin>>n;

        clr(n);

        for(i=1; i<n; i++)
        {
            cin>>a>>b;

            adj[a].eb(b);

            adj[b].eb(a);
        }

        cin>>m;

        ll cost[m+2];

        input(cost,m);

        dfs(1,1);

        Dfs(1,1);

        sort(vll(v),greater<ll>());

        if(n<m)
        {
              sort(cost,cost+m);

              j=0;
              for(i=n; i<m; i++)
              {

                  if( (cost[j]*cost[i])>Mod)
                  {
                      j++;
                  j%=n;
                  }
                   cost[j]=(cost[j]*cost[i])%Mod;
              }

             // sort(cost,cost+n,greater)

              m=n;

        }

        sort(cost,cost+m,greater<ll>());

        sum=0;
        for(ll i=0; i<m; i++)
        {
            sum=(sum+(v[i]*cost[i])%Mod)%Mod;

           // cout<<sum<<" "<<i<<endl;
        }

        for(i=m; i<n-1; i++)
        {
            sum=(sum+((v[i])*1ll)%Mod)%Mod;
        }

        cout<<sum%Mod<<endl;
    }
}


/*
    -ensure correct output format
    -ensure printing required output
    -reread the problem statement
  */

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll>pll;
typedef pair<ll,pair<ll,ll>>plll;
#define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL));
#define vll(v) v.begin(),v.end()
#define all(x) x.rbegin(),x.rend()
#define min3(a, b, c)   min(a, min(b, c))
#define max3(a, b, c)   max(a, max(b, c))
#define F first
#define S second
#define in  freopen("input.txt", "r", stdin)
#define out  freopen("output.txt", "w", stdout)
#define minheap int,vector<int>,greater<int>
#define pb push_back
#define eb emplace_back
#define ischar(x)  (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z'))
#define isvowel(ch) ((ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')||(ch=='A'|| ch=='E' || ch=='I'|| ch=='O'|| ch=='U'))
#define bug cout<<"BUG"<<endl;
const int Max = 1e5 + 10;
const int Mod = 1e9 + 7;
const double PI  =3.141592653589793238463;
bool compare(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
    return (a.first > b.first);
}
ll lcm(ll a,ll b)
{
    if(a==0 || b==0)return 0;

    return a/__gcd(a,b)*b;
}

void input(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)cin>>ara[i];
}

void print(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)
        cout<<ara[i]<<" ";
    cout<<endl;
}

vector<ll>adj[Max+2];

ll dp[Max+2],n;
vector<ll>v;

void clr(ll n)
{
    for(ll i=0; i<=n; i++)
    {
        adj[i].clear();
        dp[i]=0;
    }

    v.clear();
}

ll dfs(ll s,ll p)
{
    for(auto x : adj[s])
    {
        if(x!=p)
        {

            dp[s]+=dfs(x,s);
        }
    }

    dp[s]++;

    return dp[s];
}

void Dfs(ll s,ll p)
{
    for(auto x : adj[s])
    {
        if(x!=p)
        {
            Dfs(x,s);

            ll a=n-(dp[x]);

          //  if(a==0 ||dp[x]==0)v.eb(n-1);

             v.eb( (a*dp[x])%Mod);

        }
    }

}

int main()
{

    fastread();

    ll i,j,m,p,a,sum=0,k,t,b,c,d,cnt=0,q,l,r;

    bool flag=false;

    string str;

    cin>>t;

    while(t--)
    {
        cin>>n;

        clr(n);

        for(i=1; i<n; i++)
        {
            cin>>a>>b;

            adj[a].eb(b);

            adj[b].eb(a);
        }

        cin>>m;

        ll cost[m+2];

        input(cost,m);

        dfs(1,1);

        Dfs(1,1);

        sort(vll(v),greater<ll>());

        sort(cost,cost+m,greater<ll>());

        for(auto x : v)
            cout<<x<<" ";

        cout<<endl;

        sum=0;
        for(ll i=0; i<m; i++)
        {
            sum=(sum+(v[i]*cost[i])%Mod)%Mod;

           // cout<<sum<<" "<<i<<endl;
        }

        for(i=m; i<n-1; i++)
        {
            sum=(sum+((n-1)*1ll)%Mod)%Mod;
        }

        cout<<sum%Mod<<endl;
    }
}


/*
    -ensure correct output format
    -ensure printing required output
    -reread the problem statement
  */

#include<bits/stdc++.h>
using namespace std;
typedef long long ull;
typedef unsigned long long ll;
typedef pair<ll,ll>pll;
typedef pair<ll,pair<ll,ll>>plll;
#define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL));
#define vll(v) v.begin(),v.end()
#define all(x) x.rbegin(),x.rend()
#define min3(a, b, c)   min(a, min(b, c))
#define max3(a, b, c)   max(a, max(b, c))
#define F first
#define S second
#define in  freopen("input.txt", "r", stdin)
#define out  freopen("output.txt", "w", stdout)
#define minheap int,vector<int>,greater<int>
#define pb push_back
#define eb emplace_back
#define ischar(x)  (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z'))
#define isvowel(ch) ((ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')||(ch=='A'|| ch=='E' || ch=='I'|| ch=='O'|| ch=='U'))
#define bug cout<<"BUG"<<endl;
const int Max = 1e5 + 10;
const int Mod = 1e9 + 7;
const double PI  =3.141592653589793238463;
bool compare(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
    return (a.first > b.first);
}
ll lcm(ll a,ll b)
{
    if(a==0 || b==0)return 0;

    return a/__gcd(a,b)*b;
}

void input(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)cin>>ara[i];
}

void print(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)
        cout<<ara[i]<<" ";
    cout<<endl;
}

vector<ll>adj[Max+2];

ll dp[Max+2],n;
vector<ll>v;

void clr(ll n)
{
    for(ll i=0; i<=n+1; i++)
    {
        adj[i].clear();
        dp[i]=0;
    }

    v.clear();
}

ll dfs(ll s,ll p)
{
    for(auto x : adj[s])
    {
        if(x!=p)
        {

            dp[s]+=dfs(x,s);
        }
    }

   dp[s]++;

    return dp[s];
}

void Dfs(ll s,ll p)
{
    for(auto x : adj[s])
    {
        if(x!=p)
        {
            Dfs(x,s);

            ll a=n-(dp[x]);

            if(a==0 ||dp[x]==0)v.eb(n-1);

            else v.eb( (a*dp[x])%Mod);

        }
    }

}

int main()
{

    fastread();

    ll i,j,m,p,a,sum=0,k,t,b,c,d,cnt=0,q,l,r;

    bool flag=false;

    string str;

    cin>>t;

    while(t--)
    {
        cin>>n;

        clr(n);

        for(i=1; i<n; i++)
        {
            cin>>a>>b;

            adj[a].eb(b);

            adj[b].eb(a);
        }

        cin>>m;

        ll cost[m+2];

        input(cost,m);

        dfs(1,1);

        Dfs(1,1);

        sort(vll(v),greater<ll>());

        if(n<m)
        {
              sort(cost,cost+m);

              j=0;
              for(i=n; i<m; i++)
              {

                  if( (cost[j]*cost[i])>Mod)
                  {
                      j++;
                  j%=n;
                  }
                   cost[j]=(cost[j]*cost[i])%Mod;
              }

             // sort(cost,cost+n,greater)

              m=n;

        }

        sort(cost,cost+m,greater<ll>());

        sum=0;
        for(ll i=0; i<m; i++)
        {
            sum=(sum+(v[i]*cost[i])%Mod)%Mod;

           // cout<<sum<<" "<<i<<endl;
        }

        for(i=m; i<n-1; i++)
        {
            sum=(sum+((n-1)*1ll)%Mod)%Mod;
        }

        cout<<sum%Mod<<endl;
    }
}


/*
    -ensure correct output format
    -ensure printing required output
    -reread the problem statement
  */

#include<bits/stdc++.h>
using namespace std;
typedef long long ull;
typedef unsigned long long ll;
typedef pair<ll,ll>pll;
typedef pair<ll,pair<ll,ll>>plll;
#define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL));
#define vll(v) v.begin(),v.end()
#define all(x) x.rbegin(),x.rend()
#define min3(a, b, c)   min(a, min(b, c))
#define max3(a, b, c)   max(a, max(b, c))
#define F first
#define S second
#define in  freopen("input.txt", "r", stdin)
#define out  freopen("output.txt", "w", stdout)
#define minheap int,vector<int>,greater<int>
#define pb push_back
#define eb emplace_back
#define ischar(x)  (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z'))
#define isvowel(ch) ((ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')||(ch=='A'|| ch=='E' || ch=='I'|| ch=='O'|| ch=='U'))
#define bug cout<<"BUG"<<endl;
const int Max = 1e5 + 10;
const int Mod = 1e9 + 7;
const double PI  =3.141592653589793238463;
bool compare(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
    return (a.first > b.first);
}
ll lcm(ll a,ll b)
{
    if(a==0 || b==0)return 0;

    return a/__gcd(a,b)*b;
}

void input(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)cin>>ara[i];
}

void print(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)
        cout<<ara[i]<<" ";
    cout<<endl;
}

vector<ll>adj[Max+2];

ll dp[Max+2],n;
vector<ll>v;

void clr(ll n)
{
    for(ll i=0; i<=n+1; i++)
    {
        adj[i].clear();
        dp[i]=0;
    }

    v.clear();
}

ll dfs(ll s,ll p)
{
    for(auto x : adj[s])
    {
        if(x!=p)
        {

            dp[s]+=dfs(x,s);
        }
    }

   dp[s]++;

    return dp[s];
}

void Dfs(ll s,ll p)
{
    for(auto x : adj[s])
    {
        if(x!=p)
        {
            Dfs(x,s);

            ll a=n-(dp[x]);

          /*  if(a==0 ||dp[x]==0)v.eb(n-1);

            else */v.eb( (a*dp[x]));

        }
    }

}

int main()
{

    fastread();

    ll i,j,m,p,a,sum=0,k,t,b,c,d,cnt=0,q,l,r;

    bool flag=false;

    string str;

    cin>>t;

    while(t--)
    {
        cin>>n;

        clr(n);

        for(i=1; i<n; i++)
        {
            cin>>a>>b;

            adj[a].eb(b);

            adj[b].eb(a);
        }

        cin>>m;

        ll cost[m+2];

        input(cost,m);

        dfs(1,1);

        Dfs(1,1);

        sort(vll(v),greater<ll>());

        if(n<m)
        {
              sort(cost,cost+m,greater<ll>());

              for(i=n; i<m; i++)
              {
                  cost[n-1]=(cost[0]*cost[i])%Mod;
              }

        }

       else  sort(cost,cost+m,greater<ll>());

        sum=0;
        for(ll i=0; i<m; i++)
        {
            sum=(sum+(v[i]*cost[i])%Mod)%Mod;

           // cout<<sum<<" "<<i<<endl;
        }

        for(i=m; i<n-1; i++)
        {
            sum=(sum+((n-1)*1ll)%Mod)%Mod;
        }

        cout<<sum%Mod<<endl;
    }
}


/*
    -ensure correct output format
    -ensure printing required output
    -reread the problem statement
  */

#include<bits/stdc++.h>
using namespace std;
typedef long long ull;
typedef unsigned long long ll;
typedef pair<ll,ll>pll;
typedef pair<ll,pair<ll,ll>>plll;
#define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL));
#define vll(v) v.begin(),v.end()
#define all(x) x.rbegin(),x.rend()
#define min3(a, b, c)   min(a, min(b, c))
#define max3(a, b, c)   max(a, max(b, c))
#define F first
#define S second
#define in  freopen("input.txt", "r", stdin)
#define out  freopen("output.txt", "w", stdout)
#define minheap int,vector<int>,greater<int>
#define pb push_back
#define eb emplace_back
#define ischar(x)  (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z'))
#define isvowel(ch) ((ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')||(ch=='A'|| ch=='E' || ch=='I'|| ch=='O'|| ch=='U'))
#define bug cout<<"BUG"<<endl;
const int Max = 1e5 + 10;
const int Mod = 1e9 + 7;
const double PI  =3.141592653589793238463;
bool compare(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
    return (a.first > b.first);
}
ll lcm(ll a,ll b)
{
    if(a==0 || b==0)return 0;

    return a/__gcd(a,b)*b;
}

void input(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)cin>>ara[i];
}

void print(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)
        cout<<ara[i]<<" ";
    cout<<endl;
}

vector<ll>adj[Max+2];

ll dp[Max+2],n;
vector<ll>v;

void clr(ll n)
{
    for(ll i=0; i<=n+1; i++)
    {
        adj[i].clear();
        dp[i]=0;
    }

    v.clear();
}

ll dfs(ll s,ll p)
{
    for(auto x : adj[s])
    {
        if(x!=p)
        {

            dp[s]+=dfs(x,s);
        }
    }

   dp[s]++;

    return dp[s];
}

void Dfs(ll s,ll p)
{
    for(auto x : adj[s])
    {
        if(x!=p)
        {
            Dfs(x,s);

            ll a=n-(dp[x]);

          /*  if(a==0 ||dp[x]==0)v.eb(n-1);

            else */v.eb( (a*dp[x]));

        }
    }

}

int main()
{

    fastread();

    ll i,j,m,p,a,sum=0,k,t,b,c,d,cnt=0,q,l,r;

    bool flag=false;

    string str;

    cin>>t;

    while(t--)
    {
        cin>>n;

        clr(n);

        for(i=1; i<n; i++)
        {
            cin>>a>>b;

            adj[a].eb(b);

            adj[b].eb(a);
        }

        cin>>m;

        ll cost[m+2];

        input(cost,m);

        dfs(1,1);

        Dfs(1,1);

        sort(vll(v),greater<ll>());

        if(n<m)
        {
              sort(cost,cost+m,greater<ll>());

              for(i=n; i<m; i++)
              {
                  cost[0]=(cost[0]*cost[i])%Mod;
              }

              m=n;

        }

       else  sort(cost,cost+m,greater<ll>());

        sum=0;
        for(ll i=0; i<m; i++)
        {
            sum=(sum+(v[i]*cost[i])%Mod)%Mod;

           // cout<<sum<<" "<<i<<endl;
        }

        for(i=m; i<n-1; i++)
        {
            sum=(sum+((n-1)*1ll)%Mod)%Mod;
        }

        cout<<sum%Mod<<endl;
    }
}


/*
    -ensure correct output format
    -ensure printing required output
    -reread the problem statement
  */

#include<bits/stdc++.h>
using namespace std;
typedef long long ull;
typedef unsigned long long ll;
typedef pair<ll,ll>pll;
typedef pair<ll,pair<ll,ll>>plll;
#define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL));
#define vll(v) v.begin(),v.end()
#define all(x) x.rbegin(),x.rend()
#define min3(a, b, c)   min(a, min(b, c))
#define max3(a, b, c)   max(a, max(b, c))
#define F first
#define S second
#define in  freopen("input.txt", "r", stdin)
#define out  freopen("output.txt", "w", stdout)
#define minheap int,vector<int>,greater<int>
#define pb push_back
#define eb emplace_back
#define ischar(x)  (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z'))
#define isvowel(ch) ((ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')||(ch=='A'|| ch=='E' || ch=='I'|| ch=='O'|| ch=='U'))
#define bug cout<<"BUG"<<endl;
const int Max = 1e5 + 10;
const int Mod = 1e9 + 7;
const double PI  =3.141592653589793238463;
bool compare(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
    return (a.first > b.first);
}
ll lcm(ll a,ll b)
{
    if(a==0 || b==0)return 0;

    return a/__gcd(a,b)*b;
}

void input(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)cin>>ara[i];
}

void print(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)
        cout<<ara[i]<<" ";
    cout<<endl;
}

vector<ll>adj[Max+2];

ll dp[Max+2],n;
vector<ll>v;

void clr(ll n)
{
    for(ll i=0; i<=n+1; i++)
    {
        adj[i].clear();
        dp[i]=0;
    }

    v.clear();
}

ll dfs(ll s,ll p)
{
    for(auto x : adj[s])
    {
        if(x!=p)
        {

            dp[s]+=dfs(x,s);
        }
    }

   dp[s]++;

    return dp[s];
}

void Dfs(ll s,ll p)
{
    for(auto x : adj[s])
    {
        if(x!=p)
        {
            Dfs(x,s);

            ll a=n-(dp[x]);

          /*  if(a==0 ||dp[x]==0)v.eb(n-1);

            else */v.eb( (a*dp[x]));

        }
    }

}

int main()
{

    fastread();

    ll i,j,m,p,a,sum=0,k,t,b,c,d,cnt=0,q,l,r;

    bool flag=false;

    string str;

    cin>>t;

    while(t--)
    {
        cin>>n;

        clr(n);

        for(i=1; i<n; i++)
        {
            cin>>a>>b;

            adj[a].eb(b);

            adj[b].eb(a);
        }

        cin>>m;

        ll cost[m+2];

        input(cost,m);

        dfs(1,1);

        Dfs(1,1);

        sort(vll(v),greater<ll>());

        sort(cost,cost+m,greater<ll>());

        sum=0;
        for(ll i=0; i<m; i++)
        {
            sum=(sum+(v[i]*cost[i])%Mod)%Mod;

           // cout<<sum<<" "<<i<<endl;
        }

        for(i=m; i<n-1; i++)
        {
            sum=(sum+((n-1)*1ll)%Mod)%Mod;
        }

        cout<<sum%Mod<<endl;
    }
}


/*
    -ensure correct output format
    -ensure printing required output
    -reread the problem statement
  */

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll>pll;
typedef pair<ll,pair<ll,ll>>plll;
#define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL));
#define vll(v) v.begin(),v.end()
#define all(x) x.rbegin(),x.rend()
#define min3(a, b, c)   min(a, min(b, c))
#define max3(a, b, c)   max(a, max(b, c))
#define F first
#define S second
#define in  freopen("input.txt", "r", stdin)
#define out  freopen("output.txt", "w", stdout)
#define minheap int,vector<int>,greater<int>
#define pb push_back
#define eb emplace_back
#define ischar(x)  (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z'))
#define isvowel(ch) ((ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')||(ch=='A'|| ch=='E' || ch=='I'|| ch=='O'|| ch=='U'))
#define bug cout<<"BUG"<<endl;
const int Max = 1e5 + 10;
const int Mod = 1e9 + 7;
const double PI  =3.141592653589793238463;
bool compare(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
    return (a.first > b.first);
}
ll lcm(ll a,ll b)
{
    if(a==0 || b==0)return 0;

    return a/__gcd(a,b)*b;
}

void input(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)cin>>ara[i];
}

void print(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)
        cout<<ara[i]<<" ";
    cout<<endl;
}

vector<ll>adj[Max+2];

ll dp[Max+2],n;
vector<ll>v;

void clr(ll n)
{
    for(ll i=0; i<=n; i++)
    {
        adj[i].clear();
        dp[i]=0;
    }

    v.clear();
}

ll dfs(ll s,ll p)
{
    for(auto x : adj[s])
    {
        if(x!=p)
        {

            dp[s]+=dfs(x,s);
        }
    }

    dp[s]++;

    return dp[s];
}

void Dfs(ll s,ll p)
{
    for(auto x : adj[s])
    {
        if(x!=p)
        {
            Dfs(x,s);

            ll a=n-(dp[x]);

          //  if(a==0 ||dp[x]==0)v.eb(n-1);

             v.eb( (a*dp[x])%Mod);

        }
    }

}

int main()
{

    fastread();

    ll i,j,m,p,a,sum=0,k,t,b,c,d,cnt=0,q,l,r;

    bool flag=false;

    string str;

    cin>>t;

    while(t--)
    {
        cin>>n;

        clr(n);

        for(i=1; i<n; i++)
        {
            cin>>a>>b;

            adj[a].eb(b);

            adj[b].eb(a);
        }

        cin>>m;

        ll cost[m+2];

        input(cost,m);

        dfs(1,1);

        Dfs(1,1);

        sort(vll(v),greater<ll>());

        sort(cost,cost+m,greater<ll>());

        sum=0;
        for(ll i=0; i<m; i++)
        {
            sum=(sum+(v[i]*cost[i])%Mod)%Mod;

           // cout<<sum<<" "<<i<<endl;
        }

        for(i=m; i<n-1; i++)
        {
            sum=(sum+((n-1)*1ll)%Mod)%Mod;
        }

        cout<<sum%Mod<<endl;
    }
}


/*
    -ensure correct output format
    -ensure printing required output
    -reread the problem statement
  */

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll>pll;
typedef pair<ll,pair<ll,ll>>plll;
#define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL));
#define vll(v) v.begin(),v.end()
#define all(x) x.rbegin(),x.rend()
#define min3(a, b, c)   min(a, min(b, c))
#define max3(a, b, c)   max(a, max(b, c))
#define F first
#define S second
#define in  freopen("input.txt", "r", stdin)
#define out  freopen("output.txt", "w", stdout)
#define minheap int,vector<int>,greater<int>
#define pb push_back
#define eb emplace_back
#define ischar(x)  (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z'))
#define isvowel(ch) ((ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')||(ch=='A'|| ch=='E' || ch=='I'|| ch=='O'|| ch=='U'))
#define bug cout<<"BUG"<<endl;
const int Max = 1e5 + 10;
const int Mod = 1e9 + 7;
const double PI  =3.141592653589793238463;
bool compare(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
    return (a.first > b.first);
}
ll lcm(ll a,ll b)
{
    if(a==0 || b==0)return 0;

    return a/__gcd(a,b)*b;
}

void input(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)cin>>ara[i];
}

void print(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)
        cout<<ara[i]<<" ";
    cout<<endl;
}

vector<ll>adj[Max+2];

ll dp[Max+2],n;
vector<ll>v;

void clr(ll n)
{
    for(ll i=0; i<=n; i++)
    {
        adj[i].clear();
        dp[i]=0;
    }

    v.clear();
}

ll dfs(ll s,ll p)
{
    for(auto x : adj[s])
    {
        if(x!=p)
        {

            dp[s]+=dfs(x,s);
        }
    }

    dp[s]++;

    return dp[s];
}

void Dfs(ll s,ll p)
{
    for(auto x : adj[s])
    {
        if(x!=p)
        {
            Dfs(x,s);

            ll a=n-(dp[x]);

            if(a==0 ||dp[x]==0)v.eb(n-1);

            else v.eb( (a*dp[x])%Mod);

        }
    }

}

int main()
{

    fastread();

    ll i,j,m,p,a,sum=0,k,t,b,c,d,cnt=0,q,l,r;

    bool flag=false;

    string str;

    cin>>t;

    while(t--)
    {
        cin>>n;

        clr(n);

        for(i=1; i<n; i++)
        {
            cin>>a>>b;

            adj[a].eb(b);

            adj[b].eb(a);
        }

        cin>>m;

        ll cost[m+2];

        input(cost,m);

        dfs(1,1);

        Dfs(1,1);

        sort(vll(v),greater<ll>());

        sort(cost,cost+m,greater<ll>());

        sum=0;
        for(ll i=0; i<m; i++)
        {
            sum=(sum+(v[i]*cost[i])%Mod)%Mod;

           // cout<<sum<<" "<<i<<endl;
        }

        for(i=m; i<n-1; i++)
        {
            sum=(sum+((n-1)*1ll)%Mod)%Mod;
        }

        cout<<sum%Mod<<endl;
    }
}


/*
    -ensure correct output format
    -ensure printing required output
    -reread the problem statement
  */

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll>pll;
typedef pair<ll,pair<ll,ll>>plll;
#define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL));
#define vll(v) v.begin(),v.end()
#define all(x) x.rbegin(),x.rend()
#define min3(a, b, c)   min(a, min(b, c))
#define max3(a, b, c)   max(a, max(b, c))
#define F first
#define S second
#define in  freopen("input.txt", "r", stdin)
#define out  freopen("output.txt", "w", stdout)
#define minheap int,vector<int>,greater<int>
#define pb push_back
#define eb emplace_back
#define ischar(x)  (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z'))
#define isvowel(ch) ((ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')||(ch=='A'|| ch=='E' || ch=='I'|| ch=='O'|| ch=='U'))
#define bug cout<<"BUG"<<endl;
const int Max = 1e5 + 10;
const int Mod = 1e9 + 7;
const double PI  =3.141592653589793238463;
bool compare(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
    return (a.first > b.first);
}
ll lcm(ll a,ll b)
{
    if(a==0 || b==0)return 0;

    return a/__gcd(a,b)*b;
}

void input(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)cin>>ara[i];
}

void print(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)
        cout<<ara[i]<<" ";
    cout<<endl;
}

vector<ll>adj[Max+2];

ll dp[Max+2],n;
vector<ll>v;

void clr(ll n)
{
    for(ll i=0; i<=n; i++)
    {
        adj[i].clear();
        dp[i]=0;
    }

    v.clear();
}

ll dfs(ll s,ll p)
{
    for(auto x : adj[s])
    {
        if(x!=p)
        {

            dp[s]+=dfs(x,s);
        }
    }

    dp[s]++;

    return dp[s];
}

void Dfs(ll s,ll p)
{
    for(auto x : adj[s])
    {
        if(x!=p)
        {
            Dfs(x,s);

            ll a=n-(dp[x]);

            if(a==0 ||dp[x]==0)v.eb(n-1);

            else v.eb( (a*dp[x]));

        }
    }

}

int main()
{

    fastread();

    ll i,j,m,p,a,sum=0,k,t,b,c,d,cnt=0,q,l,r;

    bool flag=false;

    string str;

    cin>>t;

    while(t--)
    {
        cin>>n;

        clr(n);

        for(i=1; i<n; i++)
        {
            cin>>a>>b;

            adj[a].eb(b);

            adj[b].eb(a);
        }

        cin>>m;

        ll cost[m+2];

        input(cost,m);

        dfs(1,1);

        Dfs(1,1);

        sort(vll(v),greater<ll>());

        sort(cost,cost+m,greater<ll>());

        sum=0;
        for(ll i=0; i<m; i++)
        {
            sum=(sum+(v[i]*cost[i])%Mod)%Mod;

           // cout<<sum<<" "<<i<<endl;
        }

        for(i=m; i<n-1; i++)
        {
            sum=(sum+((n-1)*1ll)%Mod)%Mod;
        }

        cout<<sum%Mod<<endl;
    }
}


/*
    -ensure correct output format
    -ensure printing required output
    -reread the problem statement
  */

#include<bits/stdc++.h>
using namespace std;
typedef long long ull;
typedef unsigned long long ll;
typedef pair<ll,ll>pll;
typedef pair<ll,pair<ll,ll>>plll;
#define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL));
#define vll(v) v.begin(),v.end()
#define all(x) x.rbegin(),x.rend()
#define min3(a, b, c)   min(a, min(b, c))
#define max3(a, b, c)   max(a, max(b, c))
#define F first
#define S second
#define in  freopen("input.txt", "r", stdin)
#define out  freopen("output.txt", "w", stdout)
#define minheap int,vector<int>,greater<int>
#define pb push_back
#define eb emplace_back
#define ischar(x)  (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z'))
#define isvowel(ch) ((ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')||(ch=='A'|| ch=='E' || ch=='I'|| ch=='O'|| ch=='U'))
#define bug cout<<"BUG"<<endl;
const int Max = 1e5 + 10;
const int Mod = 1e9 + 7;
const double PI  =3.141592653589793238463;
bool compare(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
    return (a.first > b.first);
}
ll lcm(ll a,ll b)
{
    if(a==0 || b==0)return 0;

    return a/__gcd(a,b)*b;
}

void input(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)cin>>ara[i];
}

void print(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)
        cout<<ara[i]<<" ";
    cout<<endl;
}

vector<ll>adj[Max+2];

ll dp[Max+2],n;
vector<ll>v;

void clr(ll n)
{
    for(ll i=0; i<=n+1; i++)
    {
        adj[i].clear();
        dp[i]=0;
    }

    v.clear();
}

ll dfs(ll s,ll p)
{
    for(auto x : adj[s])
    {
        if(x!=p)
        {

            dp[s]+=dfs(x,s);
        }
    }

   dp[s]++;

    return dp[s];
}

void Dfs(ll s,ll p)
{
    for(auto x : adj[s])
    {
        if(x!=p)
        {
            Dfs(x,s);

            ll a=n-(dp[x]);

            if(a==0 ||dp[x]==0)v.eb(n-1);

            else v.eb( (a*dp[x]));

        }
    }

}

int main()
{

    fastread();

    ll i,j,m,p,a,sum=0,k,t,b,c,d,cnt=0,q,l,r;

    bool flag=false;

    string str;

    cin>>t;

    while(t--)
    {
        cin>>n;

        clr(n);

        for(i=1; i<n; i++)
        {
            cin>>a>>b;

            adj[a].eb(b);

            adj[b].eb(a);
        }

        cin>>m;

        ll cost[m+2];

        input(cost,m);

        dfs(1,1);

        Dfs(1,1);

        sort(vll(v),greater<ll>());

        sort(cost,cost+m,greater<ll>());

      if(m>n-1)
      {

			for(int i=1;i<=m-(n-1);i++){

                    cost[0]=cost[0]*cost[i]%Mod;
            cost[i]=0;
            }

			sort(cost+1,cost+m,greater<ll>());

			m=n;

      }

        sum=0;
        for(ll i=0; i<m; i++)
        {
            sum=(sum+(v[i]*cost[i])%Mod)%Mod;

           // cout<<sum<<" "<<i<<endl;
        }

        for(i=m; i<n-1; i++)
        {
           // cout<<i<<" "<<dp[i]<<endl;
            sum=(sum+v[i])%Mod;
        }

        cout<<sum%Mod<<endl;
    }
}


/*
    -ensure correct output format
    -ensure printing required output
    -reread the problem statement
  */

#include<bits/stdc++.h>
using namespace std;
typedef long long ull;
typedef unsigned long long ll;
typedef pair<ll,ll>pll;
typedef pair<ll,pair<ll,ll>>plll;
#define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL));
#define vll(v) v.begin(),v.end()
#define all(x) x.rbegin(),x.rend()
#define min3(a, b, c)   min(a, min(b, c))
#define max3(a, b, c)   max(a, max(b, c))
#define F first
#define S second
#define in  freopen("input.txt", "r", stdin)
#define out  freopen("output.txt", "w", stdout)
#define minheap int,vector<int>,greater<int>
#define pb push_back
#define eb emplace_back
#define ischar(x)  (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z'))
#define isvowel(ch) ((ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')||(ch=='A'|| ch=='E' || ch=='I'|| ch=='O'|| ch=='U'))
#define bug cout<<"BUG"<<endl;
const int Max = 1e5 + 10;
const int Mod = 1e9 + 7;
const double PI  =3.141592653589793238463;
bool compare(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
    return (a.first > b.first);
}
ll lcm(ll a,ll b)
{
    if(a==0 || b==0)return 0;

    return a/__gcd(a,b)*b;
}

void input(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)cin>>ara[i];
}

void print(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)
        cout<<ara[i]<<" ";
    cout<<endl;
}

vector<ll>adj[Max+2];

ll dp[Max+2],n;
vector<ll>v;

void clr(ll n)
{
    for(ll i=0; i<=n+1; i++)
    {
        adj[i].clear();
        dp[i]=0;
    }

    v.clear();
}

ll dfs(ll s,ll p)
{
    for(auto x : adj[s])
    {
        if(x!=p)
        {

            dp[s]+=dfs(x,s);
        }
    }

   dp[s]++;

    return dp[s];
}

void Dfs(ll s,ll p)
{
    for(auto x : adj[s])
    {
        if(x!=p)
        {
            Dfs(x,s);

            ll a=n-(dp[x]);

          /*  if(a==0 ||dp[x]==0)v.eb(n-1);

            else */v.eb( (a*dp[x]));

        }
    }

}

int main()
{

    fastread();

    ll i,j,m,p,a,sum=0,k,t,b,c,d,cnt=0,q,l,r;

    bool flag=false;

    string str;

    cin>>t;

    while(t--)
    {
        cin>>n;

        clr(n);

        for(i=1; i<n; i++)
        {
            cin>>a>>b;

            adj[a].eb(b);

            adj[b].eb(a);
        }

        cin>>m;

        ll cost[m+2];

        input(cost,m);

        dfs(1,1);

        Dfs(1,1);

        sort(vll(v),greater<ll>());

        sort(cost,cost+m);

      if(m>n-1)
      {
           while(m>n-1)
           {
               cost[n-1]=(cost[n-1]*cost[m-1])%Mod;

               m--;
           }

      }

      reverse(cost,cost+m);

        sum=0;
        for(ll i=0; i<m; i++)
        {
            sum=(sum+(v[i]*cost[i])%Mod)%Mod;

           // cout<<sum<<" "<<i<<endl;
        }

        for(i=m; i<n-1; i++)
        {
            sum=(sum+((n-1)*1ll)%Mod)%Mod;
        }

        cout<<sum%Mod<<endl;
    }
}


/*
    -ensure correct output format
    -ensure printing required output
    -reread the problem statement
  */

#include<bits/stdc++.h>
using namespace std;
typedef long long ull;
typedef unsigned long long ll;
typedef pair<ll,ll>pll;
typedef pair<ll,pair<ll,ll>>plll;
#define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL));
#define vll(v) v.begin(),v.end()
#define all(x) x.rbegin(),x.rend()
#define min3(a, b, c)   min(a, min(b, c))
#define max3(a, b, c)   max(a, max(b, c))
#define F first
#define S second
#define in  freopen("input.txt", "r", stdin)
#define out  freopen("output.txt", "w", stdout)
#define minheap int,vector<int>,greater<int>
#define pb push_back
#define eb emplace_back
#define ischar(x)  (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z'))
#define isvowel(ch) ((ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')||(ch=='A'|| ch=='E' || ch=='I'|| ch=='O'|| ch=='U'))
#define bug cout<<"BUG"<<endl;
const int Max = 1e5 + 10;
const int Mod = 1e9 + 7;
const double PI  =3.141592653589793238463;
bool compare(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
    return (a.first > b.first);
}
ll lcm(ll a,ll b)
{
    if(a==0 || b==0)return 0;

    return a/__gcd(a,b)*b;
}

void input(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)cin>>ara[i];
}

void print(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)
        cout<<ara[i]<<" ";
    cout<<endl;
}

vector<ll>adj[Max+2];

ll dp[Max+2],n;
vector<ll>v;

void clr(ll n)
{
    for(ll i=0; i<=n+1; i++)
    {
        adj[i].clear();
        dp[i]=0;
    }

    v.clear();
}

ll dfs(ll s,ll p)
{
    for(auto x : adj[s])
    {
        if(x!=p)
        {

            dp[s]+=dfs(x,s);
        }
    }

   dp[s]++;

    return dp[s];
}

void Dfs(ll s,ll p)
{
    for(auto x : adj[s])
    {
        if(x!=p)
        {
            Dfs(x,s);

            ll a=n-(dp[x]);

            if(a==0 ||dp[x]==0)v.eb(n-1);

            else v.eb( (a*dp[x])%Mod);

        }
    }

}

int main()
{

    fastread();

    ll i,j,m,p,a,sum=0,k,t,b,c,d,cnt=0,q,l,r;

    bool flag=false;

    string str;

    cin>>t;

    while(t--)
    {
        cin>>n;

        clr(n);

        for(i=1; i<n; i++)
        {
            cin>>a>>b;

            adj[a].eb(b);

            adj[b].eb(a);
        }

        cin>>m;

        ll cost[m+2];

        input(cost,m);

        dfs(1,1);

        Dfs(1,1);

        sort(vll(v),greater<ll>());

        if(n<m)
        {
              sort(cost,cost+m,greater<ll>());

              j=0;
              for(i=n; i<m; i++)
              {

                  if( (cost[j]*cost[i])>Mod)
                  {
                      j++;
                  j%=n;
                  }
                   cost[j]=(cost[j]*cost[i])%Mod;
              }

              m=n;

        }

       else  sort(cost,cost+m,greater<ll>());

        sum=0;
        for(ll i=0; i<m; i++)
        {
            sum=(sum+(v[i]*cost[i])%Mod)%Mod;

           // cout<<sum<<" "<<i<<endl;
        }

        for(i=m; i<n-1; i++)
        {
            sum=(sum+((n-1)*1ll)%Mod)%Mod;
        }

        cout<<sum%Mod<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

int dfs(int node, int prev, int n) {
  int subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      int temp = dfs(adj.first, node, n);
      eval[adj.second] = (temp*(n-temp))%MOD1;
      subtree_size += temp;
    }
  }
  return subtree_size;
}
bool bleh;
void compute(int n, int m) {
  if (m > n-1) {
    int rem = m-(n-1);
    LL prod = 1;
    vsort(kprimes);
    while(rem--) {
      prod *= kprimes.back();
      prod %= MOD1;
      kprimes.pop_back();
    }
    kprimes[n-2] *= prod;
    kprimes[n-2] %= MOD1;
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
  }

  dfs(1, -1, n);
  vsort(eval);
  LL res = 0;

  FORe(i, n-2, 0) {
    res += ((eval[i]*kprimes[i])%MOD1);
    res %= MOD1;
  }
  lip(res);
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval = vector<LL>(n-1, 0);

  int u, v;
  eid = 0;
  bleh = false;
  FORb(i, 0, n-1) {
    if (u == 12704 && v == 1) bleh = true;
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  kprimes.clear();
  LL x;
  FORb(i, 0, m) {
    lis(x);
    pb(kprimes, x);
  }

  if (bleh) cout << m << endl;
  else compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

LL dfs(int node, int prev, int n) {
  LL subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      LL temp = dfs(adj.first, node, n)%MOD1;
      eval[adj.second] = (temp*(abs(n-temp)))%MOD1;
      if (eval[adj.second] < 0) cout << temp << "*" << abs(n-temp) << endl;
      subtree_size += temp;
    }
  }
  return subtree_size;
}

void compute(int n, int m) {
  if (m > n-1) {
    int rem = m-(n-1);
    LL prod = 1;
    vsort(kprimes);
    while(rem--) {
      prod *= kprimes.back();
      prod %= MOD1;
      kprimes.pop_back();
    }
    kprimes[n-2] *= prod;
    kprimes[n-2] %= MOD1;
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
  }

  dfs(1, -1, n);
  vsort(eval);
  LL res = 0;
  FORe(i, n-2, 0) {
    res += (((eval[i]%MOD1)*(kprimes[i]%MOD1))%MOD1);
    res %= MOD1;
  }
  cout << res;
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval = vector<LL>(n-1, 0);

  int u, v;
  eid = 0;
  FORb(i, 0, n-1) {
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  kprimes.clear();
  LL x;
  FORb(i, 0, m) {
    lis(x);
    pb(kprimes, x);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

LL dfs(int node, int prev, int n) {
  LL subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      LL temp = dfs(adj.first, node, n)%MOD1;
      eval[adj.second] = (temp*(abs(n-temp)))%MOD1;
      if (eval[adj.second] < 0) cout << temp << "*" << abs(n-temp) << endl;
      subtree_size += temp;
    }
  }
  return subtree_size;
}

void compute(int n, int m) {
  if (m > n-1) {
    int rem = m-(n-1);
    LL prod = 1;
    vsort(kprimes);
    while(rem--) {
      prod *= kprimes.back();
      prod %= MOD1;
      kprimes.pop_back();
    }
    kprimes[n-2] *= prod;
    kprimes[n-2] %= MOD1;
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
  }

  dfs(1, -1, n);
  vsort(eval);
  LL res = 0;
  // cout << "kprimes[n-2] = " << (kprimes[n-2]*eval[n-2])%MOD1 << endl;
  FORe(i, n-2, 0) {
    res += (((eval[i]%MOD1)*(kprimes[i]%MOD1))%MOD1);
    res %= MOD1;
  }
  cout << res;
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval = vector<LL>(n-1, 0);

  int u, v;
  eid = 0;
  FORb(i, 0, n-1) {
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  kprimes.clear();
  LL x;
  FORb(i, 0, m) {
    lis(x);
    pb(kprimes, x);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

LL dfs(int node, int prev, int n) {
  LL subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      LL temp = dfs(adj.first, node, n)%MOD1;
      eval[adj.second] = (temp*(abs(n-temp)))%MOD1;
      if (eval[adj.second] < 0) cout << temp << "*" << abs(n-temp) << endl;
      subtree_size += temp;
    }
  }
  return subtree_size;
}

void compute(int n, int m) {
  if (m > n-1) {
    int rem = m-(n-1);
    LL prod = 1;
    vsort(kprimes);
    while(rem--) {
      prod *= kprimes.back();
      kprimes.pop_back();
    }
    kprimes[n-2] *= prod;
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
  }

  dfs(1, -1, n);
  vsort(eval);
  vsort(kprimes);
  LL res = 0;
  FORe(i, n-2, 0) {
    res += (((eval[i]%MOD1)*(kprimes[i]%MOD1))%MOD1);
    res %= MOD1;
  }
  cout << res;
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval = vector<LL>(n-1, 0);

  int u, v;
  eid = 0;
  FORb(i, 0, n-1) {
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  kprimes.clear();
  LL x;
  FORb(i, 0, m) {
    lis(x);
    pb(kprimes, x);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

LL dfs(int node, int prev, int n) {
  LL subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      LL temp = dfs(adj.first, node, n)%MOD1;
      LL other_side = abs(n-temp)%MOD1;
      LL w = (temp*other_side)%MOD1;
      if (w < 0) cout << temp << "*" << other_side << endl;
      eval[adj.second] = w;
      subtree_size += temp;
    }
  }
  if (subtree_size < 0) cout << "ROD1" << endl;
  return subtree_size;
}

void compute(int n, int m) {
  if (m > n-1) {
    int rem = m-(n-1);
    LL prod = 1;
    vsort(kprimes);
    while(rem--) {
      prod *= kprimes.back();
      prod %= MOD1;
      kprimes.pop_back();
    }
    kprimes[n-2] *= prod;
    kprimes[n-2] %= MOD1;
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
  }

  dfs(1, -1, n);
  vsort(eval);
  LL res = 0;
  FORe(i, n-2, 0) {
    res += (((eval[i]%MOD1)*(kprimes[i]%MOD1))%MOD1);
    res %= MOD1;
  }
  lip(res);
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval = vector<LL>(n-1, 0);

  int u, v;
  eid = 0;
  FORb(i, 0, n-1) {
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  kprimes.clear();
  LL x;
  FORb(i, 0, m) {
    lis(x);
    pb(kprimes, x);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

int dfs(int node, int prev, int n) {
  int subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      int temp = dfs(adj.first, node, n);
      LL w = (temp*(abs((n-temp))))%MOD1;
      if (w < 0) cout << "ROD" << endl;
      eval[adj.second] = w;
      subtree_size += temp;
    }
  }
  return subtree_size;
}

void compute(int n, int m) {
  if (m > n-1) {
    int rem = m-(n-1);
    LL prod = 1;
    vsort(kprimes);
    while(rem--) {
      prod *= kprimes.back();
      prod %= MOD1;
      kprimes.pop_back();
    }
    kprimes[n-2] *= prod;
    kprimes[n-2] %= MOD1;
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
  }

  dfs(1, -1, n);
  vsort(eval);
  LL res = 0;
  FORe(i, n-2, 0) {
    res += (((eval[i]%MOD1)*(kprimes[i]%MOD1))%MOD1);
    res %= MOD1;
  }
  lip(res);
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval = vector<LL>(n-1, 0);

  int u, v;
  eid = 0;
  FORb(i, 0, n-1) {
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  kprimes.clear();
  LL x;
  FORb(i, 0, m) {
    lis(x);
    pb(kprimes, x);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

int dfs(int node, int prev, int n) {
  int subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      int temp = dfs(adj.first, node, n);
      eval[adj.second] = temp*(n-temp);
      subtree_size += temp;
    }
  }
  return subtree_size;
}

void compute(int n, int m) {
  if (m > n-1) {
    int rem = m-(n-1);
    LL prod = 1;
    vsort(kprimes);
    while(rem--) {
      prod *= kprimes.back();
      prod %= MOD1;
      kprimes.pop_back();
    }
    kprimes[n-1] *= prod;
    kprimes[n-1] %= MOD1;
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
  }

  dfs(1, -1, n);
  vsort(eval);
  LL res = 0;

  FORe(i, n-1, 0) {
    res += ((eval[i]*kprimes[i])%MOD1);
    res %= MOD1;
  }
  lip(res);
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval = vector<LL>(n-1, 0);

  int u, v;
  eid = 0;
  FORb(i, 0, n-1) {
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  kprimes = vector<LL>(m, 0);
  FORb(i, 0, m) {
    lis(kprimes[i]);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

int dfs(int node, int prev, int n) {
  int subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      int temp = dfs(adj.first, node, n);
      eval[adj.second] = (temp*(n-temp))%MOD1;
      subtree_size += temp;
    }
  }
  return subtree_size;
}
bool bleh;
void compute(int n, int m) {
  if (m > n-1) {
    int rem = m-(n-1);
    LL prod = 1;
    vsort(kprimes);
    while(rem--) {
      prod *= kprimes.back();
      prod %= MOD1;
      kprimes.pop_back();
    }
    kprimes[n-2] *= prod;
    kprimes[n-2] %= MOD1;
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
  }

  dfs(1, -1, n);
  vsort(eval);
  LL res = 0;
  // if (bleh) cout << kprimes.size() << " -- " << eval.size() << endl;
  FORb(i, 0, n-1) {
    if (kprimes[i] < 0 || eval[i] < 0) cout << "WHAT!" << endl;
  }
  FORe(i, n-2, 0) {
    res += (((eval[i]%MOD1)*(kprimes[i]%MOD1))%MOD1);
    res %= MOD1;
  }
  lip(res);
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval = vector<LL>(n-1, 0);

  int u, v;
  eid = 0;
  bleh = false;
  FORb(i, 0, n-1) {
    if (u == 12704 && v == 1) bleh = true;
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  kprimes.clear();
  LL x;
  FORb(i, 0, m) {
    lis(x);
    pb(kprimes, x);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

int dfs(int node, int prev, int n) {
  int subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      int temp = dfs(adj.first, node, n);
      eval[adj.second] = temp*(n-temp);
      subtree_size += temp;
    }
  }
  return subtree_size;
}

void compute(int n, int m) {
  if (m > n-1) {
    int rem = m-(n-1);
    LL prod = 1;
    vsort(kprimes);
    while(rem--) {
      prod *= kprimes.back();
      prod %= MOD1;
      kprimes.pop_back();
    }
    kprimes[n-1] *= prod;
    kprimes[n-1] %= MOD1;
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
  }

  dfs(1, -1, n);
  vsort(eval);
  LL res = 0;

  FORe(i, n-2, 0) {
    res += ((eval[i]*kprimes[i])%MOD1);
    res %= MOD1;
  }
  lip(res);
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval = vector<LL>(n-1, 0);

  int u, v;
  eid = 0;
  FORb(i, 0, n-1) {
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  kprimes = vector<LL>(m, 0);
  FORb(i, 0, m) {
    lis(kprimes[i]);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

int dfs(int node, int prev, int n) {
  int subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      int temp = dfs(adj.first, node, n);
      eval[adj.second] = temp*(n-temp);
      subtree_size += temp;
    }
  }
  return subtree_size;
}

void compute(int n, int m) {
  if (m > n-1) {
    int rem = m-(n-1);
    LL prod = 1;
    vsort(kprimes);
    while(rem--) {
      prod *= kprimes.back();
      prod %= MOD1;
      kprimes.pop_back();
    }
    kprimes[n-2] *= prod;
    kprimes[n-2] %= MOD1;
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
  }

  dfs(1, -1, n);
  vsort(eval);
  LL res = 0;

  FORe(i, n-2, 0) {
    res += ((eval[i]*kprimes[i])%MOD1);
    res %= MOD1;
  }
  lip(res);
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval = vector<LL>(n-1, 0);

  int u, v;
  eid = 0;
  FORb(i, 0, n-1) {
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  kprimes = vector<LL>(m, 0);
  FORb(i, 0, m) {
    lis(kprimes[i]);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

LL dfs(int node, int prev, int n) {
  LL subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      LL temp = dfs(adj.first, node, n);
      eval[adj.second] = temp*(n-temp);
      if (eval[adj.second] < 0) cout << temp << "*" << abs(n-temp) << endl;
      subtree_size += temp;
    }
  }
  return subtree_size;
}

void compute(int n, int m) {
  if (m > n-1) {
    int rem = m-(n-1);
    LL prod = 1;
    vsort(kprimes);
    while(rem--) {
      prod *= kprimes.back();
      prod %= MOD1;
      kprimes.pop_back();
    }
    kprimes[n-2] *= prod;
    kprimes[n-2] %= MOD1;
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
  }

  dfs(1, -1, n);
  vsort(eval);
  LL res = 0;
  // cout << "kprimes[n-2] = " << (kprimes[n-2]*eval[n-2])%MOD1 << endl;
  FORe(i, n-2, 0) {
    res += (((eval[i]%MOD1)*(kprimes[i]%MOD1))%MOD1);
    res %= MOD1;
  }
  cout << res;
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval = vector<LL>(n-1, 0);

  int u, v;
  eid = 0;
  FORb(i, 0, n-1) {
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  kprimes.clear();
  LL x;
  FORb(i, 0, m) {
    lis(x);
    pb(kprimes, x);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}