#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
typedef long long ll;
typedef pair<int,int> pi;
int n,m;
vector<pi> adj[100005];
bool visited[100005];
int p[6*10000 +5];
const int MOD = 1e9 +7;
ll w[200040]; // w[i] 는 i번쨰 간선의 좌우 노드들의 곱
// 간선의 번호는 1번 노드의 가장 낮은 번호로 가는 간선이 1번
// n-1 번 노드의 가장 높은 번호로 가는 간선이 n-1 번

// dfs(v) 는 v 노드의 사이즈를 반환
int dfs(int v){

    visited[v] = true;
    int cnt = 1;
    rep(i,0,(int)adj[v].size()-1){
        if(!visited[adj[v][i].first]){
            int tmp = dfs(adj[v][i].first);

            w[adj[v][i].second] = (((long long)tmp)*(n-tmp))%MOD;

            cnt += tmp;
        }
    }

    return cnt;
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);

        rep(i,1,n){
            adj[i].clear();
            visited[i] = false;
        }

        rep(i,1,n-1){
            int a,b;
            scanf("%d %d",&a,&b);
            adj[a].push_back({b,0});
            adj[b].push_back({a,0});
        }

        rep(i,1,n) sort(adj[i].begin(), adj[i].end());

        int cnt = 1;
        rep(i,1,n)
            rep(j,0,(int)adj[i].size() -1)
                adj[i][j].second = cnt++;

        memset(p,0,sizeof(p));
        scanf("%d",&m);
        rep(i,1,m) scanf("%d",&p[i]);

        memset(w,0,sizeof(w));

        dfs(1);

        sort(w+1, w+ 20040);
        reverse(w+1, w+ 20040);

        sort(p+1, p+m+1);
        reverse(p+1,p+m+1);

        ll ans = 0;

        if(m <= n-1){
            rep(i,1,n-1){
                ll pt = 1ll;
                if(i<=m) pt = p[i];
                ans = (ans + ((ll)(w[i]%MOD)*pt%MOD)%MOD)%MOD;
            }
            printf("%lld\n", ans);
            continue;
        }
        // 남는 소수 개수 : m - n + 1
        ll pt = 1;
        rep(i,1,m-n+2){
            pt = (pt * (ll)p[i])%MOD;
        }
        ans = ((w[1]%MOD)*(pt%MOD))%MOD;

        rep(i,2,n-1){
            ans = (ans + ((ll)(w[i]%MOD)*(p[i + m-n +1]%MOD))%MOD)%MOD;
        }
        printf("%lld\n", ans);

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
typedef long long ll;
typedef pair<int,int> pi;
int n,m;
vector<pi> adj[100001];
bool visited[100001];
int p[6*10000 +1];
const int MOD = 1e9 +7;
ll w[200040]; // w[i] 는 i번쨰 간선의 좌우 노드들의 곱
// 간선의 번호는 1번 노드의 가장 낮은 번호로 가는 간선이 1번
// n-1 번 노드의 가장 높은 번호로 가는 간선이 n-1 번

// dfs(v) 는 v 노드의 사이즈를 반환
int dfs(int v){

    visited[v] = true;
    int cnt = 1;
    rep(i,0,(int)adj[v].size()-1){
        if(!visited[adj[v][i].first]){
            int tmp = dfs(adj[v][i].first);

            w[adj[v][i].second] = ((long long)tmp)*(n-tmp);

            cnt += tmp;
        }
    }

    return cnt;
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);

        rep(i,1,n){
            adj[i].clear();
            visited[i] = false;
        }

        rep(i,1,n-1){
            int a,b;
            scanf("%d %d",&a,&b);
            adj[a].push_back({b,0});
            adj[b].push_back({a,0});
        }

        rep(i,1,n) sort(adj[i].begin(), adj[i].end());

        int cnt = 1;
        rep(i,1,n)
            rep(j,0,(int)adj[i].size() -1)
                adj[i][j].second = cnt++;

        scanf("%d",&m);
        rep(i,1,m) scanf("%d",&p[i]);

        memset(w,0,sizeof(w));
        dfs(1);

        sort(w+1, w+ 20040);
        reverse(w+1, w+ 20040);

        /* rep(i,1,cnt-1){
            printf("%d : %d\n", i, w[i]);
        } */

        sort(p+1, p+m+1);

        ll ans = 0;

        if(m <= n-1){
            rep(i,1,n-1){
                int pt = 1;
                if(i<=m) pt = p[i];
                ans = (ans + ((ll)(w[i])*pt)%MOD)%MOD;
            }
            printf("%lld\n", ans);
            continue;
        }

        // 남는 소수 개수 : m - n + 1
        ll pt = 1;
        rep(i,1,m-n+1){
            pt = (pt * (ll)p[i])%MOD;
        }
        ans = w[1]*pt;

        rep(i,m-n+2,m){
            ans = (ans + ((ll)(w[i])*p[i])%MOD)%MOD;
        }
        printf("%lld\n", ans);

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
typedef long long ll;
typedef pair<int,int> pi;
int n,m;
vector<pi> adj[100005];
bool visited[100005];
int p[6*10000 +5];
const int MOD = 1e9 +7;
ll w[200040]; // w[i] 는 i번쨰 간선의 좌우 노드들의 곱
// 간선의 번호는 1번 노드의 가장 낮은 번호로 가는 간선이 1번
// n-1 번 노드의 가장 높은 번호로 가는 간선이 n-1 번

// dfs(v) 는 v 노드의 사이즈를 반환
int dfs(int v){

    visited[v] = true;
    int cnt = 1;
    rep(i,0,(int)adj[v].size()-1){
        if(!visited[adj[v][i].first]){
            int tmp = dfs(adj[v][i].first);

            w[adj[v][i].second] = ((long long)tmp)*(n-tmp);

            cnt += tmp;
        }
    }

    return cnt;
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);

        rep(i,1,n){
            adj[i].clear();
            visited[i] = false;
        }

        rep(i,1,n-1){
            int a,b;
            scanf("%d %d",&a,&b);
            adj[a].push_back({b,0});
            adj[b].push_back({a,0});
        }

        rep(i,1,n) sort(adj[i].begin(), adj[i].end());

        int cnt = 1;
        rep(i,1,n)
            rep(j,0,(int)adj[i].size() -1)
                adj[i][j].second = cnt++;

        memset(p,0,sizeof(p));
        scanf("%d",&m);
        rep(i,1,m) scanf("%d",&p[i]);

        memset(w,0,sizeof(w));

        dfs(1);

        sort(w+1, w+ 20040);
        reverse(w+1, w+ 20040);

        sort(p+1, p+m+1);
        reverse(p+1,p+m+1);

        ll ans = 0;

        if(m <= n-1){
            rep(i,1,n-1){
                ll pt = 1ll;
                if(i<=m) pt = p[i];
                ans = (ans + ((ll)(w[i]%MOD)*(pt%MOD))%MOD)%MOD;
            }
            printf("%lld\n", ans);
            continue;
        }
        // 남는 소수 개수 : m - n + 1
        ll pt = 1;
        rep(i,1,m-n+2){
            pt = (pt * (ll)p[i])%MOD;
        }
        ans = ((w[1]%MOD)*(pt%MOD))%MOD;

        rep(i,2,n-1){
            ans = (ans + ((ll)(w[i]%MOD)*(p[i + m-n +1]%MOD))%MOD)%MOD;
        }
        printf("%lld\n", ans);

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
typedef long long ll;
typedef pair<int,int> pi;
int n,m;
vector<pi> adj[100005];
bool visited[100005];
int p[6*10000 +5];
const int MOD = 1e9 +7;
ll w[200040]; // w[i] 는 i번쨰 간선의 좌우 노드들의 곱
// 간선의 번호는 1번 노드의 가장 낮은 번호로 가는 간선이 1번
// n-1 번 노드의 가장 높은 번호로 가는 간선이 n-1 번

// dfs(v) 는 v 노드의 사이즈를 반환
int dfs(int v){

    visited[v] = true;
    int cnt = 1;
    rep(i,0,(int)adj[v].size()-1){
        if(!visited[adj[v][i].first]){
            int tmp = dfs(adj[v][i].first);

            w[adj[v][i].second] = ((long long)tmp)*(n-tmp);

            cnt += tmp;
        }
    }

    return cnt;
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);

        rep(i,1,n){
            adj[i].clear();
            visited[i] = false;
        }

        rep(i,1,n-1){
            int a,b;
            scanf("%d %d",&a,&b);
            adj[a].push_back({b,0});
            adj[b].push_back({a,0});
        }

        rep(i,1,n) sort(adj[i].begin(), adj[i].end());

        int cnt = 1;
        rep(i,1,n)
            rep(j,0,(int)adj[i].size() -1)
                adj[i][j].second = cnt++;

        memset(p,0,sizeof(p));
        scanf("%d",&m);
        rep(i,1,m) scanf("%d",&p[i]);

        memset(w,0,sizeof(w));

        dfs(1);

        sort(w+1, w+ 20040);
        reverse(w+1, w+ 20040);

        sort(p+1, p+m+1);
        reverse(p+1,p+m+1);

        ll ans = 0;

        if(m <= n-1){
            rep(i,1,n-1){
                ll pt = 1ll;
                if(i<=m) pt = p[i];
                ans = (ans + ((ll)(w[i]%MOD)*pt%MOD)%MOD)%MOD;
            }
            printf("%lld\n", ans);
            continue;
        }
        // 남는 소수 개수 : m - n + 1
        ll pt = 1;
        rep(i,1,m-n+2){
            pt = (pt * (ll)p[i])%MOD;
        }
        ans = ((w[1]%MOD)*(pt%MOD))%MOD;

        rep(i,2,n-1){
            ans = (ans + ((ll)(w[i]%MOD)*(p[i + m-n +1]%MOD))%MOD)%MOD;
        }
        printf("%lld\n", ans);

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
typedef long long ll;
typedef pair<int,int> pi;
int n,m;
vector<pi> adj[100005];
bool visited[100005];
int p[6*10000 +5];
const int MOD = 1e9 +7;
ll w[200040]; // w[i] 는 i번쨰 간선의 좌우 노드들의 곱
// 간선의 번호는 1번 노드의 가장 낮은 번호로 가는 간선이 1번
// n-1 번 노드의 가장 높은 번호로 가는 간선이 n-1 번

// dfs(v) 는 v 노드의 사이즈를 반환
int dfs(int v){

    visited[v] = true;
    int cnt = 1;
    rep(i,0,(int)adj[v].size()-1){
        if(!visited[adj[v][i].first]){
            int tmp = dfs(adj[v][i].first);

            w[adj[v][i].second] = ((long long)tmp)*(n-tmp);

            cnt += tmp;
        }
    }

    return cnt;
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);

        rep(i,1,n){
            adj[i].clear();
            visited[i] = false;
        }

        rep(i,1,n-1){
            int a,b;
            scanf("%d %d",&a,&b);
            adj[a].push_back({b,0});
            adj[b].push_back({a,0});
        }

        rep(i,1,n) sort(adj[i].begin(), adj[i].end());

        int cnt = 1;
        rep(i,1,n)
            rep(j,0,(int)adj[i].size() -1)
                adj[i][j].second = cnt++;

        memset(p,0,sizeof(p));
        scanf("%d",&m);
        rep(i,1,m) scanf("%d",&p[i]);

        memset(w,0,sizeof(w));

        dfs(1);

        sort(w+1, w+ 200040);
        reverse(w+1, w+ 200040);

        sort(p+1, p+m+1);
        reverse(p+1,p+m+1);

        ll ans = 0;

        if(m <= n-1){
            rep(i,1,n-1){
                ll pt = 1ll;
                if(i<=m) pt = p[i];
                ans = (ans + ((ll)(w[i]%MOD)*(pt%MOD))%MOD)%MOD;
            }
            printf("%lld\n", ans);
            continue;
        }
        // 남는 소수 개수 : m - n + 1
        ll pt = 1;
        rep(i,1,m-n+2){
            pt = (pt * (ll)p[i])%MOD;
        }
        ans = ((w[1]%MOD)*(pt%MOD))%MOD;

        rep(i,2,n-1){
            ans = (ans + ((ll)(w[i]%MOD)*(p[i + m-n +1]%MOD))%MOD)%MOD;
        }
        printf("%lld\n", ans);

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
typedef long long ll;
typedef pair<int,int> pi;
int n,m;
vector<pi> adj[100001];
bool visited[100001];
int p[6*10000 +1];
const int MOD = 1e9 +7;
ll w[200040]; // w[i] 는 i번쨰 간선의 좌우 노드들의 곱
// 간선의 번호는 1번 노드의 가장 낮은 번호로 가는 간선이 1번
// n-1 번 노드의 가장 높은 번호로 가는 간선이 n-1 번

// dfs(v) 는 v 노드의 사이즈를 반환
int dfs(int v){

    visited[v] = true;
    int cnt = 1;
    rep(i,0,(int)adj[v].size()-1){
        if(!visited[adj[v][i].first]){
            int tmp = dfs(adj[v][i].first);

            w[adj[v][i].second] = ((long long)tmp)*(n-tmp);

            cnt += tmp;
        }
    }

    return cnt;
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);

        rep(i,1,n){
            adj[i].clear();
            visited[i] = false;
        }

        rep(i,1,n-1){
            int a,b;
            scanf("%d %d",&a,&b);
            adj[a].push_back({b,0});
            adj[b].push_back({a,0});
        }

        rep(i,1,n) sort(adj[i].begin(), adj[i].end());

        int cnt = 1;
        rep(i,1,n)
            rep(j,0,(int)adj[i].size() -1)
                adj[i][j].second = cnt++;

        memset(p,0,sizeof(p));
        scanf("%d",&m);
        rep(i,1,m) scanf("%d",&p[i]);

        memset(w,0,sizeof(w));

        dfs(1);

        sort(w+1, w+ 20040);
        reverse(w+1, w+ 20040);

        /* rep(i,1,cnt-1){
            printf("%d : %d\n", i, w[i]);
        } */

        sort(p+1, p+m+1);
        reverse(p+1,p+m+1);

        ll ans = 0;

        if(m <= n-1){
            rep(i,1,n-1){
                int pt = 1;
                if(i<=m) pt = p[i];
                ans = (ans + ((ll)(w[i])*pt)%MOD)%MOD;
            }
            printf("%lld\n", ans);
            continue;
        }

        // 남는 소수 개수 : m - n + 1
        ll pt = 1;
        rep(i,1,m-n+2){
            pt = (pt * (ll)p[i])%MOD;
        }
        ans = w[1]*pt;

        rep(i,m-n+3,m){
            ans = (ans + ((ll)(w[i])*p[i])%MOD)%MOD;
        }
        printf("%lld\n", ans);

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
typedef long long ll;
typedef pair<int,int> pi;
int n,m;
vector<pi> adj[100001];
bool visited[100001];
int p[6*10000 +1];
const int MOD = 1e9 +7;
ll w[200040]; // w[i] 는 i번쨰 간선의 좌우 노드들의 곱
// 간선의 번호는 1번 노드의 가장 낮은 번호로 가는 간선이 1번
// n-1 번 노드의 가장 높은 번호로 가는 간선이 n-1 번

