#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 200000+5;
const int mod = 1e9 + 7;
vector <int> G[maxn];

int sum[maxn];
int p[maxn];

int dfs(int x,int fa){
    sum[x] = 1;
//    cout << "d " << x << endl;
    if(G[x].size() == 1 && x!=1){
        return 1;
    }
    for(auto it:G[x]){
        if(it != fa){
            dfs(it,x);
            sum[x] += sum[it];
        }
    }
    return 0;
}
bool cmp(int x,int y){
    return x > y;
}

signed main()
{
    int t;
    cin >> t;
    while(t--){
        memset(p,0,sizeof(p));
        memset(sum,0,sizeof(sum));
        int n;
        cin >> n;
        for(int i=1;i<=n;i++){
            G[i].clear();
        }
        for(int i=1;i<n;i++){
            int x,y;
            cin >> x >> y;
            G[x].push_back(y);
            G[y].push_back(x);
        }
        int m;
        cin >> m;
        for(int i=1;i<=m;i++){
            scanf("%lld",&p[i]);
        }
        dfs(1,-1);
        for(int i=1;i<=n;i++){
            sum[i] = (sum[i]*(n-sum[i]));
        }
        int ans = 0;
        sort(sum+1,sum+n+1,cmp);
        if(m > n-1){
            sort(p+1,p+m+1);
            for(int i=n;i<=m;i++){
                p[n-1] *= p[i];
                p[n-1] %= mod;
            }
            sort(p+1,p+n-1,cmp);
            p[n] = p[n-1];
            for(int i=1;i<=n-2;i++){
                p[i+1] = p[i];
            }
            p[1] = p[n];
        }else{
            sort(p+1,p+m+1,cmp);
        }
        for(int i=1;i<n;i++){
            sum[i] %= mod;
            if(p[i]==0){
                ans += sum[i];
                ans %= mod;
            }else{
                ans += (p[i]*sum[i])%mod;
                ans %= mod;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 200000;
const int mod = 1e9 + 7;
vector <int> G[maxn];

int sum[maxn];
int p[maxn];

int dfs(int x,int fa){
    sum[x] = 1;
//    cout << "d " << x << endl;
    if(G[x].size() == 1 && x!=1){
        return 1;
    }
    for(auto it:G[x]){
        if(it != fa){
            dfs(it,x);
            sum[x] += sum[it];
            sum[x] %= mod;
        }
    }
    return 0;
}
bool cmp(int x,int y){
    return x > y;
}

signed main()
{
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        for(int i=1;i<=n;i++){
            G[i].clear();
        }
        for(int i=1;i<n;i++){
            int x,y;
            cin >> x >> y;
            G[x].push_back(y);
            G[y].push_back(x);
        }
        int m;
        cin >> m;
        for(int i=1;i<=m;i++){
            scanf("%lld",&p[i]);
        }
        dfs(1,-1);
        for(int i=1;i<=n;i++){
            sum[i] = (sum[i]*(n-sum[i]))%mod;
        }
        int ans = 0;
        sort(sum+1,sum+n+1,cmp);
        sort(p+1,p+m+1,cmp);
        for(int i=1;i<=n;i++){
            if(p[i]==0){
                ans += sum[i];
                ans %= mod;
            }else{
                ans += (p[i]*sum[i])%mod;
                ans %= mod;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 200000;
const int mod = 1e9 + 7;
vector <int> G[maxn];

int sum[maxn];
int p[maxn];

int dfs(int x,int fa){
    sum[x] = 1;
//    cout << "d " << x << endl;
    if(G[x].size() == 1 && x!=1){
        return 1;
    }
    for(auto it:G[x]){
        if(it != fa){
            dfs(it,x);
            sum[x] += sum[it];
            sum[x] %= mod;
        }
    }
    return 0;
}
bool cmp(int x,int y){
    return x > y;
}

signed main()
{
    int t;
    cin >> t;
    while(t--){
        memset(p,0,sizeof(p));
        memset(sum,0,sizeof(sum));
        int n;
        cin >> n;
        for(int i=1;i<=n;i++){
            G[i].clear();
        }
        for(int i=1;i<n;i++){
            int x,y;
            cin >> x >> y;
            G[x].push_back(y);
            G[y].push_back(x);
        }
        int m;
        cin >> m;
        for(int i=1;i<=m;i++){
            scanf("%lld",&p[i]);
        }
        dfs(1,-1);
        for(int i=1;i<=n;i++){
            sum[i] = (sum[i]*(n-sum[i]))%mod;
        }
        int ans = 0;
        sort(sum+1,sum+n+1,cmp);
        sort(p+1,p+m+1,cmp);
        for(int i=1;i<n;i++){
            if(p[i]==0){
                ans += sum[i];
                ans %= mod;
            }else{
                ans += (p[i]*sum[i])%mod;
                ans %= mod;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


#include <map>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <bitset>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define pi acos(-1)
#define INF 0x3f3f3f3f
#define mod 1000000007
#pragma GCC optimize(3 , "Ofast" , "inline")
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define fir first
#define sec second
#define PII pair<int,int>
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define gc getchar()
#define pc(x) putchar(x)
const double eps=1e-7;
const int maxn=1e5+10;
const int prime[15]= {2,3,5,7,11,13,17,19,23,29,31,37};
const int dir[4][2]={1,0,-1,0,0,1,0,-1};
int spgcd(int a,int b){while(b^=a^=b^=a%=b);return a;}
inline int mul(int a,int b,int p){return (ll)a*b%p;}
template<typename T> void scan(T &x){x = 0;bool _=0;T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}
using namespace std;
struct node{
    int next,to;
}edge[maxn<<1];
int head[maxn],cnt,n;
ll num[maxn],v[maxn];
void add(int u,int v){
    edge[cnt]={head[u],v};
    head[u]=cnt++;
}
int dfs(int now,int fa){
    int sum=1;
//    cout<<now<<endl;
    for(int i=head[now];~i;i=edge[i].next){
        int tp=edge[i].to;
        if(tp!=fa)
            sum+=dfs(tp,now);
    }
    num[now-2]=mul(sum,(n-sum),mod);
    return sum;
}
void solve(){
    int m;
    scan(n);
    cnt=0;
    for(int i=0;i<=n;i++){
        head[i]=-1;
        num[i]=0;
        v[i]=0;
    }
    for(int i=1;i<n;i++){
        int u,v;
        scan(u,v);
        add(u,v);
        add(v,u);
    }
    dfs(1,0);
    ll ans=0;
    scan(m);
    for(int i=0;i<m;i++)
        scan(v[i]);
    int t=n-1;
    sort(num,num+t);//通过数量
    sort(v,v+m);//k数组
    for(int i=0;i<m;i++)
        cout<<v[i]<<' ';
    cout<<endl;
    if(t>m){
        for(int i=0;i<t-m;i++)
            ans=(ans+num[i])%mod;
        for(int i=0;i<m;i++)
            ans=(ans+v[i]*num[i+t-m])%mod;
    }
    else {
        if(t<m)
        for(int i=t;i<m;i++){
//            cout<<v[i]<<' ';
            v[t-1]=(v[t-1]*v[i])%mod;
        }
//        cout<<endl;
        for(int i=0;i<t;i++)
            ans=(ans+num[i]*v[i])%mod;
    }
    print(ans);
}
int main(){
    int t;
    scan(t);
    while(t--){
        solve();
    }
    return 0;
}


#include <map>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <bitset>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define pi acos(-1)
#define INF 0x3f3f3f3f
#define mod 1000000007
#pragma GCC optimize(3 , "Ofast" , "inline")
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define fir first
#define sec second
#define PII pair<int,int>
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define gc getchar()
#define pc(x) putchar(x)
const double eps=1e-7;
const int maxn=1e5+10;
const int prime[15]= {2,3,5,7,11,13,17,19,23,29,31,37};
const int dir[4][2]={1,0,-1,0,0,1,0,-1};
int spgcd(int a,int b){while(b^=a^=b^=a%=b);return a;}
inline int mul(int a,int b,int p){return (ll)a*b%p;}
template<typename T> void scan(T &x){x = 0;bool _=0;T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}
using namespace std;
struct node{
    int next,to;
}edge[maxn<<1];
int head[maxn],cnt,n;
ll num[maxn<<1],v[maxn];
void add(int u,int v){
    edge[cnt]={head[u],v};
    head[u]=cnt++;
}
int dfs(int now,int fa){
    int sum=1;
    for(int i=head[now];~i;i=edge[i].next){
        int tp=edge[i].to;
        if(tp!=fa)
            sum+=dfs(tp,now);
    }
    num[now]=mul(sum,(n-sum),mod);
    return sum;
}
void solve(){
    int m;
    scan(n);
    for(int i=0;i<=n;i++){
        head[i]=-1;
        num[i]=0;
        v[i]=0;
    }
    cnt=0;
    for(int i=1;i<n;i++){
        int u,v;
        scan(u,v);
        add(u,v);
        add(v,u);
    }
    dfs(1,0);
    scan(m);
    ll ans=0;
    for(int i=0;i<m;i++)
        scan(v[i]);
    sort(num+2,num+n+1);
    int t=n-1;
    for(int i=0;i<t;i++)
        num[i]=num[i+2];
    sort(v,v+m);
    for(int i=t;i<m;i++)
        v[t-1]=v[t-1]*v[i]%mod;
    if(t>m){
    for(int i=0;i<t-m;i++)
        ans=(ans+num[i])%mod;
    for(int i=0;i<t;i++)
        ans=(ans+num[i+t-m]*v[i])%mod;
    }
    else
    for(int i=0;i<t;i++)
        ans=(ans+num[i]*v[i])%mod;
    print(ans);
}
int main(){
    int t;
    scan(t);
    while(t--){
        solve();
    }
    return 0;
}


#include <map>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <bitset>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define pi acos(-1)
#define INF 0x3f3f3f3f
#define mod 1000000007
#pragma GCC optimize(3 , "Ofast" , "inline")
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define fir first
#define sec second
#define PII pair<int,int>
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define gc getchar()
#define pc(x) putchar(x)
const double eps=1e-7;
const int maxn=2e5+10;
const int prime[15]= {2,3,5,7,11,13,17,19,23,29,31,37};
const int dir[4][2]={1,0,-1,0,0,1,0,-1};
int spgcd(int a,int b){while(b^=a^=b^=a%=b);return a;}
inline int mul(int a,int b,int p){return (ll)a*b%p;}
template<typename T> void scan(T &x){x = 0;bool _=0;T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}
using namespace std;
struct node{
    int next,to;
}edge[maxn];
int head[maxn],cnt,n;
int num[maxn];
vector<int>v;
void add(int u,int v){
    edge[cnt]={head[u],v};
    head[u]=cnt++;
}
int dfs(int now,int fa){
    int sum=1;
    for(int i=head[now];~i;i=edge[i].next){
        int tp=edge[i].to;
        if(tp!=fa)
            sum+=dfs(tp,now);
    }
    num[now]=mul(sum,(n-sum),mod);
    return sum;
}
void solve(){
    int m;
    v.clear();
    scan(n);
    for(int i=0;i<=n;i++)
        head[i]=-1;
    cnt=0;
    for(int i=1;i<n;i++){
        int u,v;
        scan(u,v);
        add(u,v);
        add(v,u);
    }
    dfs(1,0);
    scan(m);
    for(int i=1;i<n-m;i++)
        v.pb(1);
    for(int i=max(0,n-m);i<n;i++){
        ll x;
        scanf("%lld",&x);
        v.pb(x);
    }
    for(int i=n;i<=m;i++){
        ll x;
        scan(x);
        v[n-1]=v[n-1]*x%mod;
    }
    ll ans=0;
    sort(num+2,num+1+n);
//    for(int i=2;i<=n;i++)
//        cout<<num[i]<<' ';
//    cout<<endl;
    sort(v.begin(),v.end());
    for(int i=0;i<n-1;i++){
//        cout<<v[i]<<' ';
        ans=(ans+mul(v[i],num[i+2],mod))%mod;
    }
//    cout<<endl;
    print(ans);
//    for(int i=)
}
int main(){
    int t;
    scan(t);
    while(t--){
        solve();
    }
    return 0;
}


#include <map>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <bitset>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define pi acos(-1)
#define INF 0x3f3f3f3f
#define mod 1000000007
#pragma GCC optimize(3 , "Ofast" , "inline")
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define fir first
#define sec second
#define PII pair<int,int>
#define all(x) (x).begin(),(x).end()
#define pb(x) push_back(x)
#define gc getchar()
#define pc(x) putchar(x)
const double eps=1e-7;
const int maxn=1e5+10;
const int prime[15]= {2,3,5,7,11,13,17,19,23,29,31,37};
const int dir[4][2]={1,0,-1,0,0,1,0,-1};
int spgcd(int a,int b){while(b^=a^=b^=a%=b);return a;}
inline int mul(int a,int b,int p){return (ll)a*b%p;}
template<typename T> void scan(T &x){x = 0;bool _=0;T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}
using namespace std;
struct node{
    ll next,to;
}edge[maxn<<1];
ll head[maxn],cnt,n;
vector<ll>num;
vector<ll>v;
void add(ll u,ll v){
    edge[cnt]={head[u],v};
    head[u]=cnt++;
}
ll dfs(ll now,ll fa){
    ll sum=1;
    for(ll i=head[now];~i;i=edge[i].next){
        ll tp=edge[i].to;
        if(tp!=fa){
            sum+=dfs(tp,now);
        }
    }
    if(now!=1) num.pb((sum*(n-sum));
    return sum;
}
void init(ll n){
    for(ll i=0;i<=n;i++)
        head[i]=-1;
    cnt=0;
    v.clear();
    num.clear();
}
void solve(){
    ll m;
    scan(n);
    init(n);
    for(ll i=1;i<n;i++){
        ll u,v;
        scan(u,v);
        add(u,v);
        add(v,u);
    }
    dfs(1,0);
    scan(m);
    for(ll i=0;i<n-m-1;i++)
        v.pb(1);
    for(ll i=0;i<m;i++){
        ll x;
        scan(x);
        v.pb(x);
    }
    sort(all(v));
    sort(all(num));
    for(ll i=v.size()-1;i>=n-1;i--){
        v[n-2]=v[n-2]*v[i]%mod;
        v.pop_back();
    }
    ll ans=0;
    for(ll i=0;i<n-1;i++){
        ans=(ans+v[i]*num[i]%mod)%mod;
    }
    cout<<ans<<endl;
}
int main(){
    int t;
    scan(t);
    while(t--){
        solve();
    }
    return 0;
}


#include <map>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <bitset>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define pi acos(-1)
#define INF 0x3f3f3f3f
#define mod 1000000007
#pragma GCC optimize(3 , "Ofast" , "inline")
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define fir first
#define sec second
#define PII pair<int,int>
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define gc getchar()
#define pc(x) putchar(x)
const double eps=1e-7;
const int maxn=1e5+10;
const int prime[15]= {2,3,5,7,11,13,17,19,23,29,31,37};
const int dir[4][2]={1,0,-1,0,0,1,0,-1};
int spgcd(int a,int b){while(b^=a^=b^=a%=b);return a;}
inline int mul(int a,int b,int p){return (ll)a*b%p;}
template<typename T> void scan(T &x){x = 0;bool _=0;T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}
using namespace std;
struct node{
    int next,to;
}edge[maxn<<1];
int head[maxn],cnt,n;
ll num[maxn],v[maxn];
void add(int u,int v){
    edge[cnt]={head[u],v};
    head[u]=cnt++;
}
int dfs(int now,int fa){
    int sum=1;
    for(int i=head[now];~i;i=edge[i].next){
        int tp=edge[i].to;
        if(tp!=fa)
            sum+=dfs(tp,now);
    }
    num[now]=mul(sum,(n-sum),mod);
    return sum;
}
void solve(){
    int m;
    scan(n);
    for(int i=0;i<=n;i++){
        head[i]=-1;
        num[i]=0;
        v[i]=0;
    }
    cnt=0;
    for(int i=1;i<n;i++){
        int u,v;
        scan(u,v);
        add(u,v);
        add(v,u);
    }
    dfs(1,0);
    scan(m);
    ll ans=0;
    for(int i=0;i<m;i++)
        scan(v[i]);
    sort(num+2,num+n+1);
    int t=n-1;
    for(int i=0;i<t;i++)
        num[i]=num[i+2];
    sort(v,v+m);
    for(int i=t;i<m;i++)
        v[t-1]=v[t-1]*v[i]%mod;
    if(t>m){
    for(int i=0;i<t-m;i++)
        ans=(ans+num[i])%mod;
    for(int i=0;i<m;i++)
        ans=(ans+num[i+t-m]*v[i])%mod;
    }
    else
    for(int i=0;i<t;i++)
        ans=(ans+num[i]*v[i],mod))%mod;
    print(ans);
}
int main(){
    int t;
    scan(t);
    while(t--){
        solve();
    }
    return 0;
}


#include <map>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <bitset>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define pi acos(-1)
#define INF 0x3f3f3f3f
#define mod 1000000007
#pragma GCC optimize(3 , "Ofast" , "inline")
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define fir first
#define sec second
#define PII pair<int,int>
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define gc getchar()
#define pc(x) putchar(x)
const double eps=1e-7;
const int maxn=1e5+10;
const int prime[15]= {2,3,5,7,11,13,17,19,23,29,31,37};
const int dir[4][2]={1,0,-1,0,0,1,0,-1};
int spgcd(int a,int b){while(b^=a^=b^=a%=b);return a;}
inline int mul(int a,int b,int p){return (ll)a*b%p;}
template<typename T> void scan(T &x){x = 0;bool _=0;T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}
using namespace std;
struct node{
    int next,to;
}edge[maxn<<1];
int head[maxn],cnt,n;
ll num[maxn],v[maxn];
void add(int u,int v){
    edge[cnt]={head[u],v};
    head[u]=cnt++;
}
int dfs(int now,int fa){
    int sum=1;
//    cout<<now<<endl;
    for(int i=head[now];~i;i=edge[i].next){
        int tp=edge[i].to;
        if(tp!=fa)
            sum+=dfs(tp,now);
    }
    num[now]=mul(sum,(n-sum),mod);
    return sum;
}
void solve(){
    int m;
    scan(n);
    cnt=0;
    for(int i=0;i<=n;i++){
        head[i]=-1;
        num[i]=0;
        v[i]=0;
    }
    for(int i=1;i<n;i++){
        int u,v;
        scan(u,v);
        add(u,v);
        add(v,u);
    }
    dfs(1,0);
    ll ans=0;
    scan(m);
    for(int i=1;i<=m;i++)
        scan(v[i]);
    sort(num+2,num+n+1);//通过数量
    sort(v+1,v+m+1);//k数组
//    for(int i=2;i<=n;i++)
//        cout<<num[i]<<' ';
//    cout<<endl;
//    for(int i=1;i<=m;i++)
//        cout<<v[i]<<' ';
//    cout<<endl;
    if(n-1>=m){
        for(int i=n,j=m;j>=1;i--,j--)
            ans=(ans+num[i]*v[j]%mod)%mod;
        for(int i=n-m;i>1;i--)
            ans=(ans+num[i])%mod;
    }
    else{
            for(int i=n;i<=m;i++)
                v[n-1]=(v[n-1]*v[i])%mod;
//                for(int i=1;i<=m;i++)
//                    cout<<v[i]<<' ';
//                cout<<endl;
            for(int i=1;i<n;i++)
                ans=(ans+num[i+1]*v[i])%mod;
    }
    print(ans);
}
int main(){
    int t;
    scan(t);
    while(t--){
        solve();
    }
    return 0;
}


#include <map>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <bitset>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define pi acos(-1)
#define INF 0x3f3f3f3f
#define mod 1000000007
#pragma GCC optimize(3 , "Ofast" , "inline")
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define fir first
#define sec second
#define PII pair<int,int>
#define all(x) (x).begin(),(x).end()
#define pb(x) push_back(x)
#define gc getchar()
#define pc(x) putchar(x)
const double eps=1e-7;
const int maxn=1e5+10;
const int prime[15]= {2,3,5,7,11,13,17,19,23,29,31,37};
const int dir[4][2]={1,0,-1,0,0,1,0,-1};
int spgcd(int a,int b){while(b^=a^=b^=a%=b);return a;}
inline int mul(int a,int b,int p){return (ll)a*b%p;}
template<typename T> void scan(T &x){x = 0;bool _=0;T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}
using namespace std;
struct node{
    int next,to;
}edge[maxn<<1];
int head[maxn],cnt,n;
vector<ll>num;
vector<ll>v;
void add(int u,int v){
    edge[cnt]={head[u],v};
    head[u]=cnt++;
}
int dfs(int now,int fa){
    int sum=1;
//    cout<<now<<endl;
    for(int i=head[now];~i;i=edge[i].next){
        int tp=edge[i].to;
        if(tp!=fa){
            sum+=dfs(tp,now);
        }
    }
    if(now!=1) num.pb((ll)sum*(n-sum)%mod);
    return sum;
}
void init(int n){
    for(int i=0;i<=2*n;i++)
        head[i]=-1;
    cnt=0;
    v.clear();
    num.clear();
}
void solve(){
    int m;
    scan(n);
    init(n);
    for(int i=1;i<n;i++){
        int u,v;
        scan(u,v);
        add(u,v);
        add(v,u);
    }
    dfs(1,0);
    scan(m);
    for(int i=0;i<n-m-1;i++)
        v.pb(1);
    for(int i=0;i<m;i++){
        ll x;
        scan(x);
        v.pb(x);
    }
    sort(all(v));
    sort(all(num));
    for(int i=n-1;i<v.size();i++)
        v[n-2]=v[n-2]*v[i]%mod;
    ll ans=0;
    for(int i=0;i<n-1;i++){
        ans=(ans+v[i]*num[i])%mod;
//        cout<<v[i]<<' '<<num[i]<<endl;
    }
    print(ans);
}
int main(){
    int t;
    scan(t);
    while(t--){
        solve();
    }
    return 0;
}


#include <map>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <bitset>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define pi acos(-1)
#define INF 0x3f3f3f3f
#define mod 1000000007
#pragma GCC optimize(3 , "Ofast" , "inline")
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define fir first
#define sec second
#define PII pair<int,int>
#define all(x) (x).begin(),(x).end()
#define pb(x) push_back(x)
#define gc getchar()
#define pc(x) putchar(x)
const double eps=1e-7;
const int maxn=1e5+10;
const int prime[15]= {2,3,5,7,11,13,17,19,23,29,31,37};
const int dir[4][2]={1,0,-1,0,0,1,0,-1};
int spgcd(int a,int b){while(b^=a^=b^=a%=b);return a;}
inline int mul(int a,int b,int p){return (ll)a*b%p;}
template<typename T> void scan(T &x){x = 0;bool _=0;T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}
using namespace std;
struct node{
    ll next,to;
}edge[maxn<<1];
ll head[maxn],cnt,n;
vector<ll>num;
vector<ll>v;
void add(ll u,ll v){
    edge[cnt]={head[u],v};
    head[u]=cnt++;
}
ll dfs(ll now,ll fa){
    ll sum=1;
    for(ll i=head[now];~i;i=edge[i].next){
        ll tp=edge[i].to;
        if(tp!=fa){
            sum+=dfs(tp,now);
        }
    }
    if(now!=1) num.pb(sum*(n-sum));
    return sum;
}
void init(ll n){
    for(ll i=0;i<=n;i++)
        head[i]=-1;
    cnt=0;
    v.clear();
    num.clear();
}
void solve(){
    ll m;
    scan(n);
    init(n);
    for(ll i=1;i<n;i++){
        ll u,v;
        scan(u,v);
        add(u,v);
        add(v,u);
    }
    dfs(1,0);
    scan(m);
    for(ll i=0;i<n-m-1;i++)
        v.pb(1);
    for(ll i=0;i<m;i++){
        ll x;
        scan(x);
        v.pb(x);
    }
    sort(all(v));
    sort(all(num));
    for(ll i=v.size()-1;i>=n-1;i--){
        v[n-2]=v[n-2]*v[i]%mod;
        v.pop_back();
    }
    ll ans=0;
    for(ll i=0;i<n-1;i++){
        ans=(ans+v[i]*num[i]%mod)%mod;
    }
    cout<<ans<<endl;
}
int main(){
    int t;
    scan(t);
    while(t--){
        solve();
    }
    return 0;
}


#include <map>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <bitset>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define pi acos(-1)
#define INF 0x3f3f3f3f
#define mod 1000000007
#pragma GCC optimize(3 , "Ofast" , "inline")
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define fir first
#define sec second
#define PII pair<int,int>
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define gc getchar()
#define pc(x) putchar(x)
const double eps=1e-7;
const int maxn=1e5+10;
const int prime[15]= {2,3,5,7,11,13,17,19,23,29,31,37};
const int dir[4][2]={1,0,-1,0,0,1,0,-1};
int spgcd(int a,int b){while(b^=a^=b^=a%=b);return a;}
inline int mul(int a,int b,int p){return (ll)a*b%p;}
template<typename T> void scan(T &x){x = 0;bool _=0;T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}
using namespace std;
struct node{
    int next,to;
}edge[maxn<<1];
int head[maxn],cnt,n;
ll num[maxn],v[maxn];
void add(int u,int v){
    edge[cnt]={head[u],v};
    head[u]=cnt++;
}
int dfs(int now,int fa){
    int sum=1;
    for(int i=head[now];~i;i=edge[i].next){
        int tp=edge[i].to;
        if(tp!=fa)
            sum+=dfs(tp,now);
    }
    num[now]=mul(sum,(n-sum),mod);
    return sum;
}
void solve(){
    int m;
    scan(n);
    for(int i=0;i<=n;i++){
        head[i]=-1;
        num[i]=0;
        v[i]=0;
    }
    cnt=0;
    for(int i=1;i<n;i++){
        int u,v;
        scan(u,v);
        add(u,v);
        add(v,u);
    }
    dfs(1,0);
    scan(m);
    ll ans=0;
    for(int i=0;i<m;i++)
        scan(v[i]);
    sort(num+2,num+n+1);
    int t=n-1;
    for(int i=0;i<t;i++)
        num[i]=num[i+2];
    sort(v,v+m);
    for(int i=t;i<m;i++)
        v[t-1]=v[t-1]*v[i]%mod;
    if(t>m){
    for(int i=0;i<t-m;i++)
        ans=(ans+num[i])%mod;
    for(int i=0;i<t;i++)
        ans=(ans+num[i+t-m]*v[i])%mod;
    }
    else
    for(int i=0;i<t;i++)
        ans=(ans+num[i]*v[i])%mod;
    print(ans);
}
int main(){
    int t;
    scan(t);
    while(t--){
        solve();
    }
    return 0;
}


#include <map>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <bitset>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define pi acos(-1)
#define INF 0x3f3f3f3f
#define mod 1000000007
#pragma GCC optimize(3 , "Ofast" , "inline")
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define fir first
#define sec second
#define PII pair<int,int>
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define gc getchar()
#define pc(x) putchar(x)
const double eps=1e-7;
const int maxn=1e5+10;
const int prime[15]= {2,3,5,7,11,13,17,19,23,29,31,37};
const int dir[4][2]={1,0,-1,0,0,1,0,-1};
int spgcd(int a,int b){while(b^=a^=b^=a%=b);return a;}
inline int mul(int a,int b,int p){return (ll)a*b%p;}
template<typename T> void scan(T &x){x = 0;bool _=0;T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}
using namespace std;
struct node{
    int next,to;
}edge[maxn<<1];
int head[maxn],cnt,n;
ll num[maxn],v[maxn];
void add(int u,int v){
    edge[cnt]={head[u],v};
    head[u]=cnt++;
}
int dfs(int now,int fa){
    int sum=1;
    for(int i=head[now];~i;i=edge[i].next){
        int tp=edge[i].to;
        if(tp!=fa)
            sum+=dfs(tp,now);
    }
    num[now]=mul(sum,(n-sum),mod);
    return sum;
}
void solve(){
    int m;
    scan(n);
    for(int i=0;i<=n;i++){
        head[i]=-1;
        num[i]=0;
        v[i]=0;
    }
    cnt=0;
    for(int i=1;i<n;i++){
        int u,v;
        scan(u,v);
        add(u,v);
        add(v,u);
    }
    dfs(1,0);
    scan(m);
    ll ans=0;
    for(int i=0;i<m;i++)
        scan(v[i]);
    sort(num+2,num+n+1);
    int t=n-1;
    for(int i=0;i<t;i++)
        num[i]=num[i+2];
    sort(v,v+m);
    for(int i=t;i<m;i++)
        v[t-1]=v[t-1]*v[i]%mod;
    if(t>m){
    for(int i=0;i<t-m;i++)
        ans=(ans+num[i])%mod;
    for(int i=0;i<m;i++)
        ans=(ans+num[i+t-m]*v[i])%mod;
    }
    else
    for(int i=0;i<t;i++)
        ans=(ans+num[i]*v[i],mod)%mod;
    print(ans);
}
int main(){
    int t;
    scan(t);
    while(t--){
        solve();
    }
    return 0;
}


#include <map>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <bitset>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define pi acos(-1)
#define INF 0x3f3f3f3f
#define mod 1000000007
#pragma GCC optimize(3 , "Ofast" , "inline")
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define fir first
#define sec second
#define PII pair<int,int>
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define gc getchar()
#define pc(x) putchar(x)
const double eps=1e-7;
const int maxn=1e5+10;
const int prime[15]= {2,3,5,7,11,13,17,19,23,29,31,37};
const int dir[4][2]={1,0,-1,0,0,1,0,-1};
int spgcd(int a,int b){while(b^=a^=b^=a%=b);return a;}
inline int mul(int a,int b,int p){return (ll)a*b%p;}
template<typename T> void scan(T &x){x = 0;bool _=0;T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}
using namespace std;
struct node{
    int next,to;
}edge[maxn<<1];
int head[maxn],cnt,n;
ll num[maxn],v[maxn];
void add(int u,int v){
    edge[cnt]={head[u],v};
    head[u]=cnt++;
}
int dfs(int now,int fa){
    int sum=1;
//    cout<<now<<endl;
    for(int i=head[now];~i;i=edge[i].next){
        int tp=edge[i].to;
        if(tp!=fa)
            sum+=dfs(tp,now);
    }
    num[now]=mul(sum,(n-sum),mod);
    return sum;
}
void solve(){
    int m;
    scan(n);
    cnt=0;
    for(int i=0;i<=n;i++){
        head[i]=-1;
        num[i]=0;
        v[i]=0;
    }
    for(int i=1;i<n;i++){
        int u,v;
        scan(u,v);
        add(u,v);
        add(v,u);
    }
    dfs(1,0);
    ll ans=0;
    scan(m);
    for(int i=1;i<=m;i++)
        scan(v[i]);
    sort(num+2,num+n+1);//通过数量
    sort(v+1,v+m+1);//k数组
//    for(int i=2;i<=n;i++)
//        cout<<num[i]<<' ';
//    cout<<endl;
//    for(int i=1;i<=m;i++)
//        cout<<v[i]<<' ';
//    cout<<endl;
    if(n-1>=m){
        for(int i=n,j=m;j>=1;i--,j--)
            ans=(ans+num[i]*v[j]%mod)%mod;
        for(int i=n-m;i>1;i--)
            ans=(ans+num[i])%mod;
    }
    else{
            for(int i=n+1;i<=m;i++){
                v[n]=(v[n]*v[i])%mod;
            for(int i=0;i<=n;i++)
                ans=(ans+num[i]*v[i])%mod;
        }
    }
    print(ans);
}
int main(){
    int t;
    scan(t);
    while(t--){
        solve();
    }
    return 0;
}


#include <map>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <bitset>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define pi acos(-1)
#define INF 0x3f3f3f3f
#define mod 1000000007
#pragma GCC optimize(3 , "Ofast" , "inline")
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define fir first
#define sec second
#define PII pair<int,int>
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define gc getchar()
#define pc(x) putchar(x)
const double eps=1e-7;
const int maxn=1e5+10;
const int prime[15]= {2,3,5,7,11,13,17,19,23,29,31,37};
const int dir[4][2]={1,0,-1,0,0,1,0,-1};
int spgcd(int a,int b){while(b^=a^=b^=a%=b);return a;}
inline int mul(int a,int b,int p){return (ll)a*b%p;}
template<typename T> void scan(T &x){x = 0;bool _=0;T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}
using namespace std;
struct node{
    int next,to;
}edge[maxn<<1];
int head[maxn],cnt,n;
int num[maxn],v[maxn];
void add(int u,int v){
    edge[cnt]={head[u],v};
    head[u]=cnt++;
}
int dfs(int now,int fa){
    int sum=1;
    for(int i=head[now];~i;i=edge[i].next){
        int tp=edge[i].to;
        if(tp!=fa)
            sum+=dfs(tp,now);
    }
    num[now]=mul(sum,(n-sum),mod);
    return sum;
}
void solve(){
    int m;
    scan(n);
    for(int i=0;i<=n;i++)
        head[i]=-1;
    cnt=0;
    for(int i=1;i<n;i++){
        int u,v;
        scan(u,v);
        add(u,v);
        add(v,u);
    }
    dfs(1,0);
    scan(m);
    for(int i=0;i<n-m-1;i++)
        v[i]=1;
    for(int i=max(0,n-m-1);i<n-1;i++){
        ll x;
        scanf("%lld",&x);
        v[i]=x;
    }
    for(int i=n-1;i<m;i++){
        ll x;
        scan(x);
        v[n-1]=mul(v[n-1],x,mod);
    }
    ll ans=0;
    sort(num+2,num+1+n);
    sort(v,v+n-1);
    for(int i=0;i<n-1;i++){
//        cout<<v[i]<<' ';
        ans=(ans+mul(v[i],num[i+2],mod))%mod;
    }
//    cout<<endl;
    print(ans);
}
int main(){
    int t;
    scan(t);
    while(t--){
        solve();
    }
    return 0;
}


#include <map>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <bitset>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define pi acos(-1)
#define INF 0x3f3f3f3f
#define mod 1000000007
#pragma GCC optimize(3 , "Ofast" , "inline")
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define fir first
#define sec second
#define PII pair<int,int>
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define gc getchar()
#define pc(x) putchar(x)
const double eps=1e-7;
const int maxn=1e5+10;
const int prime[15]= {2,3,5,7,11,13,17,19,23,29,31,37};
const int dir[4][2]={1,0,-1,0,0,1,0,-1};
int spgcd(int a,int b){while(b^=a^=b^=a%=b);return a;}
inline int mul(int a,int b,int p){return (ll)a*b%p;}
template<typename T> void scan(T &x){x = 0;bool _=0;T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}
using namespace std;
struct node{
    int next,to;
}edge[maxn<<1];
int head[maxn],cnt,n;
ll num[maxn],v[maxn];
void add(int u,int v){
    edge[cnt]={head[u],v};
    head[u]=cnt++;
}
int dfs(int now,int fa){
    int sum=1;
//    cout<<now<<endl;
    for(int i=head[now];~i;i=edge[i].next){
        int tp=edge[i].to;
        if(tp!=fa)
            sum+=dfs(tp,now);
    }
    num[now-2]=mul(sum,(n-sum),mod);
    return sum;
}
void solve(){
    int m;
    scan(n);
    cnt=0;
    for(int i=0;i<=n;i++){
        head[i]=-1;
        num[i]=0;
        v[i]=0;
    }
    for(int i=1;i<n;i++){
        int u,v;
        scan(u,v);
        add(u,v);
        add(v,u);
    }
    dfs(1,0);
    ll ans=0;
    scan(m);
    for(int i=0;i<m;i++)
        scan(v[i]);
    int t=n-1;
    sort(num,num+t);
    sort(v,v+m);
    for(int i=t;i<m;i++)
        v[t-1]=(v[t-1]*v[i])%mod;
    if(t>m){
        for(int i=0;i<t-m;i++)
            ans=(ans+num[i])%mod;
        for(int i=0;i<m;i++)
            ans=(ans+num[i+t-m]*v[i])%mod;
    }
    else{
        for(int i=0;i<t;i++)
            ans=(ans+num[i]*v[i])%mod;
    }
    print(ans);
}
int main(){
    int t;
    scan(t);
    while(t--){
        solve();
    }
    return 0;
}


#include <map>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <bitset>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define pi acos(-1)
#define INF 0x3f3f3f3f
#define mod 1000000007
#pragma GCC optimize(3 , "Ofast" , "inline")
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define fir first
#define sec second
#define PII pair<int,int>
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define gc getchar()
#define pc(x) putchar(x)
const double eps=1e-7;
const int maxn=1e5+10;
const int prime[15]= {2,3,5,7,11,13,17,19,23,29,31,37};
const int dir[4][2]={1,0,-1,0,0,1,0,-1};
int spgcd(int a,int b){while(b^=a^=b^=a%=b);return a;}
inline int mul(int a,int b,int p){return (ll)a*b%p;}
template<typename T> void scan(T &x){x = 0;bool _=0;T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}
using namespace std;
struct node{
    int next,to;
}edge[maxn<<1];
int head[maxn],cnt,n;
ll num[maxn],v[maxn];
void add(int u,int v){
    edge[cnt]={head[u],v};
    head[u]=cnt++;
}
int dfs(int now,int fa){
    int sum=1;
//    cout<<now<<endl;
    for(int i=head[now];~i;i=edge[i].next){
        int tp=edge[i].to;
        if(tp!=fa)
            sum+=dfs(tp,now);
    }
    num[now]=mul(sum,(n-sum),mod);
    return sum;
}
void solve(){
    int m;
    scan(n);
    cnt=0;
    for(int i=0;i<=n;i++){
        head[i]=-1;
        num[i]=0;
        v[i]=0;
    }
    for(int i=1;i<n;i++){
        int u,v;
        scan(u,v);
        add(u,v);
        add(v,u);
    }
    dfs(1,0);
    ll ans=0;
    scan(m);
    for(int i=1;i<=m;i++)
        scan(v[i]);
    sort(num+2,num+n+1);//通过数量
    sort(v+1,v+m+1);//k数组
    for(int i=2;i<=n;i++)
        cout<<num[i]<<' ';
    cout<<endl;
    for(int i=1;i<=m;i++)
        cout<<v[i]<<' ';
    cout<<endl;
    if(n-1>=m){
        for(int i=n,j=m;j>=1;i--,j--)
            ans=(ans+num[i]*v[j]%mod)%mod;
        for(int i=n-m;i>1;i--)
            ans=(ans+num[i])%mod;
    }
    else{
            for(int i=n;i<=m;i++)
                v[n-1]=(v[n-1]*v[i])%mod;
//                for(int i=1;i<=m;i++)
//                    cout<<v[i]<<' ';
//                cout<<endl;
            for(int i=1;i<n;i++)
                ans=(ans+num[i+1]*v[i])%mod;
    }
    print(ans);
}
int main(){
    int t;
    scan(t);
    while(t--){
        solve();
    }
    return 0;
}


#include <map>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <bitset>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define pi acos(-1)
#define INF 0x3f3f3f3f
#define mod 1000000007
#pragma GCC optimize(3 , "Ofast" , "inline")
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define fir first
#define sec second
#define PII pair<int,int>
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define gc getchar()
#define pc(x) putchar(x)
const double eps=1e-7;
const int maxn=1e5+10;
const int prime[15]= {2,3,5,7,11,13,17,19,23,29,31,37};
const int dir[4][2]={1,0,-1,0,0,1,0,-1};
int spgcd(int a,int b){while(b^=a^=b^=a%=b);return a;}
inline int mul(int a,int b,int p){return (ll)a*b%p;}
template<typename T> void scan(T &x){x = 0;bool _=0;T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}
using namespace std;
struct node{
    int next,to;
}edge[maxn<<1];
int head[maxn],cnt,n;
ll num[maxn],v[maxn];
void add(int u,int v){
    edge[cnt]={head[u],v};
    head[u]=cnt++;
}
int dfs(int now,int fa){
    int sum=1;
    for(int i=head[now];~i;i=edge[i].next){
        int tp=edge[i].to;
        if(tp!=fa)
            sum+=dfs(tp,now);
    }
    num[now]=mul(sum,(n-sum),mod);
    return sum;
}
void solve(){
    int m;
    scan(n);
    for(int i=0;i<=n;i++){
        head[i]=-1;
        num[i]=0;
        v[i]=0;
    }
    cnt=0;
    for(int i=1;i<n;i++){
        int u,v;
        scan(u,v);
        add(u,v);
        add(v,u);
    }
    dfs(1,0);
    scan(m);
    ll ans=0;
    for(int i=0;i<m;i++)
        scan(v[i]);
    sort(num+2,num+n+1);
    int t=n-1;
    for(int i=0;i<t;i++)
        num[i]=num[i+2];
    sort(v,v+m);
    for(int i=t;i<m;i++)
        v[t-1]=v[t-1]*v[i]%mod;
    if(t>m){
    for(int i=0;i<t-m;i++)
        ans=(ans+num[i])%mod;
    for(int i=0;i<m;i++)
        ans=(ans+num[i+t-m]*v[i])%mod;
    }
    else
    for(int i=0;i<t;i++)
        ans=(ans+num[i]*v[i])%mod;
    print(ans);
}
int main(){
    int t;
    scan(t);
    while(t--){
        solve();
    }
    return 0;
}


#include <map>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <bitset>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define pi acos(-1)
#define INF 0x3f3f3f3f
#define mod 1000000007
#pragma GCC optimize(3 , "Ofast" , "inline")
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define fir first
#define sec second
#define PII pair<int,int>
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define gc getchar()
#define pc(x) putchar(x)
const double eps=1e-7;
const int maxn=1e5+10;
const int prime[15]= {2,3,5,7,11,13,17,19,23,29,31,37};
const int dir[4][2]={1,0,-1,0,0,1,0,-1};
int spgcd(int a,int b){while(b^=a^=b^=a%=b);return a;}
inline int mul(int a,int b,int p){return (ll)a*b%p;}
template<typename T> void scan(T &x){x = 0;bool _=0;T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}
using namespace std;
struct node{
    int next,to;
}edge[maxn<<1];
int head[maxn],cnt,n;
ll num[maxn],v[maxn];
void add(int u,int v){
    edge[cnt]={head[u],v};
    head[u]=cnt++;
}
int dfs(int now,int fa){
    int sum=1;
//    cout<<now<<endl;
    for(int i=head[now];~i;i=edge[i].next){
        int tp=edge[i].to;
        if(tp!=fa)
            sum+=dfs(tp,now);
    }
    num[now-2]=mul(sum,(n-sum),mod);
    return sum;
}
void solve(){
    int m;
    scan(n);
    cnt=0;
    for(int i=0;i<=n;i++){
        head[i]=-1;
        num[i]=0;
        v[i]=0;
    }
    for(int i=1;i<n;i++){
        int u,v;
        scan(u,v);
        add(u,v);
        add(v,u);
    }
    dfs(1,0);
    ll ans=0;
    scan(m);
    for(int i=0;i<m;i++)
        scan(v[i]);
    int t=n-1;
    sort(num,num+t);//通过数量
    sort(v,v+m);//k数组
//    for(int i=0;i<m;i++)
//        cout<<v[i]<<' ';
//    cout<<endl;
    if(t>m){
        for(int i=0;i<t-m;i++)
            ans=(ans+num[i])%mod;
        for(int i=0;i<m;i++)
            ans=(ans+v[i]*num[i+t-m])%mod;
    }
    else {
        if(t<m)
        for(int i=t;i<m;i++){
//            cout<<v[i]<<' ';
            v[t-1]=(v[t-1]*v[i])%mod;
        }
//        cout<<endl;
        for(int i=0;i<t;i++)
            ans=(ans+num[i]*v[i])%mod;
    }
    print(ans);
}
int main(){
    int t;
    scan(t);
    while(t--){
        solve();
    }
    return 0;
}


#include <map>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <bitset>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define pi acos(-1)
#define INF 0x3f3f3f3f
#define mod 1000000007
#pragma GCC optimize(3 , "Ofast" , "inline")
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define fir first
#define sec second
#define PII pair<int,int>
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define gc getchar()
#define pc(x) putchar(x)
const double eps=1e-7;
const int maxn=1e5+10;
const int prime[15]= {2,3,5,7,11,13,17,19,23,29,31,37};
const int dir[4][2]={1,0,-1,0,0,1,0,-1};
int spgcd(int a,int b){while(b^=a^=b^=a%=b);return a;}
inline int mul(int a,int b,int p){return (ll)a*b%p;}
template<typename T> void scan(T &x){x = 0;bool _=0;T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}
using namespace std;
struct node{
    int next,to;
}edge[maxn<<1];
int head[maxn],cnt,n;
int num[maxn],v[maxn];
void add(int u,int v){
    edge[cnt]={head[u],v};
    head[u]=cnt++;
}
int dfs(int now,int fa){
    int sum=1;
    for(int i=head[now];~i;i=edge[i].next){
        int tp=edge[i].to;
        if(tp!=fa)
            sum+=dfs(tp,now);
    }
    num[now]=mul(sum,(n-sum),mod);
    return sum;
}
void solve(){
    int m;
    scan(n);
    for(int i=0;i<=n;i++)
        head[i]=-1;
    cnt=0;
    for(int i=1;i<n;i++){
        int u,v;
        scan(u,v);
        add(u,v);
        add(v,u);
    }
    dfs(1,0);
    scan(m);
    ll ans=0;
    for(int i=0;i<m;i++)
        scan(v[i]);
    sort(num+2,num+n+1);
    int t=n-1;
    for(int i=0;i<t;i++)
        num[i]=num[i+2];
    sort(v,v+m);
    for(int i=t;i<m;i++)
        v[t-1]=mul(v[t-1],v[i],mod);
//    for(int i=0;i<t;i++)
//        cout<<num[i]<<' ';
//    cout<<endl;
//    for(int i=0;i<t;i++)
//        cout<<v[i]<<' ';
//    cout<<endl;
    if(t>m){
    for(int i=0;i<t-m;i++)
        ans=(ans+num[i])%mod;
    for(int i=0;i<t;i++)
        ans=(ans+mul(num[i+t-m],v[i],mod))%mod;
    }
    else
    for(int i=0;i<t;i++)
        ans=(ans+mul(num[i],v[i],mod))%mod;
    print(ans);
}
int main(){
    int t;
    scan(t);
    while(t--){
        solve();
    }
    return 0;
}


#include <map>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <bitset>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define pi acos(-1)
#define INF 0x3f3f3f3f
#define mod 1000000007
#pragma GCC optimize(3 , "Ofast" , "inline")
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define fir first
#define sec second
#define PII pair<int,int>
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define gc getchar()
#define pc(x) putchar(x)
const double eps=1e-7;
const int maxn=2e5+10;
const int prime[15]= {2,3,5,7,11,13,17,19,23,29,31,37};
const int dir[4][2]={1,0,-1,0,0,1,0,-1};
int spgcd(int a,int b){while(b^=a^=b^=a%=b);return a;}
inline int mul(int a,int b,int p){return (ll)a*b%p;}
template<typename T> void scan(T &x){x = 0;bool _=0;T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}
using namespace std;
struct node{
    int next,to;
}edge[maxn];
int head[maxn],cnt,n;
ll num[maxn];
void add(int u,int v){
    edge[cnt]={head[u],v};
    head[u]=cnt++;
}
int dfs(int now,int fa){
    int sum=1;
    for(int i=head[now];~i;i=edge[i].next){
        int tp=edge[i].to;
        if(tp!=fa){
            sum+=dfs(tp,now);
        }
    }
    num[now]=sum*(n-sum);
    return sum;
}
void solve(){
    int m;
    scan(n);
    for(int i=0;i<=n;i++)
        head[i]=-1;
    cnt=0;
    for(int i=1;i<n;i++){
        int u,v;
        scan(u,v);
        add(u,v);
        add(v,u);
    }
    for(int i=head[1];~i;i=edge[i].next){
        dfs(edge[i].to,1);
    }
    scan(m);
    vector<ll>v;
    for(int i=1;i<n-m;i++)
        v.pb(1);
    for(int i=max(0,n-m);i<n;i++){
        ll x;
        scanf("%lld",&x);
        v.pb(x);
    }
    for(int i=n;i<=m;i++){
        ll x;
        scan(x);
        v[n-1]=v[n-1]*x%mod;
    }
    ll ans=0;
    sort(num+2,num+1+n);
//    for(int i=2;i<=n;i++)
//        cout<<num[i]<<' ';
//    cout<<endl;
    sort(v.begin(),v.end());
    for(int i=0;i<n-1;i++){
//        cout<<v[i]<<' ';
        ans=(ans+v[i]*num[i+2])%mod;
    }
//    cout<<endl;
    print(ans);
//    for(int i=)
}
int main(){
    int t;
    scan(t);
    while(t--){
        solve();
    }
    return 0;
}


#include <map>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <bitset>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define pi acos(-1)
#define INF 0x3f3f3f3f
#define mod 1000000007
#pragma GCC optimize(3 , "Ofast" , "inline")
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define fir first
#define sec second
#define PII pair<int,int>
#define all(x) (x).begin(),(x).end()
#define pb(x) push_back(x)
#define gc getchar()
#define pc(x) putchar(x)
const double eps=1e-7;
const int maxn=1e5+10;
const int prime[15]= {2,3,5,7,11,13,17,19,23,29,31,37};
const int dir[4][2]={1,0,-1,0,0,1,0,-1};
int spgcd(int a,int b){while(b^=a^=b^=a%=b);return a;}
inline int mul(int a,int b,int p){return (ll)a*b%p;}
template<typename T> void scan(T &x){x = 0;bool _=0;T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}
using namespace std;
struct node{
    ll next,to;
}edge[maxn<<1];
ll head[maxn],cnt,n;
vector<ll>num;
vector<ll>v;
void add(ll u,ll v){
    edge[cnt]={head[u],v};
    head[u]=cnt++;
}
ll dfs(ll now,ll fa){
    ll sum=1;
    for(ll i=head[now];~i;i=edge[i].next){
        ll tp=edge[i].to;
        if(tp!=fa){
            sum+=dfs(tp,now);
        }
    }
    if(now!=1) num.pb((ll)(sum*(n-sum)%mod));
    return sum;
}
void init(ll n){
    for(ll i=0;i<=n;i++)
        head[i]=-1;
    cnt=0;
    v.clear();
    num.clear();
}
void solve(){
    ll m;
    scan(n);
    init(n);
    for(ll i=1;i<n;i++){
        ll u,v;
        scan(u,v);
        add(u,v);
        add(v,u);
    }
    dfs(1,0);
    scan(m);
    for(ll i=0;i<n-m-1;i++)
        v.pb(1);
    for(ll i=0;i<m;i++){
        ll x;
        scan(x);
        v.pb(x);
    }
    sort(all(v));
    sort(all(num));
    for(ll i=v.size()-1;i>=n-1;i--){
        v[n-2]=v[n-2]*v[i]%mod;
        v.pop_back();
    }
    ll ans=0;
    for(ll i=0;i<n-1;i++){
        ans=(ans+v[i]*num[i])%mod;
    }
    print(ans);
}
int main(){
    int t;
    scan(t);
    while(t--){
        solve();
    }
    return 0;
}


#include <map>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <bitset>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define pi acos(-1)
#define INF 0x3f3f3f3f
#define mod 1000000007
#pragma GCC optimize(3 , "Ofast" , "inline")
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define fir first
#define sec second
#define PII pair<int,int>
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define gc getchar()
#define pc(x) putchar(x)
const double eps=1e-7;
const int maxn=1e5+10;
const int prime[15]= {2,3,5,7,11,13,17,19,23,29,31,37};
const int dir[4][2]={1,0,-1,0,0,1,0,-1};
int spgcd(int a,int b){while(b^=a^=b^=a%=b);return a;}
inline int mul(int a,int b,int p){return (ll)a*b%p;}
template<typename T> void scan(T &x){x = 0;bool _=0;T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}
using namespace std;
struct node{
    int next,to;
}edge[maxn<<1];
int head[maxn],cnt,n;
ll num[maxn],v[maxn];
void add(int u,int v){
    edge[cnt]={head[u],v};
    head[u]=cnt++;
}
int dfs(int now,int fa){
    int sum=1;
    cout<<now<<endl;
    for(int i=head[now];~i;i=edge[i].next){
        int tp=edge[i].to;
        if(tp!=fa)
            sum+=dfs(tp,now);
    }
    num[now-2]=mul(sum,(n-sum),mod);
    return sum;
}
void solve(){
    int m;
    scan(n);
    cnt=0;
    for(int i=0;i<=n;i++){
        head[i]=-1;
        num[i]=0;
        v[i]=0;
    }
    for(int i=1;i<n;i++){
        int u,v;
        scan(u,v);
        add(u,v);
        add(v,u);
    }
    dfs(1,0);
    ll ans=0;
    scan(m);
    for(int i=0;i<m;i++)
        scan(v[i]);
    int t=n-1;
    sort(num,num+t);
    sort(v,v+m);
    for(int i=t;i<m;i++)
        v[t-1]=(v[t-1]*v[i])%mod;
    if(t>m){
        for(int i=0;i<t-m;i++)
            ans=(ans+num[i])%mod;
        for(int i=0;i<m;i++)
            ans=(ans+num[i+t-m]*v[i])%mod;
    }
    else{
        for(int i=0;i<t;i++)
            ans=(ans+num[i]*v[i])%mod;
    }
    print(ans);
}
int main(){
    int t;
    scan(t);
    while(t--){
        solve();
    }
    return 0;
}


#include <map>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <bitset>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define pi acos(-1)
#define INF 0x3f3f3f3f
#define mod 1000000007
#pragma GCC optimize(3 , "Ofast" , "inline")
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define fir first
#define sec second
#define PII pair<int,int>
#define all(x) (x).begin(),(x).end()
#define pb(x) push_back(x)
#define gc getchar()
#define pc(x) putchar(x)
const double eps=1e-7;
const int maxn=1e5+10;
const int prime[15]= {2,3,5,7,11,13,17,19,23,29,31,37};
const int dir[4][2]={1,0,-1,0,0,1,0,-1};
int spgcd(int a,int b){while(b^=a^=b^=a%=b);return a;}
inline int mul(int a,int b,int p){return (ll)a*b%p;}
template<typename T> void scan(T &x){x = 0;bool _=0;T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}
using namespace std;
struct node{
    int next,to;
}edge[maxn<<1];
int head[maxn],cnt,n;
vector<ll>num;
vector<ll>v;
void add(int u,int v){
    edge[cnt]={head[u],v};
    head[u]=cnt++;
}
int dfs(int now,int fa){
    int sum=1;
//    cout<<now<<endl;
    for(int i=head[now];~i;i=edge[i].next){
        int tp=edge[i].to;
        if(tp!=fa){
            sum+=dfs(tp,now);
        }
    }
    if(now!=1) num.pb((ll)sum*(n-sum)%mod);
    return sum;
}
void init(int n){
    for(int i=0;i<=n;i++)
        head[i]=-1;
    cnt=0;
    v.clear();
    num.clear();
}
void solve(){
    int m;
    scan(n);
    init(n);
    for(int i=1;i<n;i++){
        int u,v;
        scan(u,v);
        add(u,v);
        add(v,u);
    }
    dfs(1,0);
    scan(m);
    for(int i=0;i<n-m-1;i++)
        v.pb(1);
    for(int i=0;i<m;i++){
        ll x;
        scan(x);
        v.pb(x);
    }
    sort(all(v));
    sort(all(num));
    for(int i=n-1;i<v.size();i++)
        v[n-2]=v[n-2]*v[i]%mod;
    ll ans=0;
    for(int i=0;i<n-1;i++){
        ans=(ans+v[i]*num[i])%mod;
//        cout<<v[i]<<' '<<num[i]<<endl;
    }
    print(ans);
}
int main(){
    int t;
    scan(t);
    while(t--){
        solve();
    }
    return 0;
}


#include <map>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <bitset>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define pi acos(-1)
#define INF 0x3f3f3f3f
#define mod 1000000007
#pragma GCC optimize(3 , "Ofast" , "inline")
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define fir first
#define sec second
#define PII pair<int,int>
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define gc getchar()
#define pc(x) putchar(x)
const double eps=1e-7;
const int maxn=1e5+10;
const int prime[15]= {2,3,5,7,11,13,17,19,23,29,31,37};
const int dir[4][2]={1,0,-1,0,0,1,0,-1};
int spgcd(int a,int b){while(b^=a^=b^=a%=b);return a;}
inline int mul(int a,int b,int p){return (ll)a*b%p;}
template<typename T> void scan(T &x){x = 0;bool _=0;T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}
using namespace std;
struct node{
    int next,to;
}edge[maxn<<1];
int head[maxn],cnt,n;
int num[maxn],v[maxn];
void add(int u,int v){
    edge[cnt]={head[u],v};
    head[u]=cnt++;
}
int dfs(int now,int fa){
    int sum=1;
    for(int i=head[now];~i;i=edge[i].next){
        int tp=edge[i].to;
        if(tp!=fa)
            sum+=dfs(tp,now);
    }
    num[now]=mul(sum,(n-sum),mod);
    return sum;
}
void solve(){
    int m;
    scan(n);
    for(int i=0;i<=n;i++){
        head[i]=-1;
        num[i]=0;
        v[i]=0;
    }
    cnt=0;
    for(int i=1;i<n;i++){
        int u,v;
        scan(u,v);
        add(u,v);
        add(v,u);
    }
    dfs(1,0);
    scan(m);
    ll ans=0;
    for(int i=0;i<m;i++)
        scan(v[i]);
    sort(num+2,num+n+1);
    int t=n-1;
    for(int i=0;i<t;i++)
        num[i]=num[i+2];
    sort(v,v+m);
    for(int i=t;i<m;i++)
        v[t-1]=mul(v[t-1],v[i],mod);
    if(t>m){
    for(int i=0;i<t-m;i++)
        ans+=num[i];
    for(int i=0;i<t;i++)
        ans=(ans+mul(num[i+t-m],v[i],mod))%mod;
    }
    else
    for(int i=0;i<t;i++)
        ans=(ans+mul(num[i],v[i],mod))%mod;
    print(ans);
}
int main(){
    int t;
    scan(t);
    while(t--){
        solve();
    }
    return 0;
}


#include<bits\stdc++.h>
using namespace std;
long long int num=10e8+7;
long long int dfs(long long int s,vector<int> &graph,vector<bool> &visited,vector<long long int> &node_count,int n)
{
    long long int count=1;
    visited[s]=1;
    for(int v:graph[s])
    {
        if(!visited[v])
        {
            long long int count1=dfs(v,graph,visited,node_count,n);
            node_count.push_back(count1*(n-count1));
            count+=count1;
            count%=num;
        }
    }
    return count;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        vector<int> graph[n+1];
        for(int i=0;i<n-1;i++)
        {
            int a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }
        vector<long long int> count;
        vector<bool> visited(n+1,false);
        dfs(1,graph,visited,count,n);
        int m;
        cin>>m;
        vector<long long int> factors(m);
        for(int i=0;i<m;i++)
        {
            cin>>factors[i];
        }
        while(factors.size()!=n-1)
        {
            factors.push_back(1);
        }
        sort(factors.begin(),factors.end());
        long long int ans=0;
        sort(count.begin(),count.end());
        for(int i=0;i<n-1;i++)
        {
            ans+=(count[i]*factors[i]);
            ans%=num;
        }

        cout<<ans<<endl;
    }
}


#include<bits\stdc++.h>
using namespace std;
using ll=long long;
ll n;
ll mod=1e9+7;
void dfs(int s,vector<ll> graph[],vector<bool> &visited,vector<ll> &count,vector<ll> &prod)
{
    visited[s]=1;
    for(int v:graph[s])
    {
        if(!visited[v])
        {
            dfs(v,graph,visited,count,prod);
            count[s]+=count[v];
            count[s]%=mod;
            prod.push_back((count[v]*(n-count[v])));
        }
    }
    count[s]+=1LL;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n;
        vector<ll> graph[n+1];
        for(int i=0;i<n-1;i++)
        {
            ll a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }
        vector<ll> count(n+1,0),prod;
        vector<bool> visited(n+1,false);
        dfs(1,graph,visited,count,prod);
        sort(prod.begin(),prod.end());
        ll m;
        cin>>m;
        vector<ll> factor(m);
        for(int i=0;i<m;i++)
        {
            cin>>factor[i];
            factor[i]%=mod;
        }
        if(m>n-1)
        {
            sort(factor.begin(),factor.end());
            while(factor.size()!=n-1)
            {
                factor[factor.size()-2]*=factor[factor.size()-1];
                factor[factor.size()-2]%=mod;
                factor.pop_back();
            }
        }
        else
        {
            while(factor.size()!=n-1)
            {
                factor.push_back(1LL);
            }
            sort(factor.begin(),factor.end());
        }
        for(int i=0;i<n-1;i++)
        {
            factor[i]%=mod;
            prod[i]%=mod;
        }
        ll ans=0;
        for(int i=0;i<n-1;i++)
        {
            ll temp=(factor[i]*prod[i]);
            temp%=mod;
            ans+=temp;
            ans%=mod;
        }
        ans%=mod;
        cout<<ans<<endl;
    }
}


#include<bits\stdc++.h>
using namespace std;
using ull=long long;
ull n;
ull mod=1e9+7;
void dfs(int s,vector<ull> graph[],vector<bool> &visited,vector<ull> &count,vector<ull> &prod)
{
    visited[s]=1;
    for(int v:graph[s])
    {
        if(!visited[v])
        {
            dfs(v,graph,visited,count,prod);
            count[s]+=count[v];
            count[s]%=mod;
            prod.push_back((count[v]*(n-count[v]))%mod);
        }
    }
    count[s]+=1;
    count[s]%=mod;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n;
        vector<ull> graph[n+1];
        for(int i=0;i<n-1;i++)
        {
            ull a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }
        vector<ull> count(n+1,0),prod;
        vector<bool> visited(n+1,false);
        dfs(1,graph,visited,count,prod);
        sort(prod.begin(),prod.end());
        ull m;
        cin>>m;
        vector<ull> factor(m);
        for(int i=0;i<m;i++)
        {
            cin>>factor[i];
        }
        if(m>n-1)
        {
            sort(factor.begin(),factor.end());
            while(factor.size()!=n-1)
            {
                factor[factor.size()-2]*=factor[factor.size()-1];
                factor[factor.size()-1]%=mod;
                factor.pop_back();
            }
        }
        else
        {
            while(factor.size()!=n-1)
            {
                factor.push_back(1);
            }
            sort(factor.begin(),factor.end());
        }
        for(int i=0;i<n-1;i++)
        {
            factor[i]%=mod;
            prod[i]%=mod;
        }
        ull ans=0;
        for(int i=0;i<n-1;i++)
        {
            ans+=(factor[i]*prod[i]);
            ans%=mod;
        }
        ans%=mod;
        cout<<ans<<endl;
    }
}


#include<bits\stdc++.h>
using namespace std;
using ll=long long;
ll n;
ll mod=1e9+7;
void dfs(int s,vector<ll> graph[],vector<bool> &visited,vector<ll> &count,vector<ll> &prod)
{
    visited[s]=1;
    for(int v:graph[s])
    {
        if(!visited[v])
        {
            dfs(v,graph,visited,count,prod);
            count[s]+=count[v];
            count[s]%=mod;
            prod.push_back((count[v]*(n-count[v]))%mod);
        }
    }
    count[s]+=1LL;
    count[s]%=mod;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n;
        vector<ll> graph[n+1];
        for(int i=0;i<n-1;i++)
        {
            ll a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }
        vector<ll> count(n+1,0),prod;
        vector<bool> visited(n+1,false);
        dfs(1,graph,visited,count,prod);
        sort(prod.begin(),prod.end());
        ll m;
        cin>>m;
        vector<ll> factor(m);
        for(int i=0;i<m;i++)
        {
            cin>>factor[i];
            factor[i]%=mod;
        }
        if(m>n-1)
        {
            sort(factor.begin(),factor.end());
            while(factor.size()!=n-1)
            {
                factor[factor.size()-2]*=factor[factor.size()-1];
                factor[factor.size()-2]%=mod;
                factor.pop_back();
            }
        }
        else
        {
            while(factor.size()!=n-1)
            {
                factor.push_back(1LL);
            }
            sort(factor.begin(),factor.end());
        }
        for(int i=0;i<n-1;i++)
        {
            factor[i]%=mod;
            prod[i]%=mod;
        }
        ll ans=0;
        for(int i=0;i<n-1;i++)
        {
            ll temp=(factor[i]*prod[i]);
            temp%=mod;
            ans+=temp;
            ans%=mod;
        }
        ans%=mod;
        cout<<ans<<endl;
    }
}


#include<bits\stdc++.h>
using namespace std;
long long int num=10e8+7;
long long int dfs(int s,vector<int> graph[],vector<bool> &visited,vector<long long int> &node_count,long long int n)
{
    long long int count=1;
    visited[s]=1;
    for(int v:graph[s])
    {
        if(!visited[v])
        {
            long long int count1=dfs(v,graph,visited,node_count,n);
            node_count.push_back(count1*(n-count1));
            count+=count1;
            count%=num;
        }
    }
    return count;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        long long int n;
        cin>>n;
        vector<int> graph[n+1];
        for(int i=0;i<n-1;i++)
        {
            int a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }
        vector<long long int> count;
        vector<bool> visited(n+1,false);
        dfs(1,graph,visited,count,n);
        int m;
        cin>>m;
        vector<long long int> factors(m);
        for(int i=0;i<m;i++)
        {
            cin>>factors[i];
        }
        while(factors.size()<n-1)
        {
            factors.push_back(1);
        }
        sort(factors.begin(),factors.end());
        long long int ans=0;
        sort(count.begin(),count.end());
        for(int i=0;i<n-1;i++)
        {
            ans+=(count[i]*factors[i]);
            ans%=num;
        }
        cout<<ans<<endl;
    }
}


#include<bits\stdc++.h>
using namespace std;
using ll=long long;
ll n;
ll mod=1e9+7;
void dfs(int s,vector<ll> graph[],vector<bool> &visited,vector<ll> &count,vector<ll> &prod)
{
    visited[s]=1;
    for(int v:graph[s])
    {
        if(!visited[v])
        {
            dfs(v,graph,visited,count,prod);
            count[s]+=count[v];
            count[s]%=mod;
            prod.push_back((count[v]*(n-count[v]))%mod);
        }
    }
    count[s]+=1LL;
    count[s]%=mod;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n;
        vector<ll> graph[n+1];
        for(int i=0;i<n-1;i++)
        {
            ll a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }
        vector<ll> count(n+1,0),prod;
        vector<bool> visited(n+1,false);
        dfs(1,graph,visited,count,prod);
        sort(prod.begin(),prod.end());
        ll m;
        cin>>m;
        vector<ll> factor(m);
        for(int i=0;i<m;i++)
        {
            cin>>factor[i];
            factor[i]%=mod;
        }
        if(m>n-1)
        {
            sort(factor.begin(),factor.end());
            while(factor.size()!=n-1)
            {
                factor[factor.size()-2]*=factor[factor.size()-1];
                factor[factor.size()-2]%=mod;
                factor.pop_back();
            }
        }
        else
        {
            while(factor.size()!=n-1)
            {
                factor.push_back(1LL);
            }
            sort(factor.begin(),factor.end());
        }
        for(int i=0;i<n-1;i++)
        {
            factor[i]%=mod;
            prod[i]%=mod;
        }
        ll ans=0;
        for(int i=0;i<n-1;i++)
        {
            ans+=(factor[i]*prod[i]);
            ans%=mod;
        }
        ans%=mod;
        cout<<ans<<endl;
    }
}


#include<bits\stdc++.h>
using namespace std;
long long int num=10e8+7;
long long int dfs(long long int s,vector<long long int> graph[],vector<bool> &visited,vector<long long int> &node_count,long long int n)
{
    long long int count=1;
    visited[s]=1;
    for(long long int v:graph[s])
    {
        if(!visited[v])
        {
            long long int count1=dfs(v,graph,visited,node_count,n);
            node_count.push_back(count1*(n-count1));
            count+=count1;
        }
    }
    return count;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        long long int n;
        cin>>n;
        vector<long long int> graph[n+1];
        for(int i=0;i<n-1;i++)
        {
            long long int a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }
        vector<long long int> count;
        vector<bool> visited(n+1,false);
        dfs(1,graph,visited,count,n);
        int m;
        cin>>m;
        vector<long long int> factors(m);
        for(int i=0;i<m;i++)
        {
            cin>>factors[i];
        }
        while(factors.size()<n-1)
        {
            factors.push_back(1);
        }
        sort(factors.begin(),factors.end());
        long long int ans=0;
        sort(count.begin(),count.end());
        for(int i=0;i<n-1;i++)
        {
            ans+=(count[i]*factors[i]);
            ans%=num;
        }
        cout<<ans<<endl;
    }
}


#include<bits\stdc++.h>
using namespace std;
long long int num=10e8+7;
long long int dfs(int s,vector<int> graph[],vector<bool> &visited,vector<long long int> &node_count,int n)
{
    long long int count=1;
    visited[s]=1;
    for(int v:graph[s])
    {
        if(!visited[v])
        {
            long long int count1=dfs(v,graph,visited,node_count,n);
            node_count.push_back(count1*(n-count1));
            count+=count1;
            count%=num;
        }
    }
    return count;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        vector<int> graph[n+1];
        for(int i=0;i<n-1;i++)
        {
            int a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }
        vector<long long int> count;
        vector<bool> visited(n+1,false);
        dfs(1,graph,visited,count,n);
        int m;
        cin>>m;
        vector<long long int> factors(m);
        for(int i=0;i<m;i++)
        {
            cin>>factors[i];
        }
        while(factors.size()<n-1)
        {
            factors.push_back(1);
        }
        sort(factors.begin(),factors.end());
        long long int ans=0;
        sort(count.begin(),count.end());
        for(int i=0;i<n-1;i++)
        {
            ans+=(count[i]*factors[i]);
            ans%=num;
        }
        cout<<ans<<endl;
    }
}


#include<bits\stdc++.h>
using namespace std;
using ll=long long;
ll n;
ll mod=1e9+7;
void dfs(int s,vector<ll> graph[],vector<bool> &visited,vector<ll> &count,vector<ll> &prod)
{
    visited[s]=1;
    for(int v:graph[s])
    {
        if(!visited[v])
        {
            dfs(v,graph,visited,count,prod);
            count[s]+=count[v];
            count[s]%=mod;
            prod.push_back((count[v]*(n-count[v]))%mod);
        }
    }
    count[s]+=1;
    count[s]%=mod;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n;
        vector<ll> graph[n+1];
        for(int i=0;i<n-1;i++)
        {
            ll a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }
        vector<ll> count(n+1,0),prod;
        vector<bool> visited(n+1,false);
        dfs(1,graph,visited,count,prod);
        sort(prod.begin(),prod.end());
        ll m;
        cin>>m;
        vector<ll> factor(m);
        for(int i=0;i<m;i++)
        {
            cin>>factor[i];
        }
        if(m>n-1)
        {
            sort(factor.begin(),factor.end());
            while(factor.size()!=n-1)
            {
                factor[factor.size()-2]*=factor.back();
                factor[factor.size()-1]%=mod;
                factor.pop_back();
            }
        }
        else
        {
            while(factor.size()!=n-1)
            {
                factor.push_back(1);
            }
            sort(factor.begin(),factor.end());
        }
        for(int i=0;i<n-1;i++)
        {
            factor[i]%=mod;
            prod[i]%=mod;
        }
        ll ans=0;
        for(int i=0;i<n-1;i++)
        {
            ans+=(factor[i]*prod[i]);
            ans%=mod;
        }
        ans%=mod;
        cout<<ans<<endl;
    }
}


#include<bits\stdc++.h>
using namespace std;
using ull=long long;
ull n;
ull mod=1e9+7;
void dfs(int s,vector<ull> graph[],vector<bool> &visited,vector<ull> &count,vector<ull> &prod)
{
    visited[s]=1;
    for(int v:graph[s])
    {
        if(!visited[v])
        {
            dfs(v,graph,visited,count,prod);
            count[s]+=count[v];
            count[s]%=mod;
            prod.push_back((count[v]*(n-count[v]))%mod);
        }
    }
    count[s]+=1;
    count[s]%=mod;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n;
        vector<ull> graph[n+1];
        for(int i=0;i<n-1;i++)
        {
            ull a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }
        vector<ull> count(n+1,0),prod;
        vector<bool> visited(n+1,false);
        dfs(1,graph,visited,count,prod);
        sort(prod.begin(),prod.end());
        ull m;
        cin>>m;
        vector<ull> factor(m);
        for(int i=0;i<m;i++)
        {
            cin>>factor[i];
            factor[i]%=mod;
        }
        if(m>n-1)
        {
            sort(factor.begin(),factor.end());
            while(factor.size()!=n-1)
            {
                factor[factor.size()-2]*=factor[factor.size()-1];
                factor[factor.size()-1]%=mod;
                factor.pop_back();
            }
        }
        else
        {
            while(factor.size()!=n-1)
            {
                factor.push_back(1LL);
            }
            sort(factor.begin(),factor.end());
        }
        for(int i=0;i<n-1;i++)
        {
            factor[i]%=mod;
            prod[i]%=mod;
        }
        ull ans=0;
        for(int i=0;i<n-1;i++)
        {
            ans+=(factor[i]*prod[i]);
            ans%=mod;
        }
        ans%=mod;
        cout<<ans<<endl;
    }
}


#include<bits\stdc++.h>
using namespace std;
using ull=long long;
ull n;
ull mod=1e9+7;
void dfs(int s,vector<ull> graph[],vector<bool> &visited,vector<ull> &count,vector<ull> &prod)
{
    visited[s]=1;
    for(int v:graph[s])
    {
        if(!visited[v])
        {
            dfs(v,graph,visited,count,prod);
            count[s]+=count[v];
            count[s]%=mod;
            prod.push_back((count[v]*(n-count[v]))%mod);
        }
    }
    count[s]+=1;
    count[s]%=mod;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n;
        vector<ull> graph[n+1];
        for(int i=0;i<n-1;i++)
        {
            ull a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }
        vector<ull> count(n+1,0),prod;
        vector<bool> visited(n+1,false);
        dfs(1,graph,visited,count,prod);
        sort(prod.begin(),prod.end());
        ull m;
        cin>>m;
        vector<ull> factor(m);
        for(int i=0;i<m;i++)
        {
            cin>>factor[i];
        }
        if(m>n-1)
        {
            sort(factor.begin(),factor.end());
            while(factor.size()!=n-1)
            {
                factor[factor.size()-2]*=factor.back();
                factor[factor.size()-1]%=mod;
                factor.pop_back();
            }
        }
        else
        {
            while(factor.size()!=n-1)
            {
                factor.push_back(1);
            }
            sort(factor.begin(),factor.end());
        }
        for(int i=0;i<n-1;i++)
        {
            factor[i]%=mod;
            prod[i]%=mod;
        }
        ull ans=0;
        for(int i=0;i<n-1;i++)
        {
            ans+=(factor[i]*prod[i]);
            ans%=mod;
        }
        ans%=mod;
        cout<<ans<<endl;
    }
}


#include<bits\stdc++.h>
using namespace std;
long long int num=1000000007;
long long int dfs(long long int s,vector<long long int> graph[],vector<bool> &visited,vector<long long int> &node_count,long long int n)
{
    long long int count=1;
    visited[s]=1;
    for(int v:graph[s])
    {
        if(!visited[v])
        {
            long long int count1=dfs(v,graph,visited,node_count,n);
            node_count.push_back(count1*(n-count1));
            count+=count1;
        }
    }
    return count;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        long long int n;
        cin>>n;
        vector<long long int> graph[n+1];
        for(int i=0;i<n-1;i++)
        {
            long long int a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }
        vector<long long int> count;
        vector<bool> visited(n+1,false);
        dfs(1,graph,visited,count,n);
        long long int m;
        cin>>m;
        vector<long long int> factors(m);
        for(int i=0;i<m;i++)
        {
            cin>>factors[i];
        }
        while(factors.size()!=n-1)
        {
            factors.push_back(1);
        }
        sort(factors.begin(),factors.end());
        long long int ans=0;
        sort(count.begin(),count.end());
        for(int i=0;i<n-1;i++)
        {
            ans+=(count[i]*factors[i]);
            ans%=num;
        }
        // for(int i=0;i<count.size();i++)
        // {
        //     cout<<count[i]<<" ";
        // }
        cout<<ans<<endl;
    }

}