// dfs(v) 는 v 노드의 사이즈를 반환
int dfs(int v){

    visited[v] = true;
    int cnt = 1;
    rep(i,0,(int)adj[v].size()-1){
        if(!visited[adj[v][i].first]){
            int tmp = dfs(adj[v][i].first);

            w[adj[v][i].second] = ((long long)tmp)*(n-tmp);

            cnt += tmp;
        }
    }

    return cnt;
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);

        rep(i,1,n){
            adj[i].clear();
            visited[i] = false;
        }

        rep(i,1,n-1){
            int a,b;
            scanf("%d %d",&a,&b);
            adj[a].push_back({b,0});
            adj[b].push_back({a,0});
        }

        rep(i,1,n) sort(adj[i].begin(), adj[i].end());

        int cnt = 1;
        rep(i,1,n)
            rep(j,0,(int)adj[i].size() -1)
                adj[i][j].second = cnt++;

        memset(p,0,sizeof(p));
        scanf("%d",&m);
        rep(i,1,m) scanf("%d",&p[i]);

        memset(w,0,sizeof(w));

        dfs(1);

        sort(w+1, w+ 20040);
        reverse(w+1, w+ 20040);

        /* rep(i,1,cnt-1){
            printf("%d : %d\n", i, w[i]);
        } */

        sort(p+1, p+m+1);

        ll ans = 0;

        if(m <= n-1){
            rep(i,1,n-1){
                int pt = 1;
                if(i<=m) pt = p[i];
                ans = (ans + ((ll)(w[i])*pt)%MOD)%MOD;
            }
            printf("%lld\n", ans);
            continue;
        }

        // 남는 소수 개수 : m - n + 1
        ll pt = 1;
        rep(i,1,m-n+1){
            pt = (pt * (ll)p[i])%MOD;
        }
        ans = w[1]*pt;

        rep(i,m-n+2,m){
            ans = (ans + ((ll)(w[i])*p[i])%MOD)%MOD;
        }
        printf("%lld\n", ans);

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
typedef long long ll;
typedef pair<int,int> pi;
int n,m;
vector<pi> adj[100001];
bool visited[100001];
int p[6*10000 +1];
const int MOD = 1e9 +7;
ll w[200040]; // w[i] 는 i번쨰 간선의 좌우 노드들의 곱
// 간선의 번호는 1번 노드의 가장 낮은 번호로 가는 간선이 1번
// n-1 번 노드의 가장 높은 번호로 가는 간선이 n-1 번

// dfs(v) 는 v 노드의 사이즈를 반환
int dfs(int v){

    visited[v] = true;
    int cnt = 1;
    rep(i,0,(int)adj[v].size()-1){
        if(!visited[adj[v][i].first]){
            int tmp = dfs(adj[v][i].first);

            w[adj[v][i].second] = ((long long)tmp)*(n-tmp);

            cnt += tmp;
        }
    }

    return cnt;
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);

        rep(i,1,n){
            adj[i].clear();
            visited[i] = false;
        }

        rep(i,1,n-1){
            int a,b;
            scanf("%d %d",&a,&b);
            adj[a].push_back({b,0});
            adj[b].push_back({a,0});
        }

        rep(i,1,n) sort(adj[i].begin(), adj[i].end());

        int cnt = 1;
        rep(i,1,n)
            rep(j,0,(int)adj[i].size() -1)
                adj[i][j].second = cnt++;

        memset(p,0,sizeof(p));
        scanf("%d",&m);
        rep(i,1,m) scanf("%d",&p[i]);

        memset(w,0,sizeof(w));

        dfs(1);

        sort(w+1, w+ 20040);
        reverse(w+1, w+ 20040);

        sort(p+1, p+m+1);
        reverse(p+1,p+m+1);

        ll ans = 0;

        if(m <= n-1){
            rep(i,1,n-1){
                int pt = 1;
                if(i<=m) pt = p[i];
                ans = (ans + ((ll)(w[i])*pt)%MOD)%MOD;
            }
            printf("%lld\n", ans);
            continue;
        }
        // 남는 소수 개수 : m - n + 1
        ll pt = 1;
        rep(i,1,m-n+2){
            pt = (pt * (ll)p[i])%MOD;
        }
        ans = (w[1]*pt)%MOD;

        rep(i,2,n-1){
            ans = (ans + ((ll)(w[i])*p[i + m-n +1])%MOD)%MOD;
        }
        printf("%lld\n", ans);

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
typedef long long ll;
typedef pair<int,int> pi;
int n,m;
vector<pi> adj[100001];
bool visited[100001];
int p[6*10000 +1];
const int MOD = 1e9 +7;
ll w[200040]; // w[i] 는 i번쨰 간선의 좌우 노드들의 곱
// 간선의 번호는 1번 노드의 가장 낮은 번호로 가는 간선이 1번
// n-1 번 노드의 가장 높은 번호로 가는 간선이 n-1 번

// dfs(v) 는 v 노드의 사이즈를 반환
int dfs(int v){

    visited[v] = true;
    int cnt = 1;
    rep(i,0,(int)adj[v].size()-1){
        if(!visited[adj[v][i].first]){
            int tmp = dfs(adj[v][i].first);

            w[adj[v][i].second] = ((long long)tmp)*(n-tmp);

            cnt += tmp;
        }
    }

    return cnt;
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);

        rep(i,1,n){
            adj[i].clear();
            visited[i] = false;
        }

        rep(i,1,n-1){
            int a,b;
            scanf("%d %d",&a,&b);
            adj[a].push_back({b,0});
            adj[b].push_back({a,0});
        }

        rep(i,1,n) sort(adj[i].begin(), adj[i].end());

        int cnt = 1;
        rep(i,1,n)
            rep(j,0,(int)adj[i].size() -1)
                adj[i][j].second = cnt++;

        scanf("%d",&m);
        rep(i,1,m) scanf("%d",&p[i]);

        memset(w,0,sizeof(w));
        dfs(1);

        sort(w+1, w+ 20040);
        reverse(w+1, w+ 20040);

        rep(i,1,cnt-1){
            printf("%d : %d\n", i, w[i]);
        }

        sort(p+1, p+m+1);

        ll ans = 0;

        if(m <= n-1){
            rep(i,1,n-1){
                int pt = 1;
                if(i<=m) pt = p[i];
                ans = (ans + ((ll)(w[i])*pt)%MOD)%MOD;
            }
            printf("%lld\n", ans);
            continue;
        }

        // 남는 소수 개수 : m - n + 1
        ll pt = 1;
        rep(i,1,m-n+1){
            pt = (pt * (ll)p[i])%MOD;
        }
        ans = w[1]*pt;

        rep(i,m-n+2,m){
            ans = (ans + ((ll)(w[i])*p[i])%MOD)%MOD;
        }
        printf("%lld\n", ans);

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
typedef long long ll;
typedef pair<int,int> pi;
int n,m;
vector<pi> adj[100001];
bool visited[100001];
int p[6*10000 +1];
const int MOD = 1e9 +7;
ll w[200040]; // w[i] 는 i번쨰 간선의 좌우 노드들의 곱
// 간선의 번호는 1번 노드의 가장 낮은 번호로 가는 간선이 1번
// n-1 번 노드의 가장 높은 번호로 가는 간선이 n-1 번