#include<bits\stdc++.h>
using namespace std;
long long int num=10e9+7;
long long int dfs(long long int s,vector<long long int> graph[],vector<bool> &visited,vector<long long int> &node_count,long long int n)
{
    long long int count=1;
    visited[s]=1;
    for(long long int v:graph[s])
    {
        if(!visited[v])
        {
            long long int count1=dfs(v,graph,visited,node_count,n);
            node_count.push_back(count1*(n-count1));
            count+=count1;
            count%=num;
        }
    }
    return count;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        long long int n;
        cin>>n;
        vector<long long int> graph[n+1];
        for(int i=0;i<n-1;i++)
        {
            long long int a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }
        vector<long long int> count;
        vector<bool> visited(n+1,false);
        dfs(1,graph,visited,count,n);
        int m;
        cin>>m;
        vector<long long int> factors(m);
        for(int i=0;i<m;i++)
        {
            cin>>factors[i];
        }
        while(factors.size()<n-1)
        {
            factors.push_back(1);
        }
        sort(factors.begin(),factors.end());
        long long int ans=0;
        sort(count.begin(),count.end());
        for(int i=0;i<n-1;i++)
        {
            ans+=(count[i]*factors[i]);
            ans%=num;
        }
        cout<<ans<<endl;
    }
}


#include<bits\stdc++.h>
using namespace std;
using ll=long long;
ll n;
ll mod=1e9+7;
void dfs(int s,vector<ll> graph[],vector<bool> &visited,vector<ll> &count,vector<ll> &prod)
{
    visited[s]=1;
    for(int v:graph[s])
    {
        if(!visited[v])
        {
            dfs(v,graph,visited,count,prod);
            count[s]+=count[v];
            count[s]%=mod;
            prod.push_back((count[v]*(n-count[v]))%mod);
        }
    }
    count[s]+=1;
    count[s]%=mod;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n;
        vector<ll> graph[n+1];
        for(int i=0;i<n-1;i++)
        {
            ll a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }
        vector<ll> count(n+1,0),prod;
        vector<bool> visited(n+1,false);
        dfs(1,graph,visited,count,prod);
        sort(prod.begin(),prod.end());
        ll m;
        cin>>m;
        vector<ll> factor(m);
        for(int i=0;i<m;i++)
        {
            cin>>factor[i];
        }
        if(m>n-1)
        {
            sort(factor.begin(),factor.end());
            while(factor.size()!=n-1)
            {
                factor[factor.size()-2]*=factor.back();
                factor[factor.size()-1]%=mod;
                factor.pop_back();
            }
        }
        else
        {
            while(factor.size()!=n-1)
            {
                factor.push_back(1);
            }
            sort(factor.begin(),factor.end());
        }
        ll ans=0;
        for(int i=0;i<n-1;i++)
        {
            ans+=(factor[i]*prod[i]);
            ans%=mod;
        }
        ans%=mod;
        cout<<ans<<endl;
    }
}