// dfs(v) 는 v 노드의 사이즈를 반환
int dfs(int v){

    visited[v] = true;
    int cnt = 1;
    rep(i,0,(int)adj[v].size()-1){
        if(!visited[adj[v][i].first]){
            int tmp = dfs(adj[v][i].first);

            w[adj[v][i].second] = ((long long)tmp)*(n-tmp);

            cnt += tmp;
        }
    }

    return cnt;
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);

        rep(i,1,n){
            adj[i].clear();
            visited[i] = false;
        }

        rep(i,0,n-1){
            int a,b;
            scanf("%d %d",&a,&b);
            adj[a].push_back({b,0});
            adj[b].push_back({a,0});
        }

        rep(i,1,n) sort(adj[i].begin(), adj[i].end());

        int cnt = 1;
        rep(i,1,n)
            rep(j,0,(int)adj[i].size() -1)
                adj[i][j].second = cnt++;

        scanf("%d",&m);
        rep(i,1,m) scanf("%d",&p[i]);

        memset(w,0,sizeof(w));
        dfs(1);

        sort(w+1, w+ 20040);
        reverse(w+1, w+ 20040);

        sort(p+1, p+m+1);

        ll ans = 0;

        if(m <= n-1){
            rep(i,1,n-1){
                int pt = 1;
                if(i<=m) pt = p[i];
                ans = (ans + ((ll)(w[i])*pt)%MOD)%MOD;
            }
            printf("%lld\n", ans);
            continue;
        }

        // 남는 소수 개수 : m - n + 1
        ll pt = 1;
        rep(i,1,m-n+1){
            pt = (pt * (ll)p[i])%MOD;
        }
        ans = w[1]*pt;

        rep(i,m-n+2,m){
            ans = (ans + ((ll)(w[i])*p[i])%MOD)%MOD;
        }
        printf("%lld\n", ans);

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
const int A = 1e5 + 2;
const int mod = 1e9 + 7;
vector<vector<ll>> v;
vector<ll> power;
ll subtree[A];
int mul(int x, int y) {
    return (1LL * x * y) % mod;
}
int add(int x, int y) {
    return (x + y) % mod;
}
void pre(int node, int par) {
    subtree[node] = 1;
    for (auto to : v[node]) {
        if (to == par) continue;
        pre(to, node);
        subtree[node] += subtree[to];
    }
}
void dfs(int node, int par, ll last) {
    power.emplace_back(last * subtree[node]);
    for (auto to : v[node]) {
        if (to == par) continue;
        dfs(to, node, subtree[node] - subtree[to] + last);
    }
}
int main() {
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int tc;
    cin >> tc;
    while(tc--) {
        int n, m;
        cin >> n;
        v.resize(n + 1);
        for (int i = 0; i < n - 1; i++) {
            int x, y;
            cin >> x >> y;
            v[x].pb(y), v[y].pb(x);
        }
        pre(1, 1);
        cin >> m;
        vector<ll> dev;
        for (int i = 0; i < m; i++) {
            int x;
            cin >> x;
            dev.emplace_back(x);
        }
        while(dev.size() < n - 1)
            dev.emplace_back(1);
        sort(dev.rbegin(), dev.rend());
        while(dev.size() > n - 1) {
            int tmp = dev.back();
            dev.pop_back();
            dev[dev.size() - 1] = mul(tmp, dev.back());
        }
        sort(dev.begin(), dev.end());
        dfs(1, 1, 0);
        sort(power.begin(), power.end());
        int ans = 0;
        for (int i = 0; i < n - 1; i++)
            ans = add(ans, mul(power[i + 1], dev[i]));
        cout << ans << '\n';
        power.clear();
        v.clear();
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
const int A = 1e5 + 2;
const int mod = 1e9 + 7;
vector<vector<int>> v;
vector<ll> power;
int subtree[A];
int mul(int x, int y) {
    return (1LL * x * y) % mod;
}
int add(int x, int y) {
    return (x + y) % mod;
}
void pre(int node, int par) {
    subtree[node] = 1;
    for (auto to : v[node]) {
        if (to == par) continue;
        pre(to, node);
        subtree[node] += subtree[to];
    }
}
void dfs(int node, int par, int last) {
    power.emplace_back(last * subtree[node]);
    for (auto to : v[node]) {
        if (to == par) continue;
        dfs(to, node, subtree[node] - subtree[to] + last);
    }
}
int main() {
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int tc;
    cin >> tc;
    while(tc--) {
        int n, m;
        cin >> n;
        v.resize(n + 1);
        for (int i = 0; i < n - 1; i++) {
            int x, y;
            cin >> x >> y;
            v[x].pb(y), v[y].pb(x);
        }
        pre(1, 1);
        cin >> m;
        int dev[n - 1];
        for (int i = 0; i < n - 1; i++) {
            if (i < m)
                cin >> dev[i];
            else
                dev[i] = 1;
        }
        sort(dev, dev + n - 1);
        dfs(1, 1, 0);
        sort(power.begin(), power.end());
        int ans = 0;
        for (int i = 0; i < n - 1; i++)
            ans = add(ans, mul(power[i + 1], dev[i]));
        cout << ans << '\n';
        power.clear();
        v.clear();
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
const int A = 1e5 + 2;
const int mod = 1e9 + 7;
vector<vector<int>> v;
vector<ll> power;
ll subtree[A];
int mul(int x, int y) {
	return (1LL * x * y) % mod;
}
int add(int x, int y) {
	return (x + y) % mod;
}
void pre(int node, int par) {
	subtree[node] = 1;
	for (auto to : v[node]) {
		if (to == par) continue;
		pre(to, node);
		subtree[node] += subtree[to];
	}
}
void dfs(int node, int par) {
	ll p = (subtree[1] - subtree[node]) * subtree[node];
	power.push_back(p);
	for (auto to : v[node]) {
		if (to == par) continue;
		dfs(to, node);
	}
}
int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int tc;
	cin >> tc;
	while (tc--) {
		int n, m;
		cin >> n;
		v.resize(n + 1);
		for (int i = 0; i < n - 1; i++) {
			int x, y;
			cin >> x >> y;
			v[x].pb(y), v[y].pb(x);
		}
		pre(1, 1);
		cin >> m;
		vector<ll> dev;
		for (int i = 0; i < m; i++) {
			int x;
			cin >> x;
			dev.emplace_back(x);
		}
		while (dev.size() < n - 1)
			dev.emplace_back(1);
		sort(dev.begin(), dev.end());
		while (dev.size() > n - 1) {
			ll tmp = dev.back();
			dev.pop_back();
			dev.back() = mul(tmp, dev.back());
		}
		dfs(1, 1);
		sort(power.begin(), power.end());
		int ans = 0;
		for (int i = 0; i < n - 1; i++)
			ans = add(ans, mul(power[i + 1], dev[i]));
		cout << ans << '\n';
		power.clear();
		v.clear();
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
const int A = 1e5 + 2;
const int mod = 1e9 + 7;
vector<vector<int>> v;
vector<ll> power;
ll subtree[A];
int mul(int x, int y) {
    return (1LL * x * y) % mod;
}
int add(int x, int y) {
    return (x + y) % mod;
}
void pre(int node, int par) {
    subtree[node] = 1;
    for (auto to : v[node]) {
        if (to == par) continue;
        pre(to, node);
        subtree[node] += subtree[to];
    }
}
void dfs(int node, int par, ll last) {
    power.emplace_back(last * subtree[node]);
    for (auto to : v[node]) {
        if (to == par) continue;
        dfs(to, node, subtree[node] - subtree[to] + last);
    }
}
int main() {
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int tc;
    cin >> tc;
    while(tc--) {
        int n, m;
        cin >> n;
        v.resize(n + 1);
        for (int i = 0; i < n - 1; i++) {
            int x, y;
            cin >> x >> y;
            v[x].pb(y), v[y].pb(x);
        }
        pre(1, 1);
        cin >> m;
        vector<ll> dev;
        for (int i = 0; i < m; i++) {
            int x;
            cin >> x;
            dev.emplace_back(x);
        }
        while(dev.size() < n - 1)
            dev.emplace_back(1);
        sort(dev.begin(), dev.end());
        while(dev.size() > n - 1) {
            ll tmp = dev.back();
            dev.pop_back();
            dev.back() *= tmp;
        }
        dfs(1, 1, 0);
        sort(power.begin(), power.end());
        int ans = 0;
        for (int i = 0; i < n - 1; i++)
            ans = add(ans, mul(power[i + 1], dev[i]));
        cout << ans << '\n';
        power.clear();
        v.clear();
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
const int A = 1e5 + 2;
const int mod = 1e9 + 7;
vector<vector<int>> v;
vector<ll> power;
ll subtree[A];
int mul(int x, int y) {
    return (1LL * x * y) % mod;
}
int add(int x, int y) {
    return (x + y) % mod;
}
void pre(int node, int par) {
    subtree[node] = 1;
    for (auto to : v[node]) {
        if (to == par) continue;
        pre(to, node);
        subtree[node] += subtree[to];
    }
}
void dfs(int node, int par, ll last) {
    power.emplace_back(last * subtree[node]);
    for (auto to : v[node]) {
        if (to == par) continue;
        dfs(to, node, subtree[node] - subtree[to] + last);
    }
}
int main() {
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int tc;
    cin >> tc;
    while(tc--) {
        int n, m;
        cin >> n;
        v.resize(n + 1);
        for (int i = 0; i < n - 1; i++) {
            int x, y;
            cin >> x >> y;
            v[x].pb(y), v[y].pb(x);
        }
        pre(1, 1);
        cin >> m;
        vector<ll> dev;
        for (int i = 0; i < m; i++) {
            int x;
            cin >> x;
            dev.emplace_back(x);
        }
        while(dev.size() < n - 1)
            dev.emplace_back(1);
        sort(dev.begin(), dev.end());
        while(dev.size() > n - 1) {
            ll tmp = dev.back();
            dev.pop_back();
            dev.back() = mul(tmp, dev.back());
        }
        dfs(1, 1, 0);
        sort(power.begin(), power.end());
        int ans = 0;
        for (int i = 0; i < n - 1; i++)
            ans = add(ans, mul(power[i + 1], dev[i]));
        cout << ans << '\n';
        power.clear();
        v.clear();
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
const int A = 1e5 + 2;
const int mod = 1e9 + 7;
vector<vector<ll>> v;
vector<ll> power;
ll subtree[A];
ll mul(ll x, ll y) {
    return (1LL * x * y) % mod;
}
ll add(ll x, ll y) {
    return (x + y) % mod;
}
void pre(int node, int par) {
    subtree[node] = 1;
    for (auto to : v[node]) {
        if (to == par) continue;
        pre(to, node);
        subtree[node] += subtree[to];
    }
}
void dfs(int node, int par, ll last) {
    power.emplace_back(last * subtree[node]);
    for (auto to : v[node]) {
        if (to == par) continue;
        dfs(to, node, subtree[node] - subtree[to] + last);
    }
}
int main() {
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int tc;
    cin >> tc;
    while(tc--) {
        int n, m;
        cin >> n;
        v.resize(n + 1);
        for (int i = 0; i < n - 1; i++) {
            int x, y;
            cin >> x >> y;
            v[x].pb(y), v[y].pb(x);
        }
        pre(1, 1);
        cin >> m;
        vector<ll> dev;
        for (int i = 0; i < m; i++) {
            int x;
            cin >> x;
            dev.emplace_back(x);
        }
        while(dev.size() < n - 1)
            dev.emplace_back(1);
        sort(dev.begin(), dev.end());
        while(dev.size() > n - 1) {
            int tmp = dev.back();
            dev.pop_back();
            dev[dev.size() - 1] = mul(tmp, dev.back());
        }
        dfs(1, 1, 0);
        sort(power.begin(), power.end());
        int ans = 0;
        for (int i = 0; i < n - 1; i++)
            ans = add(ans, mul(power[i + 1], dev[i]));
        cout << ans << '\n';
        power.clear();
        v.clear();
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
const int A = 1e5 + 2;
const int mod = 1e9 + 7;
vector<vector<int>> v;
vector<ll> power;
ll subtree[A];
int mul(int x, int y) {
	return (1LL * x * y) % mod;
}
int add(int x, int y) {
	return (x + y) % mod;
}
void pre(int node, int par) {
	subtree[node] = 1;
	for (auto to : v[node]) {
		if (to == par) continue;
		pre(to, node);
		subtree[node] += subtree[to];
	}
}
void dfs(int node, int par) {
	ll p = (subtree[1] - subtree[node]) * subtree[node];
	power.push_back(p);
	for (auto to : v[node]) {
		if (to == par) continue;
		dfs(to, node);
	}
}
int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int tc;
	cin >> tc;
	while (tc--) {
		int n, m;
		cin >> n;
		v.resize(n + 1);
		for (int i = 0; i < n - 1; i++) {
			int x, y;
			cin >> x >> y;
			v[x].pb(y), v[y].pb(x);
		}
		pre(1, 1);
		cin >> m;
		vector<ll> dev;
		for (int i = 0; i < m; i++) {
			int x;
			cin >> x;
			dev.emplace_back(x);
		}

		while (dev.size() < n - 1)
			dev.emplace_back(1);

		sort(dev.begin(), dev.end());

		while (dev.size() > n - 1) {
			ll tmp = dev.back();
			dev.pop_back();
			dev[dev.size()-1] = mul(tmp, dev.back());
		}

		dfs(1, 1);
		sort(power.begin(), power.end());
		int ans = 0;
		for (int i = 0; i < n - 1; i++)
			ans = add(ans, mul(power[i + 1], dev[i]));
		cout << ans << '\n';
		power.clear();
		v.clear();
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
const int A = 1e5 + 2;
const int mod = 1e9 + 7;
vector<vector<ll>> v;
vector<ll> power;
ll subtree[A];
int mul(int x, int y) {
    return (1LL * x * y) % mod;
}
ll add(ll x, ll y) {
    return (x + y) % mod;
}
void pre(int node, int par) {
    subtree[node] = 1;
    for (auto to : v[node]) {
        if (to == par) continue;
        pre(to, node);
        subtree[node] += subtree[to];
    }
}
void dfs(int node, int par, ll last) {
    power.emplace_back(last * subtree[node]);
    for (auto to : v[node]) {
        if (to == par) continue;
        dfs(to, node, subtree[node] - subtree[to] + last);
    }
}
int main() {
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int tc;
    cin >> tc;
    while(tc--) {
        int n, m;
        cin >> n;
        v.resize(n + 1);
        for (int i = 0; i < n - 1; i++) {
            int x, y;
            cin >> x >> y;
            v[x].pb(y), v[y].pb(x);
        }
        pre(1, 1);
        cin >> m;
        vector<ll> dev;
        for (int i = 0; i < m; i++) {
            int x;
            cin >> x;
            dev.emplace_back(x);
        }
        while(dev.size() < n - 1)
            dev.emplace_back(1);
        sort(dev.begin(), dev.end());
        while(dev.size() > n - 1) {
            int tmp = dev.back();
            dev.pop_back();
            dev[dev.size() - 1] = mul(tmp, dev.back());
        }
        dfs(1, 1, 0);
        sort(power.begin(), power.end());
        int ans = 0;
        for (int i = 0; i < n - 1; i++)
            ans = add(ans, mul(power[i + 1], dev[i]));
        cout << ans << '\n';
        power.clear();
        v.clear();
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
const int A = 1e5 + 2;
const int mod = 1e9 + 7;
vector<vector<ll>> v;
vector<ll> power;
ll subtree[A];
ll mul(ll x, ll y) {
    return (x * y) % mod;
}
ll add(ll x, ll y) {
    return (x + y) % mod;
}
void pre(ll node, ll par) {
    subtree[node] = 1;
    for (auto to : v[node]) {
        if (to == par) continue;
        pre(to, node);
        subtree[node] += subtree[to];
    }
}
void dfs(ll node, ll par, ll last) {
    power.emplace_back(last * subtree[node]);
    for (auto to : v[node]) {
        if (to == par) continue;
        dfs(to, node, subtree[node] - subtree[to] + last);
    }
}
int main() {
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int tc;
    cin >> tc;
    while(tc--) {
        ll n, m;
        cin >> n;
        v.resize(n + 1);
        for (int i = 0; i < n - 1; i++) {
            int x, y;
            cin >> x >> y;
            v[x].pb(y), v[y].pb(x);
        }
        pre(1, 1);
        cin >> m;
        vector<ll> dev;
        for (int i = 0; i < m; i++) {
            int x;
            cin >> x;
            dev.emplace_back(x);
        }
        while(dev.size() < n - 1)
            dev.emplace_back(1);
        sort(dev.begin(), dev.end());
        while(dev.size() > n - 1) {
            int tmp = dev.back();
            dev.pop_back();
            dev[dev.size() - 1] = mul(tmp, dev.back());
        }
        dfs(1, 1, 0);
        sort(power.begin(), power.end());
        ll ans = 0;
        for (int i = 0; i < n - 1; i++)
            ans = add(ans, mul(power[i + 1], dev[i]));
        cout << ans << '\n';
        power.clear();
        v.clear();
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
const int A = 1e5 + 2;
const int mod = 1e9 + 7;
vector<vector<int>> v;
vector<ll> power;
int subtree[A];
int mul(int x, int y) {
    return (1LL * x * y) % mod;
}
int add(int x, int y) {
    return (x + y) % mod;
}
void pre(int node, int par) {
    subtree[node] = 1;
    for (auto to : v[node]) {
        if (to == par) continue;
        pre(to, node);
        subtree[node] += subtree[to];
    }
}
void dfs(int node, int par, int last) {
    power.emplace_back(last * subtree[node]);
    for (auto to : v[node]) {
        if (to == par) continue;
        dfs(to, node, subtree[node] - subtree[to] + last);
    }
}
int main() {
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int tc;
    cin >> tc;
    while(tc--) {
        int n, m;
        cin >> n;
        v.resize(n + 1);
        for (int i = 0; i < n - 1; i++) {
            int x, y;
            cin >> x >> y;
            v[x].pb(y), v[y].pb(x);
        }
        pre(1, 1);
        cin >> m;
        vector<int> dev;
        for (int i = 0; i < m; i++) {
            int x;
            cin >> x;
            dev.emplace_back(x);
        }
        while(dev.size() < n - 1)
            dev.emplace_back(1);
        sort(dev.begin(), dev.end());
        while(dev.size() > n - 1) {
            int tmp = dev.back();
            dev.pop_back();
            dev.back() = mul(dev.back(), tmp);
        }
        dfs(1, 1, 0);
        sort(power.begin(), power.end());
        int ans = 0;
        for (int i = 0; i < n - 1; i++)
            ans = add(ans, mul(power[i + 1], dev[i]));
        cout << ans << '\n';
        power.clear();
        v.clear();
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define zhalok Zhalok
#define inf (1<<26)
#define ll long long int
#define ull unsigned long long
#define loop(i,a,b) for(ll i=a;i<b;i++)
#define negloop(i,a,b) for(ll i=a;i>=b;i--)
#define vi vector<int>
#define vll vector<ll>
#define vull vector<ull>
#define pb push_back
#define be begin()
#define en end()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define MOD 1000000007
#define sz 300001

ll cill(ll a,ll b)
{
	if(a%b==0)
	return a/b;
	return a/b+1;
	}

ll maxx(ll a,ll b)
{
	return a>b?a:b;
}

ll minn(ll a,ll b)
{
	return a<b?a:b;
}

vll take_input(ll n)
{
	vll v;
//	v.pb(0);
	loop(i,0,n)
	{
		ll x;
		cin>>x;
		v.pb(x);
	}

	return v;
}

vi take_input(int n)
{
	vi v;
//	v.pb(0);
	loop(i,0,n)
	{
		int x;
		cin>>x;
		v.pb(x);
	}

	return v;
}

void print(vll v)
{
	loop(i,0,v.size())
	cout<<v[i]<<" ";
	cout<<endl;
}

vll num_to_vect(ll n)
{
	vll v,temp;
	v.pb(0);
	while(n)
	{
		temp.pb(n%10);
		n/=10;
	}
	reverse(temp.begin(),temp.end());
	loop(i,0,temp.size())
	v.pb(temp[i]);
	return v;
 }

ll odd_n(ll n)
{
	return (n-1)/2+1;
}

	ll gcd(ll a ,ll b)
	{
		if(a%b==0)
		return b;
		return gcd(b,a%b);
	}

ll get_min(vll v)
{
	ll mn=inf;
	loop(i,0,v.size())
	mn=minn(mn,v[i]);

	return mn;
}

vll adj[sz];
ll n;
map<ll,bool>visited;

ll bfs(int x,int y)
{
	visited.clear();
	queue<int>q;
	q.push(x);
	visited[x]=true;
	visited[y]=true;
	int cnt=1;
	while(!q.empty())
	{
		int f=q.front();
		q.pop();
		for(int i=0;i<adj[f].size();i++)
		{
			int cur=adj[f][i];
			if(visited[cur]==false)
			{
				cnt++;
				visited[cur]=true;
				q.push(cur);
			}
		}

	}

	return cnt*(n-cnt);
}

void printt(vi v)
{
	loop(i,0,v.size()) cout<<v[i]<<" "; cout<<endl;
}

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

int main()
{

	ll  t;
	cin>>t;
	while(t--)
	{
	vpii pairs;
	cin>>n;
	pre_process(n);
	for(ll  i=0;i<n-1;i++)
	{
		ll  x,y;
		cin>>x>>y;
		adj[x].pb(y);
		adj[y].pb(x);
		pairs.pb({x,y});
	}

ll  pf;
cin>>pf;
vi prime_facts;
for(ll  i=0;i<pf;i++)
{
	ll  x;
	cin>>x;
	prime_facts.pb(x);
}

sort(prime_facts.begin(),prime_facts.end());
reverse(prime_facts.begin(),prime_facts.end());

if(pf<(n-1))
{
	for(ll  i=0;i<((n-1)-pf);i++)
	prime_facts.pb(1);
}
else
{
    ll  temp=1;
    while(prime_facts.size()>=n-1)
    {
    	temp*=prime_facts.back();
    //	temp%=MOD;
    	prime_facts.pop_back();
	}
   prime_facts.pb(temp);
	sort(prime_facts.begin(),prime_facts.end());
	reverse(prime_facts.begin(),prime_facts.end());
}

vll weights;
for(ll  i=0;i<pairs.size();i++)
weights.pb(bfs(pairs[i].ff,pairs[i].ss));
sort(weights.begin(),weights.end());
reverse(weights.begin(),weights.end());
///*
ull ans=0;
for(ll  i=0;i<weights.size();i++)
{

ans+=(weights[i]*prime_facts[i]);
ans%=MOD;

}
cout<<ans<<endl;
//*/

//prll t(prime_facts); prll (weights);

}

}


#include<bits/stdc++.h>
using namespace std;
#define zhalok Zhalok
#define inf (1<<26)
#define ll long long int
#define ull unsigned long long
#define loop(i,a,b) for(ll i=a;i<b;i++)
#define negloop(i,a,b) for(ll i=a;i>=b;i--)
#define vi vector<int>
#define vll vector<ll>
#define vull vector<ull>
#define pb push_back
#define be begin()
#define en end()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define MOD 1000000007
#define sz 300001

ll cill(ll a,ll b)
{
	if(a%b==0)
	return a/b;
	return a/b+1;
	}

ll maxx(ll a,ll b)
{
	return a>b?a:b;
}

ll minn(ll a,ll b)
{
	return a<b?a:b;
}

vll take_input(ll n)
{
	vll v;
//	v.pb(0);
	loop(i,0,n)
	{
		ll x;
		cin>>x;
		v.pb(x);
	}

	return v;
}

vi take_input(int n)
{
	vi v;
//	v.pb(0);
	loop(i,0,n)
	{
		int x;
		cin>>x;
		v.pb(x);
	}

	return v;
}

void print(vll v)
{
	loop(i,0,v.size())
	cout<<v[i]<<" ";
	cout<<endl;
}

vll num_to_vect(ll n)
{
	vll v,temp;
	v.pb(0);
	while(n)
	{
		temp.pb(n%10);
		n/=10;
	}
	reverse(temp.begin(),temp.end());
	loop(i,0,temp.size())
	v.pb(temp[i]);
	return v;
 }

ll odd_n(ll n)
{
	return (n-1)/2+1;
}

	ll gcd(ll a ,ll b)
	{
		if(a%b==0)
		return b;
		return gcd(b,a%b);
	}

ll get_min(vll v)
{
	ll mn=inf;
	loop(i,0,v.size())
	mn=minn(mn,v[i]);

	return mn;
}

vll adj[sz];
ll n;
map<ll,bool>visited;

ll bfs(int x,int y)
{
	visited.clear();
	queue<int>q;
	q.push(x);
	visited[x]=true;
	visited[y]=true;
	int cnt=1;
	while(!q.empty())
	{
		int f=q.front();
		q.pop();
		for(int i=0;i<adj[f].size();i++)
		{
			int cur=adj[f][i];
			if(visited[cur]==false)
			{
				cnt++;
				visited[cur]=true;
				q.push(cur);
			}
		}

	}

	return cnt*(n-cnt);
}

void printt(vi v)
{
	loop(i,0,v.size()) cout<<v[i]<<" "; cout<<endl;
}

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

int main()
{

	ll  t;
	cin>>t;
	while(t--)
	{
	vpii pairs;
	cin>>n;
	pre_process(n);
	for(ll  i=0;i<n-1;i++)
	{
		ll  x,y;
		cin>>x>>y;
		adj[x].pb(y);
		adj[y].pb(x);
		pairs.pb({x,y});
	}

ll  pf;
cin>>pf;
vll prime_facts;
for(ll  i=0;i<pf;i++)
{
	ll  x;
	cin>>x;
	prime_facts.pb(x);
}

sort(prime_facts.begin(),prime_facts.end());
reverse(prime_facts.begin(),prime_facts.end());

if(pf<(n-1))
{
	for(ll  i=0;i<((n-1)-pf);i++)
	prime_facts.pb(1);
}
else
{
    ll  temp=1;
    while(prime_facts.size()>=n-1)
    {
    	temp*=prime_facts.back();
    //	temp%=MOD;
    	prime_facts.pop_back();
	}
   prime_facts.pb(temp);
	sort(prime_facts.begin(),prime_facts.end());
	reverse(prime_facts.begin(),prime_facts.end());
}

vll weights;
for(ll  i=0;i<pairs.size();i++)
weights.pb(bfs(pairs[i].ff,pairs[i].ss));
sort(weights.begin(),weights.end());
reverse(weights.begin(),weights.end());
///*
ull ans=0;
for(ll  i=0;i<weights.size();i++)
{

ans+=(weights[i]%MOD*prime_facts[i]%MOD);
ans%=MOD;

}
cout<<ans<<endl;
//*/

//prll t(prime_facts); prll (weights);

}

}


#include<bits/stdc++.h>
using namespace std;
#define zhalok Zhalok
#define inf (1<<26)
#define ll long long int
#define ull unsigned long long
#define loop(i,a,b) for(ll i=a;i<b;i++)
#define negloop(i,a,b) for(ll i=a;i>=b;i--)
#define vi vector<int>
#define vll vector<ll>
#define vull vector<ull>
#define pb push_back
#define be begin()
#define en end()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define MOD 1000000007
#define sz 300001

ll cill(ll a,ll b)
{
	if(a%b==0)
	return a/b;
	return a/b+1;
	}

ll maxx(ll a,ll b)
{
	return a>b?a:b;
}

ll minn(ll a,ll b)
{
	return a<b?a:b;
}

vll take_input(ll n)
{
	vll v;
//	v.pb(0);
	loop(i,0,n)
	{
		ll x;
		cin>>x;
		v.pb(x);
	}

	return v;
}

vi take_input(int n)
{
	vi v;
//	v.pb(0);
	loop(i,0,n)
	{
		int x;
		cin>>x;
		v.pb(x);
	}

	return v;
}

void print(vll v)
{
	loop(i,0,v.size())
	cout<<v[i]<<" ";
	cout<<endl;
}

vll num_to_vect(ll n)
{
	vll v,temp;
	v.pb(0);
	while(n)
	{
		temp.pb(n%10);
		n/=10;
	}
	reverse(temp.begin(),temp.end());
	loop(i,0,temp.size())
	v.pb(temp[i]);
	return v;
 }

ll odd_n(ll n)
{
	return (n-1)/2+1;
}

	ll gcd(ll a ,ll b)
	{
		if(a%b==0)
		return b;
		return gcd(b,a%b);
	}

ll get_min(vll v)
{
	ll mn=inf;
	loop(i,0,v.size())
	mn=minn(mn,v[i]);

	return mn;
}

void printt(vi v)
{
	loop(i,0,v.size()) cout<<v[i]<<" "; cout<<endl;
}

//*************************************

vll adj[sz];
ll n;
map<ll,bool>visited;

vll weights;

ll dfs(ll node)
{
	ll ans=1;
	visited[node]=true;
	 loop(i,0,adj[node].size())
	 {
	 	if(visited[adj[node][i]]==false)
	 	ans+=dfs(adj[node][i]);
	 }
	 weights.pb(ans*(n-ans));
	 return ans;
}

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

//*********************************************************

int main()
{

	ll  t;
	cin>>t;
	while(t--)
	{
	vpii pairs;
	cin>>n;
	pre_process(n);
	for(ll  i=0;i<n-1;i++)
	{
		ll  x,y;
		cin>>x>>y;
		adj[x].pb(y);
		adj[y].pb(x);
	//	pairs.pb({x,y});
	}

ll  pf;
cin>>pf;
vll prime_facts;

for(ll  i=0;i<pf;i++)
{
	ll  x;
	cin>>x;
	prime_facts.pb(x);
}

if(pf<(n-1))
{
	for(ll  i=0;i<((n-1)-pf);i++)
	prime_facts.pb(1);
	sort(prime_facts.begin(),prime_facts.end());
	reverse(prime_facts.begin(),prime_facts.end());
}
else
{
	sort(prime_facts.begin(),prime_facts.end());
    ll  temp=1;
    while(prime_facts.size()>=n-1)
    {
    	temp*=prime_facts.back();
    	temp%=MOD;
    	prime_facts.pop_back();
	}
   prime_facts.pb(temp);
//	sort(prime_facts.begin(),prime_facts.end());
	reverse(prime_facts.begin(),prime_facts.end());
}

weights.clear();
visited.clear();
dfs(1);

sort(weights.begin(),weights.end());
reverse(weights.begin(),weights.end());
///*
ull ans=0;
for(ll  i=0;i<weights.size();i++)
{

ans+=(weights[i]*prime_facts[i]);
ans%=MOD;

}
cout<<ans<<endl;
//*/

//prll t(prime_facts); prll (weights);

}

}


#include<bits/stdc++.h>
using namespace std;
#define zhalok Zhalok
#define inf (1<<26)
#define ll long long int
#define ull unsigned long long
#define loop(i,a,b) for(ll i=a;i<b;i++)
#define negloop(i,a,b) for(ll i=a;i>=b;i--)
#define vi vector<int>
#define vll vector<ll>
#define vull vector<ull>
#define pb push_back
#define be begin()
#define en end()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define MOD 1000000007
#define sz 300001

ll cill(ll a,ll b)
{
	if(a%b==0)
	return a/b;
	return a/b+1;
	}

ll maxx(ll a,ll b)
{
	return a>b?a:b;
}

ll minn(ll a,ll b)
{
	return a<b?a:b;
}

vll take_input(ll n)
{
	vll v;
//	v.pb(0);
	loop(i,0,n)
	{
		ll x;
		cin>>x;
		v.pb(x);
	}

	return v;
}

vi take_input(int n)
{
	vi v;
//	v.pb(0);
	loop(i,0,n)
	{
		int x;
		cin>>x;
		v.pb(x);
	}

	return v;
}

void print(vi v)
{
	loop(i,0,v.size())
	cout<<v[i]<<" ";
	cout<<endl;
}

vll num_to_vect(ll n)
{
	vll v,temp;
	v.pb(0);
	while(n)
	{
		temp.pb(n%10);
		n/=10;
	}
	reverse(temp.begin(),temp.end());
	loop(i,0,temp.size())
	v.pb(temp[i]);
	return v;
 }

ll odd_n(ll n)
{
	return (n-1)/2+1;
}

	ll gcd(ll a ,ll b)
	{
		if(a%b==0)
		return b;
		return gcd(b,a%b);
	}

ll get_min(vll v)
{
	ll mn=inf;
	loop(i,0,v.size())
	mn=minn(mn,v[i]);

	return mn;
}

vi adj[sz];
int n;
map<int,bool>visited;

ll bfs(int x,int y)
{
	visited.clear();
	queue<int>q;
	q.push(x);
	visited[x]=true;
	visited[y]=true;
	int cnt=1;
	while(!q.empty())
	{
		int f=q.front();
		q.pop();
		for(int i=0;i<adj[f].size();i++)
		{
			int cur=adj[f][i];
			if(visited[cur]==false)
			{
				cnt++;
				visited[cur]=true;
				q.push(cur);
			}
		}

	}

	return cnt*(n-cnt);
}

void pre_process(int n)
{
	for(int i=1;i<=n;i++) adj[i].clear();
}

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
	vpii pairs;
	cin>>n;
	pre_process(n);
	for(int i=0;i<n-1;i++)
	{
		int x,y;
		cin>>x>>y;
		adj[x].pb(y);
		adj[y].pb(x);
		pairs.pb({x,y});
	}

int pf;
cin>>pf;
vi prime_facts;
for(int i=0;i<pf;i++)
{
	int x;
	cin>>x;
	prime_facts.pb(x);
}

sort(prime_facts.begin(),prime_facts.end());
reverse(prime_facts.begin(),prime_facts.end());

if(pf<(n-1))
{
	for(int i=0;i<((n-1)-pf);i++)
	prime_facts.pb(1);
}
else
{
    int temp=1;
    while(prime_facts.size()>=n-1)
    {
    	temp*=prime_facts.back();
    	prime_facts.pop_back();
	}
	sort(prime_facts.begin(),prime_facts.end());
	reverse(prime_facts.begin(),prime_facts.end());
}

vll weights;
for(int i=0;i<pairs.size();i++)
weights.pb(bfs(pairs[i].ff,pairs[i].ss));

ll ans=0;
for(int i=0;i<weights.size();i++)
ans+=(weights[i]*prime_facts[i]);

cout<<ans<<endl;

}

}


#include<bits/stdc++.h>
using namespace std;
#define zhalok Zhalok
#define inf (1<<26)
#define ll long long int
#define ull unsigned long long
#define loop(i,a,b) for(ll i=a;i<b;i++)
#define negloop(i,a,b) for(ll i=a;i>=b;i--)
#define vi vector<int>
#define vll vector<ll>
#define vull vector<ull>
#define pb push_back
#define be begin()
#define en end()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define MOD 1000000007
#define sz 300001

ll cill(ll a,ll b)
{
	if(a%b==0)
	return a/b;
	return a/b+1;
	}

ll maxx(ll a,ll b)
{
	return a>b?a:b;
}

ll minn(ll a,ll b)
{
	return a<b?a:b;
}

vll take_input(ll n)
{
	vll v;
//	v.pb(0);
	loop(i,0,n)
	{
		ll x;
		cin>>x;
		v.pb(x);
	}

	return v;
}

vi take_input(int n)
{
	vi v;
//	v.pb(0);
	loop(i,0,n)
	{
		int x;
		cin>>x;
		v.pb(x);
	}

	return v;
}

void print(vll v)
{
	loop(i,0,v.size())
	cout<<v[i]<<" ";
	cout<<endl;
}

vll num_to_vect(ll n)
{
	vll v,temp;
	v.pb(0);
	while(n)
	{
		temp.pb(n%10);
		n/=10;
	}
	reverse(temp.begin(),temp.end());
	loop(i,0,temp.size())
	v.pb(temp[i]);
	return v;
 }

ll odd_n(ll n)
{
	return (n-1)/2+1;
}

	ll gcd(ll a ,ll b)
	{
		if(a%b==0)
		return b;
		return gcd(b,a%b);
	}

ll get_min(vll v)
{
	ll mn=inf;
	loop(i,0,v.size())
	mn=minn(mn,v[i]);

	return mn;
}

void printt(vi v)
{
	loop(i,0,v.size()) cout<<v[i]<<" "; cout<<endl;
}

//*************************************

vll adj[sz];
ll n;
map<ll,bool>visited;

vull weights;

ll dfs(ll node)
{
	ll ans=1;
	visited[node]=true;
	 loop(i,0,adj[node].size())
	 {
	 	if(visited[adj[node][i]]==false)
	 	ans+=dfs(adj[node][i]);
	 }
	 weights.pb(ans*(n-ans));
	 return ans;
}

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

//*********************************************************

int main()
{

	ll  t;
	cin>>t;
	while(t--)
	{
	vpii pairs;
	cin>>n;
	pre_process(n);
	for(ll  i=0;i<n-1;i++)
	{
		ll  x,y;
		cin>>x>>y;
		adj[x].pb(y);
		adj[y].pb(x);
	//	pairs.pb({x,y});
	}

ll  pf;
cin>>pf;
vull prime_facts;

for(ll  i=0;i<pf;i++)
{
	ull  x;
	cin>>x;
	prime_facts.pb(x);
}

if(pf<(n-1))
{
	for(ll  i=0;i<((n-1)-pf);i++)
	prime_facts.pb(1);
	sort(prime_facts.begin(),prime_facts.end());
	reverse(prime_facts.begin(),prime_facts.end());
}
else
{
	sort(prime_facts.begin(),prime_facts.end());
    ull  temp=1;
    while(prime_facts.size()>=n-1)
    {
    	temp*=prime_facts.back();
    //	temp%=MOD;
    	prime_facts.pop_back();
	}
   prime_facts.pb(temp);
	sort(prime_facts.begin(),prime_facts.end());
	reverse(prime_facts.begin(),prime_facts.end());
}

weights.clear();
visited.clear();
dfs(1);

sort(weights.begin(),weights.end());
reverse(weights.begin(),weights.end());
///*
ull ans=0;
for(ll  i=0;i<weights.size();i++)
{

ans+=(weights[i]*prime_facts[i]);
ans%=MOD;

}

cout<<ans<<endl;

//print(weights); print(prime_facts);

}

}


#include<bits/stdc++.h>
using namespace std;
#define zhalok Zhalok
#define inf (1<<26)
#define ll long long int
#define ull unsigned long long
#define loop(i,a,b) for(ll i=a;i<b;i++)
#define negloop(i,a,b) for(ll i=a;i>=b;i--)
#define vi vector<int>
#define vll vector<ll>
#define vull vector<ull>
#define pb push_back
#define be begin()
#define en end()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define MOD 1000000007
#define sz 300001

ll cill(ll a,ll b)
{
	if(a%b==0)
	return a/b;
	return a/b+1;
	}

ll maxx(ll a,ll b)
{
	return a>b?a:b;
}

ll minn(ll a,ll b)
{
	return a<b?a:b;
}

vll take_input(ll n)
{
	vll v;
//	v.pb(0);
	loop(i,0,n)
	{
		ll x;
		cin>>x;
		v.pb(x);
	}

	return v;
}

vi take_input(int n)
{
	vi v;
//	v.pb(0);
	loop(i,0,n)
	{
		int x;
		cin>>x;
		v.pb(x);
	}

	return v;
}

void print(vll v)
{
	loop(i,0,v.size())
	cout<<v[i]<<" ";
	cout<<endl;
}

vll num_to_vect(ll n)
{
	vll v,temp;
	v.pb(0);
	while(n)
	{
		temp.pb(n%10);
		n/=10;
	}
	reverse(temp.begin(),temp.end());
	loop(i,0,temp.size())
	v.pb(temp[i]);
	return v;
 }

ll odd_n(ll n)
{
	return (n-1)/2+1;
}

	ll gcd(ll a ,ll b)
	{
		if(a%b==0)
		return b;
		return gcd(b,a%b);
	}

ll get_min(vll v)
{
	ll mn=inf;
	loop(i,0,v.size())
	mn=minn(mn,v[i]);

	return mn;
}

void printt(vi v)
{
	loop(i,0,v.size()) cout<<v[i]<<" "; cout<<endl;
}

//*************************************

vll adj[sz];
ll n;
map<ll,bool>visited;

vll weights;

ll dfs(ll node)
{
	ll ans=1;
	visited[node]=true;
	 loop(i,0,adj[node].size())
	 {
	 	if(visited[adj[node][i]]==false)
	 	ans+=dfs(adj[node][i]);
	 }
	 weights.pb(ans*(n-ans));
	 return ans;
}

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

//*********************************************************

int main()
{

	ll  t;
	cin>>t;
	while(t--)
	{
	vpii pairs;
	cin>>n;
	pre_process(n);
	for(ll  i=0;i<n-1;i++)
	{
		ll  x,y;
		cin>>x>>y;
		adj[x].pb(y);
		adj[y].pb(x);
	//	pairs.pb({x,y});
	}

ll  pf;
cin>>pf;
vll prime_facts;

for(ll  i=0;i<pf;i++)
{
	ll  x;
	cin>>x;
	prime_facts.pb(x);
}

if(pf<(n-1))
{
	for(ll  i=0;i<((n-1)-pf);i++)
	prime_facts.pb(1);
	sort(prime_facts.begin(),prime_facts.end());
	reverse(prime_facts.begin(),prime_facts.end());
}
else
{
	sort(prime_facts.begin(),prime_facts.end());
    ll  temp=1;
    while(prime_facts.size()>=n-1)
    {
    	temp*=prime_facts.back();
    	temp%=MOD;
    	prime_facts.pop_back();
	}
   prime_facts.pb(temp);
	sort(prime_facts.begin(),prime_facts.end());
	reverse(prime_facts.begin(),prime_facts.end());
}

weights.clear();
visited.clear();
dfs(1);

sort(weights.begin(),weights.end());
reverse(weights.begin(),weights.end());
///*
ull ans=0;
for(ll  i=0;i<weights.size();i++)
{

ans+=(weights[i]*prime_facts[i]);
ans%=MOD;

}
cout<<ans<<endl;
//*/

//prll t(prime_facts); prll (weights);

}

}


#include<bits/stdc++.h>
using namespace std;
#define zhalok Zhalok
#define inf (1<<26)
#define ll long long int
#define ull unsigned long long
#define loop(i,a,b) for(ll i=a;i<b;i++)
#define negloop(i,a,b) for(ll i=a;i>=b;i--)
#define vi vector<int>
#define vll vector<ll>
#define vull vector<ull>
#define pb push_back
#define be begin()
#define en end()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define MOD 1000000007
#define sz 300001

ll cill(ll a,ll b)
{
	if(a%b==0)
	return a/b;
	return a/b+1;
	}

ll maxx(ll a,ll b)
{
	return a>b?a:b;
}

ll minn(ll a,ll b)
{
	return a<b?a:b;
}

vll take_input(ll n)
{
	vll v;
//	v.pb(0);
	loop(i,0,n)
	{
		ll x;
		cin>>x;
		v.pb(x);
	}

	return v;
}

vi take_input(int n)
{
	vi v;
//	v.pb(0);
	loop(i,0,n)
	{
		int x;
		cin>>x;
		v.pb(x);
	}

	return v;
}

void print(vll v)
{
	loop(i,0,v.size())
	cout<<v[i]<<" ";
	cout<<endl;
}

vll num_to_vect(ll n)
{
	vll v,temp;
	v.pb(0);
	while(n)
	{
		temp.pb(n%10);
		n/=10;
	}
	reverse(temp.begin(),temp.end());
	loop(i,0,temp.size())
	v.pb(temp[i]);
	return v;
 }

ll odd_n(ll n)
{
	return (n-1)/2+1;
}

	ll gcd(ll a ,ll b)
	{
		if(a%b==0)
		return b;
		return gcd(b,a%b);
	}

ll get_min(vll v)
{
	ll mn=inf;
	loop(i,0,v.size())
	mn=minn(mn,v[i]);

	return mn;
}

vll adj[sz];
ll n;
map<ll,bool>visited;

ll bfs(int x,int y)
{
	visited.clear();
	queue<int>q;
	q.push(x);
	visited[x]=true;
	visited[y]=true;
	int cnt=1;
	while(!q.empty())
	{
		int f=q.front();
		q.pop();
		for(int i=0;i<adj[f].size();i++)
		{
			int cur=adj[f][i];
			if(visited[cur]==false)
			{
				cnt++;
				visited[cur]=true;
				q.push(cur);
			}
		}

	}

	return cnt*(n-cnt);
}

void printt(vi v)
{
	loop(i,0,v.size()) cout<<v[i]<<" "; cout<<endl;
}

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

int main()
{

	ll  t;
	cin>>t;
	while(t--)
	{
	vpii pairs;
	cin>>n;
	pre_process(n);
	for(ll  i=0;i<n-1;i++)
	{
		ll  x,y;
		cin>>x>>y;
		adj[x].pb(y);
		adj[y].pb(x);
		pairs.pb({x,y});
	}

ll  pf;
cin>>pf;
vll prime_facts;
for(ll  i=0;i<pf;i++)
{
	ll  x;
	cin>>x;
	prime_facts.pb(x);
}

sort(prime_facts.begin(),prime_facts.end());
reverse(prime_facts.begin(),prime_facts.end());

if(pf<(n-1))
{
	for(ll  i=0;i<((n-1)-pf);i++)
	prime_facts.pb(1);
}
else
{
    ll  temp=1;
    while(prime_facts.size()>=n-1)
    {
    	temp*=prime_facts.back();
    	temp%=MOD;
    	prime_facts.pop_back();
	}
   prime_facts.pb(temp);
	sort(prime_facts.begin(),prime_facts.end());
	reverse(prime_facts.begin(),prime_facts.end());
}

vll weights;
for(ll  i=0;i<pairs.size();i++)
weights.pb(bfs(pairs[i].ff,pairs[i].ss));
sort(weights.begin(),weights.end());
reverse(weights.begin(),weights.end());
///*
ull ans=0;
for(ll  i=0;i<weights.size();i++)
{

ans+=(weights[i]%MOD*prime_facts[i]%MOD);
ans%=MOD;

}
cout<<ans<<endl;
//*/

//prll t(prime_facts); prll (weights);

}

}


#include<bits/stdc++.h>
using namespace std;
#define zhalok Zhalok
#define inf (1<<26)
#define ll long long int
#define ull unsigned long long
#define loop(i,a,b) for(ll i=a;i<b;i++)
#define negloop(i,a,b) for(ll i=a;i>=b;i--)
#define vi vector<int>
#define vll vector<ll>
#define vull vector<ull>
#define pb push_back
#define be begin()
#define en end()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define MOD 1000000007
#define sz 300001

ll cill(ll a,ll b)
{
	if(a%b==0)
	return a/b;
	return a/b+1;
	}

ll maxx(ll a,ll b)
{
	return a>b?a:b;
}

ll minn(ll a,ll b)
{
	return a<b?a:b;
}

vll take_input(ll n)
{
	vll v;
//	v.pb(0);
	loop(i,0,n)
	{
		ll x;
		cin>>x;
		v.pb(x);
	}

	return v;
}

vi take_input(int n)
{
	vi v;
//	v.pb(0);
	loop(i,0,n)
	{
		int x;
		cin>>x;
		v.pb(x);
	}

	return v;
}

void print(vll v)
{
	loop(i,0,v.size())
	cout<<v[i]<<" ";
	cout<<endl;
}

vll num_to_vect(ll n)
{
	vll v,temp;
	v.pb(0);
	while(n)
	{
		temp.pb(n%10);
		n/=10;
	}
	reverse(temp.begin(),temp.end());
	loop(i,0,temp.size())
	v.pb(temp[i]);
	return v;
 }

ll odd_n(ll n)
{
	return (n-1)/2+1;
}

	ll gcd(ll a ,ll b)
	{
		if(a%b==0)
		return b;
		return gcd(b,a%b);
	}

ll get_min(vll v)
{
	ll mn=inf;
	loop(i,0,v.size())
	mn=minn(mn,v[i]);

	return mn;
}

vll adj[sz];
ll n;
map<ll,bool>visited;

ll bfs(int x,int y)
{
	visited.clear();
	queue<int>q;
	q.push(x);
	visited[x]=true;
	visited[y]=true;
	int cnt=1;
	while(!q.empty())
	{
		int f=q.front();
		q.pop();
		for(int i=0;i<adj[f].size();i++)
		{
			int cur=adj[f][i];
			if(visited[cur]==false)
			{
				cnt++;
				visited[cur]=true;
				q.push(cur);
			}
		}

	}

	return cnt*(n-cnt);
}

void printt(vi v)
{
	loop(i,0,v.size()) cout<<v[i]<<" "; cout<<endl;
}

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

int main()
{

	ll  t;
	cin>>t;
	while(t--)
	{
	vpii pairs;
	cin>>n;
	pre_process(n);
	for(ll  i=0;i<n-1;i++)
	{
		ll  x,y;
		cin>>x>>y;
		adj[x].pb(y);
		adj[y].pb(x);
		pairs.pb({x,y});
	}

ll  pf;
cin>>pf;
vi prime_facts;
for(ll  i=0;i<pf;i++)
{
	ll  x;
	cin>>x;
	prime_facts.pb(x);
}

sort(prime_facts.begin(),prime_facts.end());
reverse(prime_facts.begin(),prime_facts.end());

if(pf<(n-1))
{
	for(ll  i=0;i<((n-1)-pf);i++)
	prime_facts.pb(1);
}
else
{
    ll  temp=1;
    while(prime_facts.size()>=n-1)
    {
    	temp*=prime_facts.back();
    	temp%=MOD;
    	prime_facts.pop_back();
	}
   prime_facts.pb(temp);
	sort(prime_facts.begin(),prime_facts.end());
	reverse(prime_facts.begin(),prime_facts.end());
}

vll weights;
for(ll  i=0;i<pairs.size();i++)
weights.pb(bfs(pairs[i].ff,pairs[i].ss));
sort(weights.begin(),weights.end());
reverse(weights.begin(),weights.end());
///*
ll ans=0;
for(ll  i=0;i<weights.size();i++)
{

ans+=(weights[i]*prime_facts[i]);
ans%=MOD;

}
cout<<ans<<endl;
//*/

//prll t(prime_facts); prll (weights);

}

}


#include<bits/stdc++.h>
using namespace std;
#define zhalok Zhalok
#define inf (1<<26)
#define ll long long int
#define ull unsigned long long
#define loop(i,a,b) for(ll i=a;i<b;i++)
#define negloop(i,a,b) for(ll i=a;i>=b;i--)
#define vi vector<int>
#define vll vector<ll>
#define vull vector<ull>
#define pb push_back
#define be begin()
#define en end()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define MOD 1000000007
#define sz 300001

ll cill(ll a,ll b)
{
	if(a%b==0)
	return a/b;
	return a/b+1;
	}

ll maxx(ll a,ll b)
{
	return a>b?a:b;
}

ll minn(ll a,ll b)
{
	return a<b?a:b;
}

vll take_input(ll n)
{
	vll v;
//	v.pb(0);
	loop(i,0,n)
	{
		ll x;
		cin>>x;
		v.pb(x);
	}

	return v;
}

vi take_input(int n)
{
	vi v;
//	v.pb(0);
	loop(i,0,n)
	{
		int x;
		cin>>x;
		v.pb(x);
	}

	return v;
}

void print(vll v)
{
	loop(i,0,v.size())
	cout<<v[i]<<" ";
	cout<<endl;
}

vll num_to_vect(ll n)
{
	vll v,temp;
	v.pb(0);
	while(n)
	{
		temp.pb(n%10);
		n/=10;
	}
	reverse(temp.begin(),temp.end());
	loop(i,0,temp.size())
	v.pb(temp[i]);
	return v;
 }

ll odd_n(ll n)
{
	return (n-1)/2+1;
}

	ll gcd(ll a ,ll b)
	{
		if(a%b==0)
		return b;
		return gcd(b,a%b);
	}

ll get_min(vll v)
{
	ll mn=inf;
	loop(i,0,v.size())
	mn=minn(mn,v[i]);

	return mn;
}

vi adj[sz];
int n;
map<int,bool>visited;

ll bfs(int x,int y)
{
	visited.clear();
	queue<int>q;
	q.push(x);
	visited[x]=true;
	visited[y]=true;
	int cnt=1;
	while(!q.empty())
	{
		int f=q.front();
		q.pop();
		for(int i=0;i<adj[f].size();i++)
		{
			int cur=adj[f][i];
			if(visited[cur]==false)
			{
				cnt++;
				visited[cur]=true;
				q.push(cur);
			}
		}

	}

	return cnt*(n-cnt);
}

void printt(vi v)
{
	loop(i,0,v.size()) cout<<v[i]<<" "; cout<<endl;
}

void pre_process(int n)
{
	for(int i=1;i<=n;i++) adj[i].clear();
}

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
	vpii pairs;
	cin>>n;
	pre_process(n);
	for(int i=0;i<n-1;i++)
	{
		int x,y;
		cin>>x>>y;
		adj[x].pb(y);
		adj[y].pb(x);
		pairs.pb({x,y});
	}

int pf;
cin>>pf;
vi prime_facts;
for(int i=0;i<pf;i++)
{
	int x;
	cin>>x;
	prime_facts.pb(x);
}

sort(prime_facts.begin(),prime_facts.end());
reverse(prime_facts.begin(),prime_facts.end());

if(pf<(n-1))
{
	for(int i=0;i<((n-1)-pf);i++)
	prime_facts.pb(1);
}
else
{
    int temp=1;
    while(prime_facts.size()>=n-1)
    {
    	temp*=prime_facts.back();
    	prime_facts.pop_back();
	}
   prime_facts.pb(temp);
	sort(prime_facts.begin(),prime_facts.end());
	reverse(prime_facts.begin(),prime_facts.end());
}

vll weights;
for(int i=0;i<pairs.size();i++)
weights.pb(bfs(pairs[i].ff,pairs[i].ss));
sort(weights.begin(),weights.end());
reverse(weights.begin(),weights.end());
///*
ll ans=0;
for(int i=0;i<weights.size();i++)
ans+=(weights[i]*prime_facts[i]);

cout<<ans<<endl;
//*/

//printt(prime_facts); print(weights);

}

}


#include<bits/stdc++.h>
using namespace std;
#define zhalok Zhalok
#define inf (1<<26)
#define ll long long int
#define ull unsigned long long
#define loop(i,a,b) for(ll i=a;i<b;i++)
#define negloop(i,a,b) for(ll i=a;i>=b;i--)
#define vi vector<int>
#define vll vector<ll>
#define vull vector<ull>
#define pb push_back
#define be begin()
#define en end()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define MOD 1000000007
#define sz 300001

ll cill(ll a,ll b)
{
	if(a%b==0)
	return a/b;
	return a/b+1;
	}

ll maxx(ll a,ll b)
{
	return a>b?a:b;
}

ll minn(ll a,ll b)
{
	return a<b?a:b;
}

vll take_input(ll n)
{
	vll v;
//	v.pb(0);
	loop(i,0,n)
	{
		ll x;
		cin>>x;
		v.pb(x);
	}

	return v;
}

vi take_input(int n)
{
	vi v;
//	v.pb(0);
	loop(i,0,n)
	{
		int x;
		cin>>x;
		v.pb(x);
	}

	return v;
}

void print(vll v)
{
	loop(i,0,v.size())
	cout<<v[i]<<" ";
	cout<<endl;
}

vll num_to_vect(ll n)
{
	vll v,temp;
	v.pb(0);
	while(n)
	{
		temp.pb(n%10);
		n/=10;
	}
	reverse(temp.begin(),temp.end());
	loop(i,0,temp.size())
	v.pb(temp[i]);
	return v;
 }

ll odd_n(ll n)
{
	return (n-1)/2+1;
}

	ll gcd(ll a ,ll b)
	{
		if(a%b==0)
		return b;
		return gcd(b,a%b);
	}

ll get_min(vll v)
{
	ll mn=inf;
	loop(i,0,v.size())
	mn=minn(mn,v[i]);

	return mn;
}

vll adj[sz];
ll n;
map<ll,bool>visited;

ll bfs(int x,int y)
{
	visited.clear();
	queue<int>q;
	q.push(x);
	visited[x]=true;
	visited[y]=true;
	int cnt=1;
	while(!q.empty())
	{
		int f=q.front();
		q.pop();
		for(int i=0;i<adj[f].size();i++)
		{
			int cur=adj[f][i];
			if(visited[cur]==false)
			{
				cnt++;
				visited[cur]=true;
				q.push(cur);
			}
		}

	}

	return cnt*(n-cnt);
}

void printt(vi v)
{
	loop(i,0,v.size()) cout<<v[i]<<" "; cout<<endl;
}

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

int main()
{

	ll  t;
	cin>>t;
	while(t--)
	{
	vpii pairs;
	cin>>n;
	pre_process(n);
	for(ll  i=0;i<n-1;i++)
	{
		ll  x,y;
		cin>>x>>y;
		adj[x].pb(y);
		adj[y].pb(x);
		pairs.pb({x,y});
	}

ll  pf;
cin>>pf;
vi prime_facts;
for(ll  i=0;i<pf;i++)
{
	ll  x;
	cin>>x;
	prime_facts.pb(x);
}

sort(prime_facts.begin(),prime_facts.end());
reverse(prime_facts.begin(),prime_facts.end());

if(pf<(n-1))
{
	for(ll  i=0;i<((n-1)-pf);i++)
	prime_facts.pb(1);
}
else
{
    ll  temp=1;
    while(prime_facts.size()>=n-1)
    {
    	temp*=prime_facts.back();
    	prime_facts.pop_back();
	}
   prime_facts.pb(temp);
	sort(prime_facts.begin(),prime_facts.end());
	reverse(prime_facts.begin(),prime_facts.end());
}

vll weights;
for(ll  i=0;i<pairs.size();i++)
weights.pb(bfs(pairs[i].ff,pairs[i].ss));
sort(weights.begin(),weights.end());
reverse(weights.begin(),weights.end());
///*
ll ans=0;
for(ll  i=0;i<weights.size();i++)
ans+=(weights[i]*prime_facts[i]);

cout<<ans<<endl;
//*/

//prll t(prime_facts); prll (weights);

}

}


#include<bits/stdc++.h>
using namespace std;
#define zhalok Zhalok
#define inf (1<<26)
#define ll long long int
#define ull unsigned long long
#define loop(i,a,b) for(ll i=a;i<b;i++)
#define negloop(i,a,b) for(ll i=a;i>=b;i--)
#define vi vector<int>
#define vll vector<ll>
#define vull vector<ull>
#define pb push_back
#define be begin()
#define en end()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define MOD 1000000007
#define sz 300001

ll cill(ll a,ll b)
{
	if(a%b==0)
	return a/b;
	return a/b+1;
	}

ll maxx(ll a,ll b)
{
	return a>b?a:b;
}

ll minn(ll a,ll b)
{
	return a<b?a:b;
}

vll take_input(ll n)
{
	vll v;
//	v.pb(0);
	loop(i,0,n)
	{
		ll x;
		cin>>x;
		v.pb(x);
	}

	return v;
}

vi take_input(int n)
{
	vi v;
//	v.pb(0);
	loop(i,0,n)
	{
		int x;
		cin>>x;
		v.pb(x);
	}

	return v;
}

void print(vll v)
{
	loop(i,0,v.size())
	cout<<v[i]<<" ";
	cout<<endl;
}

vll num_to_vect(ll n)
{
	vll v,temp;
	v.pb(0);
	while(n)
	{
		temp.pb(n%10);
		n/=10;
	}
	reverse(temp.begin(),temp.end());
	loop(i,0,temp.size())
	v.pb(temp[i]);
	return v;
 }

ll odd_n(ll n)
{
	return (n-1)/2+1;
}

	ll gcd(ll a ,ll b)
	{
		if(a%b==0)
		return b;
		return gcd(b,a%b);
	}

ll get_min(vll v)
{
	ll mn=inf;
	loop(i,0,v.size())
	mn=minn(mn,v[i]);

	return mn;
}

vll adj[sz];
ll n;
map<ll,bool>visited;

ll dfs(ll x)
{
	visited[x]=true;
	ll ans=0;
	for(int i=0;i<adj[x].size();i++)
	{
		ll cur=adj[x][i];
		if(visited[cur]==false)
		ans+=dfs(cur);
	}
	return ans+1;
}

ll do_dfs(ll x,ll y)
{
	visited.clear();
	visited[y]=true;
	return dfs(x);

}

ll bfs(int x,int y)
{
	visited.clear();
	queue<int>q;
	q.push(x);
	visited[x]=true;
	visited[y]=true;
	int cnt=1;
	while(!q.empty())
	{
		int f=q.front();
		q.pop();
		for(int i=0;i<adj[f].size();i++)
		{
			int cur=adj[f][i];
			if(visited[cur]==false)
			{
				cnt++;
				visited[cur]=true;
				q.push(cur);
			}
		}

	}

	return cnt*(n-cnt);
}

void printt(vi v)
{
	loop(i,0,v.size()) cout<<v[i]<<" "; cout<<endl;
}

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

int main()
{

	ll  t;
	cin>>t;
	while(t--)
	{
	vpii pairs;
	cin>>n;
	pre_process(n);
	for(ll  i=0;i<n-1;i++)
	{
		ll  x,y;
		cin>>x>>y;
		adj[x].pb(y);
		adj[y].pb(x);
		pairs.pb({x,y});
	}

ll  pf;
cin>>pf;
vll prime_facts;
for(ll  i=0;i<pf;i++)
{
	ll  x;
	cin>>x;
	prime_facts.pb(x);
}

if(pf<(n-1))
{
	for(ll  i=0;i<((n-1)-pf);i++)
	prime_facts.pb(1);
	sort(prime_facts.begin(),prime_facts.end());
	reverse(prime_facts.begin(),prime_facts.end());
}
else
{
	sort(prime_facts.begin(),prime_facts.end());
    ll  temp=1;
    while(prime_facts.size()>=n-1)
    {
    	temp*=prime_facts.back();
    	temp%=MOD;
    	prime_facts.pop_back();
	}
   prime_facts.pb(temp);
	sort(prime_facts.begin(),prime_facts.end());
	reverse(prime_facts.begin(),prime_facts.end());
}

vll weights;
for(ll  i=0;i<pairs.size();i++)
weights.pb(bfs(pairs[i].ff,pairs[i].ss));
sort(weights.begin(),weights.end());
reverse(weights.begin(),weights.end());
///*
ull ans=0;
for(ll  i=0;i<weights.size();i++)
{

ans+=(weights[i]%MOD*prime_facts[i]%MOD);
ans%=MOD;

}
cout<<ans<<endl;
//*/

//prll t(prime_facts); prll (weights);

}

}


#include<bits/stdc++.h>
using namespace std;
#define zhalok Zhalok
#define inf (1<<26)
#define ll long long int
#define ull unsigned long long
#define loop(i,a,b) for(ll i=a;i<b;i++)
#define negloop(i,a,b) for(ll i=a;i>=b;i--)
#define vi vector<int>
#define vll vector<ll>
#define vull vector<ull>
#define pb push_back
#define be begin()
#define en end()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define MOD 1000000007
#define sz 300001

ll cill(ll a,ll b)
{
	if(a%b==0)
	return a/b;
	return a/b+1;
	}

ll maxx(ll a,ll b)
{
	return a>b?a:b;
}

ll minn(ll a,ll b)
{
	return a<b?a:b;
}

vll take_input(ll n)
{
	vll v;
//	v.pb(0);
	loop(i,0,n)
	{
		ll x;
		cin>>x;
		v.pb(x);
	}

	return v;
}

vi take_input(int n)
{
	vi v;
//	v.pb(0);
	loop(i,0,n)
	{
		int x;
		cin>>x;
		v.pb(x);
	}

	return v;
}

void print(vll v)
{
	loop(i,0,v.size())
	cout<<v[i]<<" ";
	cout<<endl;
}

vll num_to_vect(ll n)
{
	vll v,temp;
	v.pb(0);
	while(n)
	{
		temp.pb(n%10);
		n/=10;
	}
	reverse(temp.begin(),temp.end());
	loop(i,0,temp.size())
	v.pb(temp[i]);
	return v;
 }

ll odd_n(ll n)
{
	return (n-1)/2+1;
}

	ll gcd(ll a ,ll b)
	{
		if(a%b==0)
		return b;
		return gcd(b,a%b);
	}

ll get_min(vll v)
{
	ll mn=inf;
	loop(i,0,v.size())
	mn=minn(mn,v[i]);

	return mn;
}

void printt(vi v)
{
	loop(i,0,v.size()) cout<<v[i]<<" "; cout<<endl;
}

//*************************************

vll adj[sz];
ll n;
map<ll,bool>visited;

vll weights;

ll dfs(ll node)
{
	ll ans=1;
	visited[node]=true;
	 loop(i,0,adj[node].size())
	 {
	 	if(visited[adj[node][i]]==false)
	 	ans+=dfs(adj[node][i]);
	 }
	 weights.pb(ans*(n-ans));
	 return ans;
}

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

//*********************************************************

int main()
{

	ll  t;
	cin>>t;
	while(t--)
	{
	vpii pairs;
	cin>>n;
	pre_process(n);
	for(ll  i=0;i<n-1;i++)
	{
		ll  x,y;
		cin>>x>>y;
		adj[x].pb(y);
		adj[y].pb(x);
	//	pairs.pb({x,y});
	}

ll  pf;
cin>>pf;
vll prime_facts;

for(ll  i=0;i<pf;i++)
{
	ll  x;
	cin>>x;
	prime_facts.pb(x);
}

if(pf<(n-1))
{
	for(ll  i=0;i<((n-1)-pf);i++)
	prime_facts.pb(1);
	sort(prime_facts.begin(),prime_facts.end());
	reverse(prime_facts.begin(),prime_facts.end());
}
else
{
	sort(prime_facts.begin(),prime_facts.end());
    ll  temp=1;
    while(prime_facts.size()>=n-1)
    {
    	temp*=prime_facts.back();
    //	temp%=MOD;
    	prime_facts.pop_back();
	}
   prime_facts.pb(temp);
	sort(prime_facts.begin(),prime_facts.end());
	reverse(prime_facts.begin(),prime_facts.end());
}

weights.clear();
visited.clear();
dfs(1);

sort(weights.begin(),weights.end());
reverse(weights.begin(),weights.end());
///*
ull ans=0;
for(ll  i=0;i<weights.size();i++)
{

ans+=(weights[i]*prime_facts[i]);
ans%=MOD;

}

cout<<ans<<endl;

//print(weights); print(prime_facts);

}

}


//https://2no.co/2bjRx5
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#pragma GCC target("popcnt")  //https://godbolt.org/z/yQv94Y

typedef long long int ll;
typedef unsigned long long int  ull;
typedef long double ld;

const ld pi = 4.0*atan(1.0);
#define xlr8 ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout.precision(10); cout<<fixed

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
// T.insert(a), T.erase(a), T.size()
// T.order_of_key(a) -- number of elements strictly less than a
// *T.find_by_order(k) -- k-th element in increasing order

struct Timer
{   chrono::time_point<chrono::high_resolution_clock> start , end; chrono:: duration<float> dur;
    Timer(){start = chrono::high_resolution_clock::now();}
    ~Timer(){   end = chrono::high_resolution_clock::now(); dur = end - start;  cout<<"\nTime Elapsed : " <<dur.count() << " s\n";  }
};

template<typename T>
void getVec(vector<T>& a , int st, int en)
{
    int dif = (en > st ? 1 : -1);
    for(int  i = st ; i!= en ; i+= dif)
        cin>>a[i];
}
template<typename T>
void getVecPair(vector<pair<T , T > >& a , int st, int en)
{
    int dif = (en > st ? 1 : -1);
    for(int  i = st ; i!= en ; i+= dif)
        cin>>a[i].first>>a[i].second;
}

//76149
template<int D, typename T>
struct Vec : public vector<Vec<D - 1, T>> {
  static_assert(D >= 1, "Vector dimension must be greater than zero!");
  template<typename... Args>
  Vec(int n = 0, Args... args) : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {
  }
};
template<typename T>
struct Vec<1, T> : public vector<T> {
  Vec(int n = 0, T val = T()) : vector<T>(n, val) {
  }
};

const ll MOD = 1e9 + 7;
struct Graph
{
    int n , m;
    vector<vector<int> > adj;
    vector<ll> p , sz;
    vector<ll> edgeFrequency;

    Graph(int n)
    {
        this->n = n;
        adj.resize(n +  1);
        sz.resize( n + 1);
    }
    void addEdge(int u , int v)
    {
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    void dfs(int u , int p )
    {
        sz[u] = 1;
        for(auto v : adj[u])
        {
            if(v!= p)
            {
                dfs(v , u);
                sz[u] += sz[v];
            }
        }
    }

    void dfsReroot(int u , int p )
    {
        for(auto v: adj[u])
        {
            if(v!= p)
            {
                sz[u] -= sz[v];
                edgeFrequency.push_back(sz[u]*sz[v]);
                sz[v] += sz[u];
                dfsReroot(v , u);
                sz[v] -= sz[u];
                sz[u] += sz[v];
            }
        }
    }
    void solve()
    {
        dfs(1, - 1);
        dfsReroot(1 , -1);
        sort(edgeFrequency.rbegin() , edgeFrequency.rend());
        ll ans = 0;

        multiset<ll , greater<ll> > divisors;
        for(auto& x: p)
            divisors.insert(x);
        while(divisors.size() < n - 1)
            divisors.insert(1LL);
        while(divisors.size() < n - 1)
        {
            ll tmp = *divisors.begin();
            divisors.erase(divisors.begin());
            tmp = tmp* (*divisors.begin());
            divisors.erase(divisors.begin());
            divisors.insert(tmp);
        }

        int idx = 0;
        for(auto& x : divisors)
            ans = (ans + (x*edgeFrequency[idx++])%MOD)%MOD;

        cout<<ans<<"\n";
    }
};

int main()
{
    xlr8;
    int testCases  = 1;
    cin>>testCases;
    while (testCases--)
    {
        int n;
        cin>>n;
        Graph g(n);
        for(int i = 0 ; i< n - 1; i++)
        {
            int u , v;
            cin>>u>>v;
            g.addEdge(u , v);
        }
        cin>>g.m;
        for(int i = 0 ; i < g.m; i++)
            {
                int x;
                cin>>x;
                g.p.emplace_back(x);
            }
        g.solve();
    }
}


//https://2no.co/2bjRx5
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#pragma GCC target("popcnt")  //https://godbolt.org/z/yQv94Y

typedef long long int ll;
typedef unsigned long long int  ull;
typedef long double ld;

const ld pi = 4.0*atan(1.0);
#define xlr8 ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout.precision(10); cout<<fixed

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
// T.insert(a), T.erase(a), T.size()
// T.order_of_key(a) -- number of elements strictly less than a
// *T.find_by_order(k) -- k-th element in increasing order

struct Timer
{   chrono::time_point<chrono::high_resolution_clock> start , end; chrono:: duration<float> dur;
    Timer(){start = chrono::high_resolution_clock::now();}
    ~Timer(){   end = chrono::high_resolution_clock::now(); dur = end - start;  cout<<"\nTime Elapsed : " <<dur.count() << " s\n";  }
};

template<typename T>
void getVec(vector<T>& a , int st, int en)
{
    int dif = (en > st ? 1 : -1);
    for(int  i = st ; i!= en ; i+= dif)
        cin>>a[i];
}
template<typename T>
void getVecPair(vector<pair<T , T > >& a , int st, int en)
{
    int dif = (en > st ? 1 : -1);
    for(int  i = st ; i!= en ; i+= dif)
        cin>>a[i].first>>a[i].second;
}

//76149
template<int D, typename T>
struct Vec : public vector<Vec<D - 1, T>> {
  static_assert(D >= 1, "Vector dimension must be greater than zero!");
  template<typename... Args>
  Vec(int n = 0, Args... args) : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {
  }
};
template<typename T>
struct Vec<1, T> : public vector<T> {
  Vec(int n = 0, T val = T()) : vector<T>(n, val) {
  }
};

const ll MOD = 1e9 + 7;
struct Graph
{
    int n , m;
    vector<vector<int> > adj;
    vector<ll> p , sz;
    vector<ll> edgeFrequency;

    Graph(int n)
    {
        this->n = n;
        adj.resize(n +  1);
        sz.resize( n + 1);
    }
    void addEdge(int u , int v)
    {
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    void dfs(int u , int p )
    {
        sz[u] = 1;
        for(auto v : adj[u])
        {
            if(v!= p)
            {
                dfs(v , u);
                sz[u] += sz[v];
            }
        }
    }

    void dfsReroot(int u , int p )
    {
        for(auto v: adj[u])
        {
            if(v!= p)
            {
                sz[u] -= sz[v];
                edgeFrequency.push_back(sz[u]*sz[v]);
                sz[v] += sz[u];
                dfsReroot(v , u);
                sz[v] -= sz[u];
                sz[u] += sz[v];
            }
        }
    }
    void solve()
    {
        dfs(1, - 1);
        dfsReroot(1 , -1);
        sort(edgeFrequency.rbegin() , edgeFrequency.rend());
        ll ans = 0;

        multiset<ll , greater<ll> > divisors;
        for(auto& x: p)
            divisors.insert(x);
        while(divisors.size() < n - 1)
            divisors.insert(1LL);
        while(divisors.size() < n - 1)
        {
            ll tmp = *divisors.begin();
            divisors.erase(divisors.begin());
            auto it = divisors.begin();
            //--it;

            tmp = (tmp* (*it))%MOD;
            divisors.erase(it);
            divisors.insert(tmp);
        }

        int idx = 0;
        for(auto& x : divisors)
            ans = (ans + (x*edgeFrequency[idx++])%MOD)%MOD;

        cout<<ans<<"\n";
    }
};

int main()
{
    xlr8;
    int testCases  = 1;
    cin>>testCases;
    while (testCases--)
    {
        int n;
        cin>>n;
        Graph g(n);
        for(int i = 0 ; i< n - 1; i++)
        {
            int u , v;
            cin>>u>>v;
            g.addEdge(u , v);
        }
        cin>>g.m;
        for(int i = 0 ; i < g.m; i++)
            {
                int x;
                cin>>x;
                g.p.emplace_back(x);
            }
        g.solve();
    }
}


//https://2no.co/2bjRx5
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#pragma GCC target("popcnt")  //https://godbolt.org/z/yQv94Y

typedef long long int ll;
typedef unsigned long long int  ull;
typedef long double ld;

const ld pi = 4.0*atan(1.0);
#define xlr8 ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout.precision(10); cout<<fixed

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
// T.insert(a), T.erase(a), T.size()
// T.order_of_key(a) -- number of elements strictly less than a
// *T.find_by_order(k) -- k-th element in increasing order

struct Timer
{   chrono::time_point<chrono::high_resolution_clock> start , end; chrono:: duration<float> dur;
    Timer(){start = chrono::high_resolution_clock::now();}
    ~Timer(){   end = chrono::high_resolution_clock::now(); dur = end - start;  cout<<"\nTime Elapsed : " <<dur.count() << " s\n";  }
};

template<typename T>
void getVec(vector<T>& a , int st, int en)
{
    int dif = (en > st ? 1 : -1);
    for(int  i = st ; i!= en ; i+= dif)
        cin>>a[i];
}
template<typename T>
void getVecPair(vector<pair<T , T > >& a , int st, int en)
{
    int dif = (en > st ? 1 : -1);
    for(int  i = st ; i!= en ; i+= dif)
        cin>>a[i].first>>a[i].second;
}

//76149
template<int D, typename T>
struct Vec : public vector<Vec<D - 1, T>> {
  static_assert(D >= 1, "Vector dimension must be greater than zero!");
  template<typename... Args>
  Vec(int n = 0, Args... args) : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {
  }
};
template<typename T>
struct Vec<1, T> : public vector<T> {
  Vec(int n = 0, T val = T()) : vector<T>(n, val) {
  }
};

const ll MOD = 1e9 + 7;
struct Graph
{
    int n , m;
    vector<vector<int> > adj;
    vector<ll> p , sz;
    vector<ll> edgeFrequency;

    Graph(int n)
    {
        this->n = n;
        adj.resize(n +  1);
        sz.resize( n + 1);
    }
    void addEdge(int u , int v)
    {
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    void dfs(int u , int p )
    {
        sz[u] = 1;
        for(auto v : adj[u])
        {
            if(v!= p)
            {
                dfs(v , u);
                sz[u] += sz[v];
            }
        }
    }

    void dfsReroot(int u , int p )
    {
        for(auto v: adj[u])
        {
            if(v!= p)
            {
                sz[u] -= sz[v];
                edgeFrequency.push_back(sz[u]*sz[v]);
                sz[v] += sz[u];
                dfsReroot(v , u);
                sz[v] -= sz[u];
                sz[u] += sz[v];
            }
        }
    }
    void solve()
    {
        dfs(1, - 1);
        dfsReroot(1 , -1);
        sort(edgeFrequency.rbegin() , edgeFrequency.rend());
        ll ans = 0;

        multiset<ll , greater<ll> > divisors;
        for(auto& x: p)
            divisors.insert(x);
        while(divisors.size() < n - 1)
            divisors.insert(1LL);

        while(divisors.size() > n - 1)
        {
            ll tmp = *divisors.begin();
            divisors.erase(divisors.begin());
            auto it = divisors.end();
            --it;

            tmp = (tmp* (*it))%MOD;
            divisors.erase(it);
            divisors.insert(tmp);
        }

        int idx = 0;
        for(auto& x : divisors)
            ans = (ans + (x*edgeFrequency[idx++])%MOD)%MOD;

        cout<<ans<<"\n";
    }
};

int main()
{
    xlr8;
    int testCases  = 1;
    cin>>testCases;
    while (testCases--)
    {
        int n;
        cin>>n;
        Graph g(n);
        for(int i = 0 ; i< n - 1; i++)
        {
            int u , v;
            cin>>u>>v;
            g.addEdge(u , v);
        }
        cin>>g.m;
        for(int i = 0 ; i < g.m; i++)
            {
                int x;
                cin>>x;
                g.p.emplace_back(x);
            }
        g.solve();
    }
}


//https://2no.co/2bjRx5
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#pragma GCC target("popcnt")  //https://godbolt.org/z/yQv94Y

typedef long long int ll;
typedef unsigned long long int  ull;
typedef long double ld;

const ld pi = 4.0*atan(1.0);
#define xlr8 ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout.precision(10); cout<<fixed

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
// T.insert(a), T.erase(a), T.size()
// T.order_of_key(a) -- number of elements strictly less than a
// *T.find_by_order(k) -- k-th element in increasing order

struct Timer
{   chrono::time_point<chrono::high_resolution_clock> start , end; chrono:: duration<float> dur;
    Timer(){start = chrono::high_resolution_clock::now();}
    ~Timer(){   end = chrono::high_resolution_clock::now(); dur = end - start;  cout<<"\nTime Elapsed : " <<dur.count() << " s\n";  }
};

template<typename T>
void getVec(vector<T>& a , int st, int en)
{
    int dif = (en > st ? 1 : -1);
    for(int  i = st ; i!= en ; i+= dif)
        cin>>a[i];
}
template<typename T>
void getVecPair(vector<pair<T , T > >& a , int st, int en)
{
    int dif = (en > st ? 1 : -1);
    for(int  i = st ; i!= en ; i+= dif)
        cin>>a[i].first>>a[i].second;
}

//76149
template<int D, typename T>
struct Vec : public vector<Vec<D - 1, T>> {
  static_assert(D >= 1, "Vector dimension must be greater than zero!");
  template<typename... Args>
  Vec(int n = 0, Args... args) : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {
  }
};
template<typename T>
struct Vec<1, T> : public vector<T> {
  Vec(int n = 0, T val = T()) : vector<T>(n, val) {
  }
};

const ll MOD = 1e9 + 7;
struct Graph
{
    int n , m;
    vector<vector<int> > adj;
    vector<ll> p , sz;
    vector<ll> edgeFrequency;

    Graph(int n)
    {
        this->n = n;
        adj.resize(n +  1);
        sz.resize( n + 1);
    }
    void addEdge(int u , int v)
    {
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    void dfs(int u , int p )
    {
        sz[u] = 1;
        for(auto v : adj[u])
        {
            if(v!= p)
            {
                dfs(v , u);
                sz[u] += sz[v];
            }
        }
    }

    void dfsReroot(int u , int p )
    {
        for(auto v: adj[u])
        {
            if(v!= p)
            {
                sz[u] -= sz[v];
                edgeFrequency.push_back( sz[u]*sz[v]);
                sz[v] += sz[u];
                dfsReroot(v , u);
                sz[v] -= sz[u];
                sz[u] += sz[v];
            }
        }
    }
    void solve()
    {
        dfs(1, - 1);
        dfsReroot(1 , -1);
        sort(edgeFrequency.rbegin() , edgeFrequency.rend());
        ll ans = 0;

        multiset<ll , greater<ll> > divisors;
        for(auto& x: p)
            divisors.insert(x);
        while(divisors.size() < n - 1)
            divisors.insert(1LL);

        while(divisors.size() > n - 1)
        {
            ll tmp = *divisors.begin();
            divisors.erase(divisors.begin());
            auto it = divisors.begin();
            tmp = (tmp* (*it))%MOD;
            divisors.erase(it);
            divisors.insert(tmp);
        }

        int idx = 0;
        for(auto& x : divisors)
            ans = (ans + (x*edgeFrequency[idx++])%MOD)%MOD;

        cout<<ans<<"\n";
    }
};

int main()
{
    xlr8;
    int testCases  = 1;
    cin>>testCases;
    while (testCases--)
    {
        int n;
        cin>>n;
        Graph g(n);
        for(int i = 0 ; i< n - 1; i++)
        {
            int u , v;
            cin>>u>>v;
            g.addEdge(u , v);
        }
        cin>>g.m;
        for(int i = 0 ; i < g.m; i++)
            {
                ll x;
                cin>>x;
                g.p.emplace_back(x);
            }
        g.solve();
    }
}


//https://2no.co/2bjRx5
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#pragma GCC target("popcnt")  //https://godbolt.org/z/yQv94Y

typedef long long int ll;
typedef unsigned long long int  ull;
typedef long double ld;

const ld pi = 4.0*atan(1.0);
#define xlr8 ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout.precision(10); cout<<fixed

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
// T.insert(a), T.erase(a), T.size()
// T.order_of_key(a) -- number of elements strictly less than a
// *T.find_by_order(k) -- k-th element in increasing order

struct Timer
{   chrono::time_point<chrono::high_resolution_clock> start , end; chrono:: duration<float> dur;
    Timer(){start = chrono::high_resolution_clock::now();}
    ~Timer(){   end = chrono::high_resolution_clock::now(); dur = end - start;  cout<<"\nTime Elapsed : " <<dur.count() << " s\n";  }
};

template<typename T>
void getVec(vector<T>& a , int st, int en)
{
    int dif = (en > st ? 1 : -1);
    for(int  i = st ; i!= en ; i+= dif)
        cin>>a[i];
}
template<typename T>
void getVecPair(vector<pair<T , T > >& a , int st, int en)
{
    int dif = (en > st ? 1 : -1);
    for(int  i = st ; i!= en ; i+= dif)
        cin>>a[i].first>>a[i].second;
}

//76149
template<int D, typename T>
struct Vec : public vector<Vec<D - 1, T>> {
  static_assert(D >= 1, "Vector dimension must be greater than zero!");
  template<typename... Args>
  Vec(int n = 0, Args... args) : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {
  }
};
template<typename T>
struct Vec<1, T> : public vector<T> {
  Vec(int n = 0, T val = T()) : vector<T>(n, val) {
  }
};

const ll MOD = 1000000007;
struct Graph
{
    int n , m;
    vector<vector<int> > adj;
    vector<ll> p , sz;
    vector<ll> edgeFrequency;

    Graph(int n)
    {
        this->n = n;
        adj.resize(n +  1);
        sz.resize( n + 1);
    }
    void addEdge(int u , int v)
    {
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    void dfs(int u , int p )
    {
        sz[u] = 1;
        for(auto v : adj[u])
        {
            if(v!= p)
            {
                dfs(v , u);
                sz[u] += sz[v];
            }
        }
    }

    void dfsReroot(int u , int p )
    {
        for(auto v: adj[u])
        {
            if(v!= p)
            {
                sz[u] -= sz[v];
                edgeFrequency.push_back( sz[u]*sz[v]);
                sz[v] += sz[u];
                dfsReroot(v , u);
                sz[v] -= sz[u];
                sz[u] += sz[v];
            }
        }
    }
    void solve()
    {
        dfs(1, - 1);
        dfsReroot(1 , -1);
        assert(edgeFrequency.size() + 1 == n);
        sort(edgeFrequency.rbegin() , edgeFrequency.rend());
        ll ans = 0;

        multiset<ll , greater<ll> > divisors;

        for(auto& x: p)
            divisors.insert(x);
        while(divisors.size() < n - 1)
            divisors.insert(1LL);
        while(divisors.size() > n - 1)
        {
            ll tmp = *divisors.begin();
            divisors.erase(divisors.begin());
            auto it = divisors.begin();
            tmp = (tmp* (*it))%MOD;
            divisors.erase(it);
            divisors.insert(tmp);
        }

        int idx = 0;
        for(auto& x : divisors)
            ans = (ans + (x*edgeFrequency[idx++])%MOD)%MOD;

        cout<<ans<<"\n";
    }
};

int main()
{
    xlr8;
    int testCases  = 1;
    cin>>testCases;
    while (testCases--)
    {
        int n;
        cin>>n;
        Graph g(n);
        for(int i = 0 ; i< n - 1; i++)
        {
            int u , v;
            cin>>u>>v;
            g.addEdge(u , v);
        }
        cin>>g.m;
        for(int i = 0 ; i < g.m; i++)
            {
                ll x;
                cin>>x;
                g.p.emplace_back(x);
            }
        g.solve();
    }
}


//https://2no.co/2bjRx5
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#pragma GCC target("popcnt")  //https://godbolt.org/z/yQv94Y

typedef long long int ll;
typedef unsigned long long int  ull;
typedef long double ld;

const ld pi = 4.0*atan(1.0);
#define xlr8 ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout.precision(10); cout<<fixed

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
// T.insert(a), T.erase(a), T.size()
// T.order_of_key(a) -- number of elements strictly less than a
// *T.find_by_order(k) -- k-th element in increasing order

struct Timer
{   chrono::time_point<chrono::high_resolution_clock> start , end; chrono:: duration<float> dur;
    Timer(){start = chrono::high_resolution_clock::now();}
    ~Timer(){   end = chrono::high_resolution_clock::now(); dur = end - start;  cout<<"\nTime Elapsed : " <<dur.count() << " s\n";  }
};

template<typename T>
void getVec(vector<T>& a , int st, int en)
{
    int dif = (en > st ? 1 : -1);
    for(int  i = st ; i!= en ; i+= dif)
        cin>>a[i];
}
template<typename T>
void getVecPair(vector<pair<T , T > >& a , int st, int en)
{
    int dif = (en > st ? 1 : -1);
    for(int  i = st ; i!= en ; i+= dif)
        cin>>a[i].first>>a[i].second;
}

//76149
template<int D, typename T>
struct Vec : public vector<Vec<D - 1, T>> {
  static_assert(D >= 1, "Vector dimension must be greater than zero!");
  template<typename... Args>
  Vec(int n = 0, Args... args) : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {
  }
};
template<typename T>
struct Vec<1, T> : public vector<T> {
  Vec(int n = 0, T val = T()) : vector<T>(n, val) {
  }
};

const ll MOD = 1e9 + 7;
struct Graph
{
    int n , m;
    vector<vector<int> > adj;
    vector<ll> p , sz;
    vector<ll> edgeFrequency;

    Graph(int n)
    {
        this->n = n;
        adj.resize(n +  1);
        sz.resize( n + 1);
    }
    void addEdge(int u , int v)
    {
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    void dfs(int u , int p )
    {
        sz[u] = 1;
        for(auto v : adj[u])
        {
            if(v!= p)
            {
                dfs(v , u);
                sz[u] += sz[v];
            }
        }
    }

    void dfsReroot(int u , int p )
    {
        for(auto v: adj[u])
        {
            if(v!= p)
            {
                sz[u] -= sz[v];
                edgeFrequency.push_back( (sz[u]*sz[v])%MOD);
                sz[v] += sz[u];
                dfsReroot(v , u);
                sz[v] -= sz[u];
                sz[u] += sz[v];
            }
        }
    }
    void solve()
    {
        dfs(1, - 1);
        dfsReroot(1 , -1);
        sort(edgeFrequency.rbegin() , edgeFrequency.rend());
        ll ans = 0;

        multiset<ll , greater<ll> > divisors;
        for(auto& x: p)
            divisors.insert(x);
        while(divisors.size() < n - 1)
            divisors.insert(1LL);

        while(divisors.size() > n - 1)
        {
            ll tmp = *divisors.begin();
            divisors.erase(divisors.begin());
            auto it = divisors.begin();
            tmp = (tmp* (*it))%MOD;
            divisors.erase(it);
            divisors.insert(tmp);
        }

        int idx = 0;
        for(auto& x : divisors)
            ans = (ans + (x*edgeFrequency[idx++])%MOD)%MOD;

        cout<<ans<<"\n";
    }
};

int main()
{
    xlr8;
    int testCases  = 1;
    cin>>testCases;
    while (testCases--)
    {
        int n;
        cin>>n;
        Graph g(n);
        for(int i = 0 ; i< n - 1; i++)
        {
            int u , v;
            cin>>u>>v;
            g.addEdge(u , v);
        }
        cin>>g.m;
        for(int i = 0 ; i < g.m; i++)
            {
                int x;
                cin>>x;
                g.p.emplace_back(x);
            }
        g.solve();
    }
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<set>
#define ll long long
using namespace std;
const int maxn = 201000;
const int inf = 0x3f3f3f3f;
#define fr(i,r,l) for(int i=r;i<=l;i++)
int a[maxn];
int main()
{
	int n, t;
	cin >> n >> t;
	int lazy = 0;
	memset(a, 0, sizeof(a));
	int b[1010][5];
	fr(i, 1 ,t)
	{
		scanf("%d %d %d", &b[i][0], &b[i][1], &b[i][2]);
		if (b[i][0]==1)
		{
			fr(j, b[i][1] + 1, b[i][2])
			{
				a[j] = 1;
			}
		}
	}
	fr(i, 1, t)
	{
		if (b[i][0]==0)
		{
			bool p = 0;
			fr(j, b[i][1] + 1, b[i][2])
			{
				if (a[j] == 0) {
					p = 1;
					break;
				}
			}
			if (p == 0)
			{
				printf("NO\n");
				return 0;
			}
		}
	}
	a[1] = 9999;
	printf("YES\n");
	fr(i, 2, n)
	{
		if (a[i]==1)
		{
			a[i] = a[i-1] ;
		}
		else
		{
			a[i] = a[i - 1] - 1;
		}
	}
	fr(i, 1, n)printf("%d ", a[i]);
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<set>
#define ll long long
using namespace std;
const int maxn = 201000;
const int inf = 0x3f3f3f3f;
int a[maxn];
int main()
{
	int n, t;
	cin >> n >> t;
	for (int i = 1;i <= n;i++)
	{
		a[i] = i;
	}
	int lazy = 0;
	int flag[maxn];
	memset(flag, 0, sizeof(flag));
	while (t--)
	{
		int x, y, z;
		cin >> x >> y >> z;
		if (x == 1)
		{
			for (int i = y+1;i < z;i++)
			{
				if (a[i] != i) {
					lazy = 1;
				}
				if (lazy == 1)break;
				if (i > y && i < z)
				{
					flag[i] = 1;
				}
			}
		}
		if (x == 0)
		{
			if (flag[y + 1] == 1)lazy = 1;
			if(a[y+1]==y+1)
			a[y + 1] = a[y ] - 1;
		}
	}
	if (lazy == 1)printf("NO\n");
	else
	{
		printf("YES\n");
		for (int i = 1;i <= n;i++)
		{
			printf("%d", a[i]);
			if (i != n)printf(" ");
		}
	}
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<set>
#define ll long long
using namespace std;
const int maxn = 201000;
const int inf = 0x3f3f3f3f;
#define fr(i,r,l) for(int i=r;i<=l;i++)
int a[maxn];
int main()
{
	int n, t;
	cin >> n >> t;
	int lazy = 0;
	memset(a, 0, sizeof(a));
	int b[1010][5];
	fr(i, 1 ,t)
	{
		scanf("%d %d %d", &b[i][0], &b[i][1], &b[i][2]);
		if (b[i][0])
		{
			fr(j, b[i][1] + 1, b[i][2])
			{
				a[j] = 1;
			}
		}
	}
	fr(i, 1, t)
	{
		if (!b[i][0])
		{
			bool p = 0;
			fr(j, b[i][1] + 1, b[i][2])
			{
				if (a[j] == 0)
					p = 1;
				break;
			}
			if (p == 0)
			{
				printf("NO\n");
				return 0;
			}
		}
	}
	a[1] = 9999;
	printf("YES\n");
	fr(i, 2, n)
	{
		if (a[i]==1)
		{
			a[i] = a[i-1] ;
		}
		else
		{
			a[i] = a[i - 1] - 1;
		}
	}
	fr(i, 1, n)printf("%d ", a[i]);
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<set>
#define ll long long
using namespace std;
const int maxn = 201000;
const int inf = 0x3f3f3f3f;
int a[maxn];
int main()
{
	int n, t;
	cin >> n >> t;
	for (int i = 1;i <= n;i++)
	{
		a[i] = i;
	}
	int lazy = 0;
	int flag[maxn];
	memset(flag, 0, sizeof(flag));
	while (t--)
	{
		int x, l, r;
		cin >> x >> l >> r;
		if (lazy == 1)continue;
		if (x == 1)
		{
			for (int i = l+1;i <= r;i++)
			{
				if (flag[i] == 2)
				{
					lazy = 1;
				}
				if (a[i] != i)
				{
					lazy = 1;
					break;
				}
				flag[i] = 1;
			}
		}
		if (x == 0)
		{
			for (int i = l+1;i <= r;i++)
			{
				if (flag[i] == 1)
				{
					lazy = 1;
					break;
				}
				flag[i] = 2;
				a[i] = i - 2;
			}
		}
	}
	if (lazy == 1)printf("NO\n");
	else
	{
		printf("YES\n");
		for (int i = 1;i <= n;i++)
		{
			printf("%d", a[i]);
			if (i != n)printf(" ");
		}
	}
}