#include<bits\stdc++.h>
using namespace std;
long long int num=10e8+7;
long long int dfs(int s,vector<int> graph[],vector<bool> &visited,vector<long long int> &node_count,int n)
{
    long long int count=1;
    visited[s]=1;
    for(int v:graph[s])
    {
        if(!visited[v])
        {
            long long int count1=dfs(v,graph,visited,node_count,n);
            node_count.push_back(count1*(n-count1));
            count+=count1;
            count%=num;
        }
    }
    return count;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        vector<int> graph[n+1];
        for(int i=0;i<n-1;i++)
        {
            int a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }
        vector<long long int> count;
        vector<bool> visited(n+1,false);
        dfs(1,graph,visited,count,n);
        int m;
        cin>>m;
        vector<long long int> factors(m);
        for(int i=0;i<m;i++)
        {
            cin>>factors[i];
        }
        while(factors.size()!=n-1)
        {
            factors.push_back(1);
        }
        sort(factors.begin(),factors.end());
        long long int ans=0;
        sort(count.begin(),count.end());
        for(int i=0;i<n-1;i++)
        {
            ans+=(count[i]*factors[i]);
            ans%=num;
        }
        cout<<ans<<endl;
    }
}


#include<bits\stdc++.h>
using namespace std;
long long int num=10e9+7;
long long int dfs(int s,vector<int> graph[],vector<bool> &visited,vector<long long int> &node_count,long long int n)
{
    long long int count=1;
    visited[s]=1;
    for(int v:graph[s])
    {
        if(!visited[v])
        {
            long long int count1=dfs(v,graph,visited,node_count,n);
            node_count.push_back(count1*(n-count1));
            count+=count1;
            count%=num;
        }
    }
    return count;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        long long int n;
        cin>>n;
        vector<int> graph[n+1];
        for(int i=0;i<n-1;i++)
        {
            int a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }
        vector<long long int> count;
        vector<bool> visited(n+1,false);
        dfs(1,graph,visited,count,n);
        int m;
        cin>>m;
        vector<long long int> factors(m);
        for(int i=0;i<m;i++)
        {
            cin>>factors[i];
        }
        while(factors.size()<n-1)
        {
            factors.push_back(1);
        }
        sort(factors.begin(),factors.end());
        long long int ans=0;
        sort(count.begin(),count.end());
        for(int i=0;i<n-1;i++)
        {
            ans+=(count[i]*factors[i]);
            ans%=num;
        }
        cout<<ans<<endl;
    }
}


#include<bits\stdc++.h>
using namespace std;
long long int num=10e8+7;
long long int dfs(long long int s,vector<long long int> graph[],vector<bool> &visited,vector<long long int> &node_count,long long int n)
{
    long long int count=1;
    visited[s]=1;
    for(long long int v:graph[s])
    {
        if(!visited[v])
        {
            long long int count1=dfs(v,graph,visited,node_count,n);
            node_count.push_back(count1*(n-count1));
            count+=count1;
        }
    }
    return count;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        long long int n;
        cin>>n;
        vector<long long int> graph[n+1];
        for(int i=0;i<n-1;i++)
        {
            long long int a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }
        vector<long long int> count;
        vector<bool> visited(n+1,false);
        dfs(1,graph,visited,count,n);
        int m;
        cin>>m;
        vector<long long int> factors(m);
        for(int i=0;i<m;i++)
        {
            cin>>factors[i];
        }
        while(factors.size()<n-1)
        {
            factors.push_back(1);
        }
        sort(factors.begin(),factors.end());
        long long int ans=0;
        sort(count.begin(),count.end());
        for(int i=0;i<n-1;i++)
        {
            count[i]%=num;
        }
        for(int i=0;i<n-1;i++)
        {
            ans+=(count[i]*factors[i]);
            ans%=num;
        }
        cout<<ans<<endl;
    }
}


#include<bits\stdc++.h>
using namespace std;
using ull=long long;
ull n;
ull mod=1e9+7;
void dfs(int s,vector<ull> graph[],vector<bool> &visited,vector<ull> &count,vector<ull> &prod)
{
    visited[s]=1;
    for(int v:graph[s])
    {
        if(!visited[v])
        {
            dfs(v,graph,visited,count,prod);
            count[s]+=count[v];
            count[s]%=mod;
            prod.push_back((count[v]*(n-count[v]))%mod);
        }
    }
    count[s]+=1;
    count[s]%=mod;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n;
        vector<ull> graph[n+1];
        for(int i=0;i<n-1;i++)
        {
            ull a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }
        vector<ull> count(n+1,0),prod;
        vector<bool> visited(n+1,false);
        dfs(1,graph,visited,count,prod);
        sort(prod.begin(),prod.end());
        ull m;
        cin>>m;
        vector<ull> factor(m);
        for(int i=0;i<m;i++)
        {
            cin>>factor[i];
            factor[i]%=mod;
        }
        if(m>n-1)
        {
            sort(factor.begin(),factor.end());
            while(factor.size()!=n-1)
            {
                factor[factor.size()-2]*=factor[factor.size()-1];
                factor[factor.size()-2]%=mod;
                factor.pop_back();
            }
        }
        else
        {
            while(factor.size()!=n-1)
            {
                factor.push_back(1LL);
            }
            sort(factor.begin(),factor.end());
        }
        for(int i=0;i<n-1;i++)
        {
            factor[i]%=mod;
            prod[i]%=mod;
        }
        ull ans=0;
        for(int i=0;i<n-1;i++)
        {
            ans+=(factor[i]*prod[i]);
            ans%=mod;
        }
        ans%=mod;
        cout<<ans<<endl;
    }
}


#include<bits\stdc++.h>
using namespace std;
long long int num=10e8+7;
long long int dfs(long long int s,vector<int> graph[],vector<bool> &visited,vector<long long int> &node_count,int n)
{
    long long int count=1;
    visited[s]=1;
    for(int v:graph[s])
    {
        if(!visited[v])
        {
            long long int count1=dfs(v,graph,visited,node_count,n);
            node_count.push_back(count1*(n-count1));
            count+=count1;
            count%=num;
        }
    }
    return count;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        long long int n;
        cin>>n;
        vector<int> graph[n+1];
        for(int i=0;i<n-1;i++)
        {
            int a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }
        vector<long long int> count;
        vector<bool> visited(n+1,false);
        dfs(1,graph,visited,count,n);
        int m;
        cin>>m;
        vector<long long int> factors(m);
        for(int i=0;i<m;i++)
        {
            cin>>factors[i];
        }
        while(factors.size()!=n-1)
        {
            factors.push_back(1);
        }
        sort(factors.begin(),factors.end());
        long long int ans=0;
        sort(count.begin(),count.end());
        for(int i=0;i<n-1;i++)
        {
            ans+=(count[i]*factors[i]);
            ans%=num;
        }

        cout<<ans<<endl;
    }
}


#include<bits\stdc++.h>
using namespace std;
using ull=long long;
ull n;
ull mod=1e9+7;
void dfs(int s,vector<ull> graph[],vector<bool> &visited,vector<ull> &count,vector<ull> &prod)
{
    visited[s]=1;
    for(int v:graph[s])
    {
        if(!visited[v])
        {
            dfs(v,graph,visited,count,prod);
            count[s]+=count[v];
            count[s]%=mod;
            prod.push_back((count[v]*(n-count[v]))%mod);
        }
    }
    count[s]+=1;
    count[s]%=mod;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n;
        vector<ull> graph[n+1];
        for(int i=0;i<n-1;i++)
        {
            ull a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }
        vector<ull> count(n+1,0),prod;
        vector<bool> visited(n+1,false);
        dfs(1,graph,visited,count,prod);
        sort(prod.begin(),prod.end());
        ull m;
        cin>>m;
        vector<ull> factor(m);
        for(int i=0;i<m;i++)
        {
            cin>>factor[i];
            factor[i]%=mod;
        }
        if(m>n-1)
        {
            sort(factor.begin(),factor.end());
            while(factor.size()!=n-1)
            {
                factor[factor.size()-2]*=factor[factor.size()-1];
                factor[factor.size()-1]%=mod;
                factor.pop_back();
            }
        }
        else
        {
            while(factor.size()!=n-1)
            {
                factor.push_back(1);
            }
            sort(factor.begin(),factor.end());
        }
        for(int i=0;i<n-1;i++)
        {
            factor[i]%=mod;
            prod[i]%=mod;
        }
        ull ans=0;
        for(int i=0;i<n-1;i++)
        {
            ans+=(factor[i]*prod[i]);
            ans%=mod;
        }
        ans%=mod;
        cout<<ans<<endl;
    }
}