#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
#define MAXN 100100
#define MOD 1000000007
#define LL long long
struct node
{
	int v;
	node *nnext;
} pool[MAXN*10], *h[MAXN*10];
int rread()
{
	int ret = 0;
	char c = getchar();
	while (c < '0' || c > '9')
		c = getchar();
	do
	{
		ret = ret * 10 + c - '0';
		c = getchar();
	} while ('0' <= c && c <= '9');
	return ret;
}
int cnt = -1;
void addedge(int u, int v)
{
	node *p = &pool[++cnt];
	p->v = v;
	p->nnext = h[u];
	h[u] = p;
	node *q = &pool[++cnt];
	q->v = u;
	q->nnext = h[v];
	h[v] = q;
}
bool vis[MAXN];
LL ssize[MAXN], wgt[MAXN];
void init(int u)
{
	//printf("%d\n",u);
	int v;
	if (vis[u] == true)
		return;
	vis[u] = true;
	ssize[u] = 1;
	for (node *p = h[u]; p; p = p->nnext)
	{
		v = p->v;
		if(vis[v]!=true)
		{
			init(v);
			ssize[u] += ssize[v];
		}
	}
}
int n, m;
LL eedge[MAXN];
int main()
{
	LL ans;
	int t, u, v;
	t = rread();
	while (t--)
	{
		cnt=-1;
		n = rread();
		for (int i = 1; i <= n; i++)
		{
			vis[i] = false;
			ssize[i]=0;
			eedge[i]=0;
		}
		for (int i = 1; i < n; i++)
		{
			u = rread();
			v = rread();
			addedge(u, v);
		}
		init(1);
		//for(int i=1;i<=n;i++)printf("%d ",ssize[i]);
		//printf("\n");
		for (int i = 2; i <= n; i++)
			eedge[i-1] = ((ssize[i] % MOD) * (n - ssize[i]) % MOD)%MOD;
		//for(int i=1;i<n;i++)printf("%d ",eedge[i]);
		sort(eedge+1, eedge + n);
		m = rread();
		for (int i = 1; i <= m; i++)
			wgt[i] = rread();
		sort(wgt+1, wgt + m + 1);
		ans = 0;
		if(m<=n-1)
		{
			for (int i = n-1; i >=1; i--)
			{
				if (i >= n-m)
				{
				//printf("%d %d\n",eedge[i],wgt[m-(n-1-i)]);
					ans = (ans + eedge[i] * wgt[m-(n-1-i)] % MOD) % MOD;
				}
				else
					ans = (ans + eedge[i]) % MOD;
			}
		}
		else
		{
			for(int i=m;i>=n;i--)
				wgt[n-1]=(wgt[n-1]*wgt[i])%MOD;
			for(int i=n-1;i>=1;i--)
				ans=(ans+wgt[i]*eedge[i]%MOD)%MOD;
		}
		printf("%lld\n", ans%MOD);
		for (int i = 0; i <= 2*n + 100; i++)
		{
			pool[i].v = 0;
			pool[i].nnext = NULL;
			h[i] = NULL;
		}
	}
	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
#define MAXN 100100
#define MOD 1000000007
#define LL long long
struct node
{
	int v;
	node *nnext;
} pool[MAXN], *h[MAXN];
int rread()
{
	int ret = 0;
	char c = getchar();
	while (c < '0' || c > '9')
		c = getchar();
	do
	{
		ret = ret * 10 + c - '0';
		c = getchar();
	} while ('0' <= c && c <= '9');
	return ret;
}
int cnt = -1;
void addedge(int u, int v)
{
	node *p = &pool[++cnt];
	p->v = v;
	p->nnext = h[u];
	h[u] = p;
	node *q = &pool[++cnt];
	q->v = u;
	q->nnext = h[v];
	h[v] = q;
}
bool vis[MAXN];
LL ssize[MAXN], wgt[MAXN];
void init(int u)
{
	//printf("%d\n",u);
	int v;
	if (vis[u] == true)
		return;
	vis[u] = true;
	ssize[u] = 1;
	for (node *p = h[u]; p; p = p->nnext)
	{
		v = p->v;
		if(vis[v]!=true)
		{
			init(v);
			ssize[u] += ssize[v];
		}
	}
}
int n, m;
LL eedge[MAXN];
int main()
{
	LL ans;
	int t, u, v;
	t = rread();
	while (t--)
	{
		cnt=-1;
		n = rread();
		for (int i = 1; i <= n; i++)
		{
			vis[i] = false;
			ssize[i]=0;
			eedge[i]=0;
		}
		for (int i = 1; i < n; i++)
		{
			u = rread();
			v = rread();
			addedge(u, v);
		}
		init(1);
		//for(int i=1;i<=n;i++)printf("%d ",ssize[i]);
		//printf("\n");
		for (int i = 2; i <= n; i++)
			eedge[i-1] = ((ssize[i] % MOD) * (n - ssize[i]) % MOD)%MOD;
		//for(int i=1;i<n;i++)printf("%d ",eedge[i]);
		sort(eedge+1, eedge + n);
		m = rread();
		for (int i = 1; i <= m; i++)
			wgt[i] = rread();
		sort(wgt+1, wgt + m + 1);
		ans = 0;
		if(m<=n-1)
		{
			for (int i = n-1; i >=1; i--)
			{
				if (i >= n-m)
				{
				//printf("%d %d\n",eedge[i],wgt[m-(n-1-i)]);
					ans = (ans + eedge[i] * wgt[m-(n-1-i)] % MOD) % MOD;
				}
				else
					ans = (ans + eedge[i]) % MOD;
			}
		}
		else
		{
			for(int i=m;i>=n;i--)
				wgt[n-1]=(wgt[n-1]*wgt[i])%MOD;
			for(int i=n-1;i>=1;i--)
				ans=(ans+wgt[i]*eedge[i]%MOD)%MOD;
		}
		printf("%lld\n", ans%MOD);
		for (int i = 0; i <= n + 100; i++)
		{
			pool[i].v = 0;
			pool[i].nnext = NULL;
			h[i] = NULL;
		}
	}
	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
#define MAXN 100100
#define MOD 1000000007
#define LL long long
struct node
{
	int v;
	node *nnext;
} pool[MAXN], *h[MAXN];
int rread()
{
	int ret = 0;
	char c = getchar();
	while (c < '0' || c > '9')
		c = getchar();
	do
	{
		ret = ret * 10 + c - '0';
		c = getchar();
	} while ('0' <= c && c <= '9');
	return ret;
}
int cnt = -1;
void addedge(int u, int v)
{
	node *p = &pool[++cnt];
	p->v = v;
	p->nnext = h[u];
	h[u] = p;
	node *q = &pool[++cnt];
	q->v = u;
	q->nnext = h[v];
	h[v] = q;
}
bool vis[MAXN];
LL ssize[MAXN], wgt[MAXN];
void init(int u)
{
	//printf("%d\n",u);
	int v;
	if (vis[u] == true)
		return;
	vis[u] = true;
	ssize[u] = 1;
	for (node *p = h[u]; p; p = p->nnext)
	{
		v = p->v;
		if(vis[v]!=true)
		{
			init(v);
			ssize[u] += ssize[v];
		}
	}
}
int n, m;
LL eedge[MAXN];
int main()
{
	LL ans;
	int t, u, v;
	t = rread();
	while (t--)
	{
		n = rread();
		for (int i = 1; i <= n; i++)
		{
			vis[i] = false;
			ssize[i]=0;
			eedge[i]=0;
		}
		for (int i = 1; i < n; i++)
		{
			u = rread();
			v = rread();
			addedge(u, v);
		}
		init(1);
		//for(int i=1;i<=n;i++)printf("%d ",ssize[i]);
		//printf("\n");
		for (int i = 2; i <= n; i++)
			eedge[i-1] = ((ssize[i] % MOD) * (n - ssize[i]) % MOD)%MOD;
		//for(int i=1;i<n;i++)printf("%d ",eedge[i]);
		sort(eedge+1, eedge + n);
		m = rread();
		for (int i = 1; i <= m; i++)
			wgt[i] = rread();
		sort(wgt+1, wgt + m + 1);
		ans = 0;
		if(m<=n-1)
		{
			for (int i = n-1; i >=1; i--)
			{
				if (i >= n-m)
				{
				//printf("%d %d\n",eedge[i],wgt[m-(n-1-i)]);
					ans = (ans + eedge[i] * wgt[m-(n-1-i)] % MOD) % MOD;
				}
				else
					ans = (ans + eedge[i]) % MOD;
			}
		}
		else
		{
			for(int i=m;i>=n;i--)
				wgt[n-1]=(wgt[n-1]*eedge[i])%MOD;
			for(int i=n-1;i>=1;i--)
				ans=(ans+wgt[i]*eedge[i]%MOD)%MOD;
		}
		printf("%lld\n", ans%MOD);
		for (int i = 0; i <= n + 100; i++)
		{
			pool[i].v = 0;
			pool[i].nnext = NULL;
			h[i] = NULL;
		}
	}
	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
#define MAXN 200100
#define MOD 1000000007
#define LL long long
struct node
{
	int v;
	node *nnext;
} pool[MAXN*10], *h[MAXN*10];
int rread()
{
	int ret = 0;
	char c = getchar();
	while (c < '0' || c > '9')
		c = getchar();
	do
	{
		ret = ret * 10 + c - '0';
		c = getchar();
	} while ('0' <= c && c <= '9');
	return ret;
}
int cnt = -1;
void addedge(int u, int v)
{
	node *p = &pool[++cnt];
	p->v = v;
	p->nnext = h[u];
	h[u] = p;
	node *q = &pool[++cnt];
	q->v = u;
	q->nnext = h[v];
	h[v] = q;
}
bool vis[MAXN];
LL ssize[MAXN], wgt[MAXN];
void init(int u)
{
	//printf("%d\n",u);
	int v;
	if (vis[u] == true)
		return;
	vis[u] = true;
	ssize[u] = 1;
	for (node *p = h[u]; p; p = p->nnext)
	{
		v = p->v;
		if(vis[v]!=true)
		{
			init(v);
			ssize[u] += ssize[v];
		}
	}
}
LL n, m;
LL eedge[MAXN];
int main()
{
	LL ans,pdt;
	int t, u, v;
	t = rread();
	while (t--)
	{
		cnt=-1;
		n = rread();
		for (int i = 1; i <= n; i++)
		{
			vis[i] = false;
			ssize[i]=0;
			eedge[i]=0;
		}
		for (int i = 1; i < n; i++)
		{
			u = rread();
			v = rread();
			addedge(u, v);
		}
		init(1);
		//for(int i=1;i<=n;i++)printf("%d ",ssize[i]);
		//printf("\n");
		for (int i = 2; i <= n; i++)
			eedge[i-1] = ((ssize[i] % MOD) * (n - ssize[i]) % MOD)%MOD;
		//for(int i=1;i<n;i++)printf("%d ",eedge[i]);
		sort(eedge+1, eedge + n);
		m = rread();
		for (int i = 1; i <= m; i++)
			wgt[i] = rread();
		sort(wgt+1, wgt + m + 1);
		ans = 0;
		if(m<=n-1)
		{
			for (int i = n-1; i >=1; i--)
			{
				if (i >= n-m)
				{
				//printf("%d %d\n",eedge[i],wgt[m-(n-1-i)]);
					ans = (ans + eedge[i]%MOD * wgt[m-(n-1-i)] % MOD) % MOD;
				}
				else
					ans = (ans + eedge[i]) % MOD;
			}
		}
		else
		{
			pdt=1;
			for(int i=m;i>=n-1;i--)pdt=(pdt%MOD*wgt[i]%MOD)%MOD;
			ans=(ans+pdt%MOD*eedge[n-1]%MOD)%MOD;
			for(int i=n-2;i>=1;i--)
				ans=(ans+(wgt[i]%MOD)*eedge[i]%MOD)%MOD;
		}
		printf("%lld\n", ans%MOD);
		for (int i = 0; i <= 2*n + 100; i++)
		{
			pool[i].v = 0;
			pool[i].nnext = NULL;
			h[i] = NULL;
		}
	}
	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
#define MAXN 100100
#define MOD 1000000007
#define LL long long
struct node
{
	int v;
	node *nnext;
} pool[MAXN], *h[MAXN];
int rread()
{
	int ret = 0;
	char c = getchar();
	while (c < '0' || c > '9')
		c = getchar();
	do
	{
		ret = ret * 10 + c - '0';
		c = getchar();
	} while ('0' <= c && c <= '9');
	return ret;
}
int cnt = -1;
void addedge(int u, int v)
{
	node *p = &pool[++cnt];
	p->v = v;
	p->nnext = h[u];
	h[u] = p;
	node *q = &pool[++cnt];
	q->v = u;
	q->nnext = h[v];
	h[v] = q;
}
bool vis[MAXN];
int val[MAXN], ssize[MAXN], wgt[MAXN];
void init(int u)
{
	//printf("%d\n",u);
	int v;
	if (vis[u] == true)
		return;
	vis[u] = true;
	ssize[u] = 1;
	for (node *p = h[u]; p; p = p->nnext)
	{
		v = p->v;
		if(vis[v]!=true)
		{
			init(v);
			ssize[u] += ssize[v];
		}
	}
}
int n, m;
int eedge[MAXN];
int main()
{
	LL ans;
	int t, u, v;
	t = rread();
	while (t--)
	{
		n = rread();
		for (int i = 1; i <= n; i++)
		{
			vis[i] = false;
			ssize[i]=0;
			eedge[i]=0;
		}
		for (int i = 1; i < n; i++)
		{
			u = rread();
			v = rread();
			addedge(u, v);
		}
		init(1);
		//for(int i=1;i<=n;i++)printf("%d ",ssize[i]);
		//printf("\n");
		for (int i = 2; i <= n; i++)
			eedge[i-1] = (((LL)ssize[i] % MOD) * (((LL)n - ssize[i]) % MOD)) % MOD;
		//for(int i=1;i<n;i++)printf("%d ",eedge[i]);
		sort(eedge+1, eedge + n);
		m = rread();
		for (int i = 1; i <= m; i++)
			wgt[i] = rread();
		sort(wgt+1, wgt + m + 1);
		ans = 0;
		for (int i = n-1; i >=1; i--)
		{
			if (i >= n-m)
			{
				//printf("%d %d\n",eedge[i],wgt[m-(n-1-i)]);
				ans = (ans + (LL)eedge[i] * wgt[m-(n-1-i)] % MOD) % MOD;
			}
			else
				ans = (ans + eedge[i]) % MOD;
		}
		printf("%lld\n", ans%MOD);
		for (int i = 0; i <= n + 100; i++)
		{
			pool[i].v = 0;
			pool[i].nnext = NULL;
			h[i] = NULL;
		}
	}
	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
#define MAXN 100100
#define MOD 1000000007
#define LL long long
struct node
{
	int v;
	node *nnext;
} pool[MAXN*10], *h[MAXN];
int rread()
{
	int ret = 0;
	char c = getchar();
	while (c < '0' || c > '9')
		c = getchar();
	do
	{
		ret = ret * 10 + c - '0';
		c = getchar();
	} while ('0' <= c && c <= '9');
	return ret;
}
int cnt = -1;
void addedge(int u, int v)
{
	node *p = &pool[++cnt];
	p->v = v;
	p->nnext = h[u];
	h[u] = p;
	node *q = &pool[++cnt];
	q->v = u;
	q->nnext = h[v];
	h[v] = q;
}
bool vis[MAXN];
LL ssize[MAXN], wgt[MAXN];
void init(int u)
{
	//printf("%d\n",u);
	int v;
	if (vis[u] == true)
		return;
	vis[u] = true;
	ssize[u] = 1;
	for (node *p = h[u]; p; p = p->nnext)
	{
		v = p->v;
		if(vis[v]!=true)
		{
			init(v);
			ssize[u] += ssize[v];
		}
	}
}
int n, m;
LL eedge[MAXN];
int main()
{
	LL ans;
	int t, u, v;
	t = rread();
	while (t--)
	{
		cnt=-1;
		n = rread();
		for (int i = 1; i <= n; i++)
		{
			vis[i] = false;
			ssize[i]=0;
			eedge[i]=0;
		}
		for (int i = 1; i < n; i++)
		{
			u = rread();
			v = rread();
			addedge(u, v);
		}
		init(1);
		//for(int i=1;i<=n;i++)printf("%d ",ssize[i]);
		//printf("\n");
		for (int i = 2; i <= n; i++)
			eedge[i-1] = ((ssize[i] % MOD) * (n - ssize[i]) % MOD)%MOD;
		//for(int i=1;i<n;i++)printf("%d ",eedge[i]);
		sort(eedge+1, eedge + n);
		m = rread();
		for (int i = 1; i <= m; i++)
			wgt[i] = rread();
		sort(wgt+1, wgt + m + 1);
		ans = 0;
		if(m<=n-1)
		{
			for (int i = n-1; i >=1; i--)
			{
				if (i >= n-m)
				{
				//printf("%d %d\n",eedge[i],wgt[m-(n-1-i)]);
					ans = (ans + eedge[i] * wgt[m-(n-1-i)] % MOD) % MOD;
				}
				else
					ans = (ans + eedge[i]) % MOD;
			}
		}
		else
		{
			for(int i=m;i>=n;i--)
				wgt[n-1]=(wgt[n-1]*wgt[i])%MOD;
			for(int i=n-1;i>=1;i--)
				ans=(ans+wgt[i]*eedge[i]%MOD)%MOD;
		}
		printf("%lld\n", ans%MOD);
		for (int i = 0; i <= n + 100; i++)
		{
			pool[i].v = 0;
			pool[i].nnext = NULL;
			h[i] = NULL;
		}
	}
	return 0;
}


/*
* Created by Jacky Wijaya
*/

#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <stack>
#include <set>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <functional>
#include <map>

#define FOREACH(it, c) for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)

#define ALL(c) (c).begin(), (c).end()
#define F first
#define S second
#define INS insert
#define MP make_pair
#define PB push_back
#define LL long long
#define ULL unsigned long long

using namespace std;

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<vii> vvii;

template <class T>
inline T MAX(T a, T b)
{
  if (a > b)
    return a;
  return b;
}
template <class T>
inline T MIN(T a, T b)
{
  if (a < b)
    return a;
  return b;
}
template <class T>
inline T ABS(T x)
{
  if (x < 0)
    return -x;
  return x;
}

inline void OPEN(const string &s)
{
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}

const static int INF = 2123123123;

#define MAXN 100005
#define MOD 1000000007

int bottom[MAXN];
bool visited[MAXN];
vvii ver(MAXN);
LL edges[MAXN];

int fillBotomNodes(int curr, int totalNodesParent)
{
  if (bottom[curr] != -1)
    return 0;
  bottom[curr] = 1;
  REP(i, ver[curr].size())
  {
    bottom[curr] += fillBotomNodes(ver[curr][i].F, bottom[curr]);
  }
  return bottom[curr];
}

void dfs(int currNode, int n)
{
  if (visited[currNode])
    return;
  visited[currNode] = true;
  REP(i, ver[currNode].size())
  {
    int w = ver[currNode][i].F;
    if (visited[w])
      continue;
    int idxEdge = ver[currNode][i].S;
    edges[idxEdge] += ((n - bottom[w]) * bottom[w]) % MOD;
    edges[idxEdge] %= MOD;
    dfs(w, n);
  }
}

int main()
{
  int t;
  scanf("%d", &t);
  REPN(tc, t)
  {
    int n, a, b;
    scanf("%d", &n);

    REPN(i, n)
    {
      bottom[i] = -1;
      ver[i].clear();
      edges[i - 1] = 0;
    }

    REP(i, n - 1)
    {
      scanf("%d%d", &a, &b);
      ver[a].PB(MP(b, i));
      ver[b].PB(MP(a, i));
    }
    fillBotomNodes(1, 0);

    memset(visited, false, sizeof(visited));
    dfs(1, n);

    int m;
    LL p[MAXN];
    scanf("%d", &m);
    REP(i, m)
    {
      scanf("%I64d", &p[i]);
    }
    if (m < n - 1)
    {
      for (int i = m; i < n - 1; i++)
      {
        p[i] = 1;
      }
    }
    sort(edges, edges + n - 1);
    sort(p, p + max(n - 1, m));

    int l = max(m, n - 1);

    if (m > n - 1)
    {
      for (int i = n - 1; i < m; i++)
      {
        p[n - 2] = (p[n - 2] * p[i]) % MOD;
        p[i] = 0;
      }
    }
    LL res = 0;
    REP(i, n - 1)
    {
      LL curr;
      curr = (p[i] * edges[i]) % MOD;
      res += curr;
      res %= MOD;
    }
    printf("%I64d\n", res);
  }
  return 0;
}

/*
1
4
1 2
2 3
3 4
5
2 3 4 5 6

1
16
5 10
16 1
14 1
7 5
13 2
16 11
1 7
1 4
3 14
8 16
1 6
4 9
4 12
5 13
1 15
18
45893 9901 51229 15511 46559 28433 4231 30241 29837 34421 12953 6577 12143 52711 40493 89 34819 28571
*/


/*
* Created by Jacky Wijaya
*/

#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <stack>
#include <set>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <functional>
#include <map>

#define FOREACH(it, c) for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)

#define ALL(c) (c).begin(), (c).end()
#define F first
#define S second
#define INS insert
#define MP make_pair
#define PB push_back
#define LL long long
#define ULL unsigned long long

using namespace std;

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<vii> vvii;

template <class T>
inline T MAX(T a, T b)
{
  if (a > b)
    return a;
  return b;
}
template <class T>
inline T MIN(T a, T b)
{
  if (a < b)
    return a;
  return b;
}
template <class T>
inline T ABS(T x)
{
  if (x < 0)
    return -x;
  return x;
}

inline void OPEN(const string &s)
{
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}

const static int INF = 2123123123;

#define MAXN 100005
#define MOD 1000000007

int bottom[MAXN];
bool visited[MAXN];
vvii ver(MAXN);
LL edges[MAXN];

int fillBotomNodes(int curr, int totalNodesParent)
{
  if (bottom[curr] != -1)
    return 0;
  bottom[curr] = 1;
  REP(i, ver[curr].size())
  {
    bottom[curr] += fillBotomNodes(ver[curr][i].F, bottom[curr]);
  }
  return bottom[curr];
}

void dfs(int currNode, int n)
{
  if (visited[currNode])
    return;
  visited[currNode] = true;
  REP(i, ver[currNode].size())
  {
    int w = ver[currNode][i].F;
    if (visited[w])
      continue;
    int idxEdge = ver[currNode][i].S;
    edges[idxEdge] += (n - bottom[w]) * bottom[w];
    dfs(w, n);
  }
}

int main()
{
  int t;
  scanf("%d", &t);
  REPN(tc, t)
  {
    int n, a, b;
    scanf("%d", &n);

    REPN(i, n)
    {
      bottom[i] = -1;
      ver[i].clear();
      edges[i - 1] = 0;
    }

    REP(i, n - 1)
    {
      scanf("%d%d", &a, &b);
      ver[a].PB(MP(b, i));
      ver[b].PB(MP(a, i));
    }
    fillBotomNodes(1, 0);

    memset(visited, false, sizeof(visited));
    dfs(1, n);

    int m;
    LL p[MAXN];
    scanf("%d", &m);
    REP(i, m)
    {
      scanf("%I64d", &p[i]);
    }
    if (m < n - 1)
    {
      for (int i = m; i < n - 1; i++)
      {
        p[i] = 1;
      }
    }
    sort(edges, edges + n - 1);
    sort(p, p + max(n - 1, m));

    int l = max(m, n - 1);

    if (m > n - 1)
    {
      for (int i = n - 1; i < m; i++)
      {
        p[n - 2] = (p[n - 2] * p[i]) % MOD;
        p[i] = 0;
      }
    }
    LL res = 0;
    REP(i, n - 1)
    {
      LL curr;
      curr = (p[i] * edges[i]) % MOD;
      res += curr;
      res %= MOD;
    }
    printf("%I64d\n", res);
  }
  return 0;
}

/*
1
4
1 2
2 3
3 4
5
2 3 4 5 6

1
16
5 10
16 1
14 1
7 5
13 2
16 11
1 7
1 4
3 14
8 16
1 6
4 9
4 12
5 13
1 15
18
45893 9901 51229 15511 46559 28433 4231 30241 29837 34421 12953 6577 12143 52711 40493 89 34819 28571
*/


/*
* Created by Jacky Wijaya
*/

#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <stack>
#include <set>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <functional>
#include <map>

#define FOREACH(it, c) for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)

#define ALL(c) (c).begin(), (c).end()
#define F first
#define S second
#define INS insert
#define MP make_pair
#define PB push_back
#define LL long long
#define ULL unsigned long long

using namespace std;

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<vii> vvii;

template <class T>
inline T MAX(T a, T b)
{
  if (a > b)
    return a;
  return b;
}
template <class T>
inline T MIN(T a, T b)
{
  if (a < b)
    return a;
  return b;
}
template <class T>
inline T ABS(T x)
{
  if (x < 0)
    return -x;
  return x;
}

inline void OPEN(const string &s)
{
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}

const static int INF = 2123123123;

#define MAXN 60005
#define MOD 1000000007

int bottom[MAXN];
bool visited[MAXN];
vvii ver(MAXN);
LL edges[MAXN];

int fillBotomNodes(int curr, int totalNodesParent)
{
  if (bottom[curr] != -1)
    return 0;
  bottom[curr] = 1;
  REP(i, ver[curr].size())
  {
    bottom[curr] += fillBotomNodes(ver[curr][i].F, bottom[curr]);
  }
  return bottom[curr];
}

void dfs(int currNode, int n)
{
  if (visited[currNode])
    return;
  visited[currNode] = true;
  REP(i, ver[currNode].size())
  {
    int w = ver[currNode][i].F;
    if (visited[w])
      continue;
    int idxEdge = ver[currNode][i].S;
    edges[idxEdge] += (n - bottom[w]) * bottom[w];
    dfs(w, n);
  }
}

int main()
{
  int t;
  scanf("%d", &t);
  REPN(tc, t)
  {
    int n, a, b;
    scanf("%d", &n);

    REPN(i, n)
    {
      bottom[i] = -1;
      ver[i].clear();
      edges[i - 1] = 0;
    }

    REP(i, n - 1)
    {
      scanf("%d%d", &a, &b);
      ver[a].PB(MP(b, i));
      ver[b].PB(MP(a, i));
    }
    fillBotomNodes(1, 0);

    memset(visited, false, sizeof(visited));
    dfs(1, n);

    int m;
    LL p[MAXN];
    scanf("%d", &m);
    REP(i, m)
    {
      scanf("%I64d", &p[i]);
    }
    if (m < n - 1)
    {
      for (int i = m; i < n - 1; i++)
      {
        p[i] = 1;
      }
    }
    sort(edges, edges + n - 1);
    sort(p, p + max(n - 1, m));

    int l = max(m, n - 1);

    if (m > n - 1)
    {
      for (int i = n - 1; i < m; i++)
      {
        p[n - 2] = (p[n - 2] * p[i]) % MOD;
        p[i] = 0;
      }
    }
    LL res = 0;
    REP(i, n - 1)
    {
      LL curr;
      curr = p[i] * edges[i];
      res += curr;
      res %= MOD;
    }
    printf("%I64d\n", res);
  }
  return 0;
}

/*
1
4
1 2
2 3
3 4
5
2 3 4 5 6

1
16
5 10
16 1
14 1
7 5
13 2
16 11
1 7
1 4
3 14
8 16
1 6
4 9
4 12
5 13
1 15
18
45893 9901 51229 15511 46559 28433 4231 30241 29837 34421 12953 6577 12143 52711 40493 89 34819 28571
*/


/*
* Created by Jacky Wijaya
*/

#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <stack>
#include <set>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <functional>
#include <map>

#define FOREACH(it, c) for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)

#define ALL(c) (c).begin(), (c).end()
#define F first
#define S second
#define INS insert
#define MP make_pair
#define PB push_back
#define LL long long
#define ULL unsigned long long

using namespace std;

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<vii> vvii;

template <class T>
inline T MAX(T a, T b)
{
  if (a > b)
    return a;
  return b;
}
template <class T>
inline T MIN(T a, T b)
{
  if (a < b)
    return a;
  return b;
}
template <class T>
inline T ABS(T x)
{
  if (x < 0)
    return -x;
  return x;
}

inline void OPEN(const string &s)
{
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}

const static int INF = 2123123123;

#define MAXN 60005
#define MOD 1000000007

int bottom[MAXN];
bool visited[MAXN];
vvii ver(MAXN);
int edges[MAXN];

int fillBotomNodes(int curr, int totalNodesParent)
{
  if (bottom[curr] != -1)
    return 0;
  bottom[curr] = 1;
  REP(i, ver[curr].size())
  {
    bottom[curr] += fillBotomNodes(ver[curr][i].F, bottom[curr]);
  }
  return bottom[curr];
}

void dfs(int currNode, int n)
{
  if (visited[currNode])
    return;
  visited[currNode] = true;
  REP(i, ver[currNode].size())
  {
    int w = ver[currNode][i].F;
    if (visited[w])
      continue;
    int idxEdge = ver[currNode][i].S;
    edges[idxEdge] += (n - bottom[w]) * bottom[w];
    dfs(w, n);
  }
}

int main()
{
  int t;
  scanf("%d", &t);
  REPN(tc, t)
  {
    int n, a, b;
    scanf("%d", &n);

    REPN(i, n)
    {
      bottom[i] = -1;
      ver[i].clear();
      edges[i - 1] = 0;
    }

    REP(i, n - 1)
    {
      scanf("%d%d", &a, &b);
      ver[a].PB(MP(b, i));
      ver[b].PB(MP(a, i));
    }
    fillBotomNodes(1, 0);

    memset(visited, false, sizeof(visited));
    dfs(1, n);

    int m, p;
    vi prime;
    scanf("%d", &m);
    REP(i, m)
    {
      scanf("%d", &p);
      prime.PB(p);
    }
    sort(edges, edges + n - 1);
    reverse(edges, edges + n - 1);
    sort(ALL(prime));
    reverse(ALL(prime));

    LL res = 0;
    REP(i, n - 1)
    {
      LL curr = 0;
      if (i < prime.size())
      {
        curr = prime[i] * edges[i];
      }
      else
      {
        curr = edges[i];
      }
      res += curr;
      res %= MOD;
    }
    printf("%I64d\n", res);
  }
  return 0;
}


/*
* Created by Jacky Wijaya
*/

#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <stack>
#include <set>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <functional>
#include <map>

#define FOREACH(it, c) for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)

#define ALL(c) (c).begin(), (c).end()
#define F first
#define S second
#define INS insert
#define MP make_pair
#define PB push_back
#define LL long long
#define ULL unsigned long long

using namespace std;

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<vii> vvii;

template <class T>
inline T MAX(T a, T b)
{
  if (a > b)
    return a;
  return b;
}
template <class T>
inline T MIN(T a, T b)
{
  if (a < b)
    return a;
  return b;
}
template <class T>
inline T ABS(T x)
{
  if (x < 0)
    return -x;
  return x;
}

inline void OPEN(const string &s)
{
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}

const static int INF = 2123123123;

#define MAXN 60005
#define MOD 1000000007

int bottom[MAXN];
bool visited[MAXN];
vvii ver(MAXN);
LL edges[MAXN];

int fillBotomNodes(int curr, int totalNodesParent)
{
  if (bottom[curr] != -1)
    return 0;
  bottom[curr] = 1;
  REP(i, ver[curr].size())
  {
    bottom[curr] += fillBotomNodes(ver[curr][i].F, bottom[curr]);
  }
  return bottom[curr];
}

void dfs(int currNode, int n)
{
  if (visited[currNode])
    return;
  visited[currNode] = true;
  REP(i, ver[currNode].size())
  {
    int w = ver[currNode][i].F;
    if (visited[w])
      continue;
    int idxEdge = ver[currNode][i].S;
    edges[idxEdge] += (n - bottom[w]) * bottom[w];
    dfs(w, n);
  }
}

int main()
{
  int t;
  scanf("%d", &t);
  REPN(tc, t)
  {
    int n, a, b;
    scanf("%d", &n);

    REPN(i, n)
    {
      bottom[i] = -1;
      ver[i].clear();
      edges[i - 1] = 0;
    }

    REP(i, n - 1)
    {
      scanf("%d%d", &a, &b);
      ver[a].PB(MP(b, i));
      ver[b].PB(MP(a, i));
    }
    fillBotomNodes(1, 0);

    memset(visited, false, sizeof(visited));
    dfs(1, n);

    int m;
    LL p[MAXN];
    scanf("%d", &m);
    REP(i, m)
    {
      scanf("%lld", &p[i]);
    }
    sort(edges, edges + n - 1);
    sort(p, p + m);

    int l = max(m, n - 1);

    for (int i = min(m, n - 1); i < l; i++)
    {
      if (m < n - 1)
        p[i] = 1;
      else
      {
        p[n - 2] = (p[n - 2] * p[i]) % MOD;
        p[i] = 0;
      }
    }

    LL res = 0;
    REP(i, n - 1)
    {
      LL curr = 0;
      if (i < m)
      {
        curr = p[i] * edges[i];
      }
      else
      {
        curr = edges[i];
      }
      res += curr;
      res %= MOD;
    }
    printf("%lld\n", res);
  }
  return 0;
}

/*
1
4
1 2
2 3
3 4
4
2 2 2 2

1
16
5 10
16 1
14 1
7 5
13 2
16 11
1 7
1 4
3 14
8 16
1 6
4 9
4 12
5 13
1 15
18
45893 9901 51229 15511 46559 28433 4231 30241 29837 34421 12953 6577 12143 52711 40493 89 34819 28571
*/


/*
* Created by Jacky Wijaya
*/

#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <stack>
#include <set>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <functional>
#include <map>

#define FOREACH(it, c) for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)

#define ALL(c) (c).begin(), (c).end()
#define F first
#define S second
#define INS insert
#define MP make_pair
#define PB push_back
#define LL long long
#define ULL unsigned long long

using namespace std;

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<vii> vvii;

template <class T>
inline T MAX(T a, T b)
{
  if (a > b)
    return a;
  return b;
}
template <class T>
inline T MIN(T a, T b)
{
  if (a < b)
    return a;
  return b;
}
template <class T>
inline T ABS(T x)
{
  if (x < 0)
    return -x;
  return x;
}

inline void OPEN(const string &s)
{
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}

const static int INF = 2123123123;

#define MAXN 60005
#define MOD 1000000007

int bottom[MAXN];
bool visited[MAXN];
vvii ver(MAXN);
LL edges[MAXN];

int fillBotomNodes(int curr, int totalNodesParent)
{
  if (bottom[curr] != -1)
    return 0;
  bottom[curr] = 1;
  REP(i, ver[curr].size())
  {
    bottom[curr] += fillBotomNodes(ver[curr][i].F, bottom[curr]);
  }
  return bottom[curr];
}

void dfs(int currNode, int n)
{
  if (visited[currNode])
    return;
  visited[currNode] = true;
  REP(i, ver[currNode].size())
  {
    int w = ver[currNode][i].F;
    if (visited[w])
      continue;
    int idxEdge = ver[currNode][i].S;
    edges[idxEdge] += (n - bottom[w]) * bottom[w];
    dfs(w, n);
  }
}

int main()
{
  int t;
  scanf("%d", &t);
  REPN(tc, t)
  {
    int n, a, b;
    scanf("%d", &n);

    REPN(i, n)
    {
      bottom[i] = -1;
      ver[i].clear();
      edges[i - 1] = 0;
    }

    REP(i, n - 1)
    {
      scanf("%d%d", &a, &b);
      ver[a].PB(MP(b, i));
      ver[b].PB(MP(a, i));
    }
    fillBotomNodes(1, 0);

    memset(visited, false, sizeof(visited));
    dfs(1, n);

    int m;
    LL p[MAXN];
    scanf("%d", &m);
    REP(i, m)
    {
      scanf("%I64d", &p[i]);
    }
    sort(edges, edges + n - 1);
    sort(p, p + m);

    int l = max(m, n - 1);

    for (int i = min(m, n - 1); i < l; i++)
    {
      if (m < n - 1)
        p[i] = 1;
      else
      {
        p[n - 2] = (p[n - 2] * p[i]) % MOD;
        p[i] = 0;
      }
    }

    LL res = 0;
    REP(i, n - 1)
    {
      LL curr = 0;
      if (i < m)
      {
        curr = p[i] * edges[i];
      }
      else
      {
        curr = edges[i];
      }
      res += curr;
      res %= MOD;
    }
    printf("%I64d\n", res);
  }
  return 0;
}

/*
1
4
1 2
2 3
3 4
4
2 2 2 2

1
16
5 10
16 1
14 1
7 5
13 2
16 11
1 7
1 4
3 14
8 16
1 6
4 9
4 12
5 13
1 15
18
45893 9901 51229 15511 46559 28433 4231 30241 29837 34421 12953 6577 12143 52711 40493 89 34819 28571
*/


/*
* Created by Jacky Wijaya
*/

#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <stack>
#include <set>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <functional>
#include <map>

#define FOREACH(it, c) for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)

#define ALL(c) (c).begin(), (c).end()
#define F first
#define S second
#define INS insert
#define MP make_pair
#define PB push_back
#define LL long long
#define ULL unsigned long long

using namespace std;

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<vii> vvii;

template <class T>
inline T MAX(T a, T b)
{
  if (a > b)
    return a;
  return b;
}
template <class T>
inline T MIN(T a, T b)
{
  if (a < b)
    return a;
  return b;
}
template <class T>
inline T ABS(T x)
{
  if (x < 0)
    return -x;
  return x;
}

inline void OPEN(const string &s)
{
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}

const static int INF = 2123123123;

#define MAXN 60005
#define MOD 1000000007

int bottom[MAXN];
bool visited[MAXN];
vvii ver(MAXN);
LL edges[MAXN];

int fillBotomNodes(int curr, int totalNodesParent)
{
  if (bottom[curr] != -1)
    return 0;
  bottom[curr] = 1;
  REP(i, ver[curr].size())
  {
    bottom[curr] += fillBotomNodes(ver[curr][i].F, bottom[curr]);
  }
  return bottom[curr];
}

void dfs(int currNode, int n)
{
  if (visited[currNode])
    return;
  visited[currNode] = true;
  REP(i, ver[currNode].size())
  {
    int w = ver[currNode][i].F;
    if (visited[w])
      continue;
    int idxEdge = ver[currNode][i].S;
    edges[idxEdge] += (n - bottom[w]) * bottom[w];
    dfs(w, n);
  }
}

int main()
{
  int t;
  scanf("%d", &t);
  REPN(tc, t)
  {
    int n, a, b;
    scanf("%d", &n);

    REPN(i, n)
    {
      bottom[i] = -1;
      ver[i].clear();
      edges[i - 1] = 0;
    }

    REP(i, n - 1)
    {
      scanf("%d%d", &a, &b);
      ver[a].PB(MP(b, i));
      ver[b].PB(MP(a, i));
    }
    fillBotomNodes(1, 0);

    memset(visited, false, sizeof(visited));
    dfs(1, n);

    int m, p;
    vector<LL> prime;
    scanf("%d", &m);
    REP(i, m)
    {
      scanf("%d", &p);
      prime.PB(p);
    }
    sort(edges, edges + n - 1);
    reverse(edges, edges + n - 1);
    sort(ALL(prime));
    reverse(ALL(prime));

    LL res = 0;
    REP(i, n - 1)
    {
      LL curr = 0;
      if (i < prime.size())
      {
        curr = prime[i] * edges[i];
      }
      else
      {
        curr = edges[i];
      }
      res += curr;
      res %= MOD;
    }
    printf("%I64d\n", res);
  }
  return 0;
}


/*
* Created by Jacky Wijaya
*/

#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <stack>
#include <set>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <functional>
#include <map>

#define FOREACH(it, c) for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)

#define ALL(c) (c).begin(), (c).end()
#define F first
#define S second
#define INS insert
#define MP make_pair
#define PB push_back
#define LL long long
#define ULL unsigned long long

using namespace std;

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<vii> vvii;

template <class T>
inline T MAX(T a, T b)
{
  if (a > b)
    return a;
  return b;
}
template <class T>
inline T MIN(T a, T b)
{
  if (a < b)
    return a;
  return b;
}
template <class T>
inline T ABS(T x)
{
  if (x < 0)
    return -x;
  return x;
}

inline void OPEN(const string &s)
{
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}

const static int INF = 2123123123;

#define MAXN 100005
#define MOD 1000000007

int bottom[MAXN];
bool visited[MAXN];
vvii ver(MAXN);
LL edges[MAXN];

int fillBotomNodes(int curr, int totalNodesParent)
{
  if (bottom[curr] != -1)
    return 0;
  bottom[curr] = 1;
  REP(i, ver[curr].size())
  {
    bottom[curr] += fillBotomNodes(ver[curr][i].F, bottom[curr]);
  }
  return bottom[curr];
}

void dfs(int currNode, int n)
{
  if (visited[currNode])
    return;
  visited[currNode] = true;
  REP(i, ver[currNode].size())
  {
    int w = ver[currNode][i].F;
    if (visited[w])
      continue;
    int idxEdge = ver[currNode][i].S;
    edges[idxEdge] += (n - bottom[w]) * bottom[w];
    dfs(w, n);
  }
}

int main()
{
  int t;
  scanf("%d", &t);
  REPN(tc, t)
  {
    int n, a, b;
    scanf("%d", &n);

    REPN(i, n)
    {
      bottom[i] = -1;
      ver[i].clear();
      edges[i - 1] = 0;
    }

    REP(i, n - 1)
    {
      scanf("%d%d", &a, &b);
      ver[a].PB(MP(b, i));
      ver[b].PB(MP(a, i));
    }
    fillBotomNodes(1, 0);

    memset(visited, false, sizeof(visited));
    dfs(1, n);

    int m;
    LL p[MAXN];
    scanf("%d", &m);
    REP(i, m)
    {
      scanf("%I64d", &p[i]);
    }
    if (m < n - 1)
    {
      for (int i = m; i < n - 1; i++)
      {
        p[i] = 1;
      }
    }
    sort(edges, edges + n - 1);
    sort(p, p + max(n - 1, m));

    int l = max(m, n - 1);

    if (m > n - 1)
    {
      for (int i = n - 1; i < m; i++)
      {
        p[n - 2] = (p[n - 2] * p[i]) % MOD;
        p[i] = 0;
      }
    }
    LL res = 0;
    REP(i, n - 1)
    {
      LL curr;
      curr = p[i] * edges[i];
      res += curr;
      res %= MOD;
    }
    printf("%I64d\n", res);
  }
  return 0;
}

/*
1
4
1 2
2 3
3 4
5
2 3 4 5 6

1
16
5 10
16 1
14 1
7 5
13 2
16 11
1 7
1 4
3 14
8 16
1 6
4 9
4 12
5 13
1 15
18
45893 9901 51229 15511 46559 28433 4231 30241 29837 34421 12953 6577 12143 52711 40493 89 34819 28571
*/


/*
* Created by Jacky Wijaya
*/

#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <stack>
#include <set>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <functional>
#include <map>

#define FOREACH(it, c) for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)

#define ALL(c) (c).begin(), (c).end()
#define F first
#define S second
#define INS insert
#define MP make_pair
#define PB push_back
#define LL long long
#define ULL unsigned long long

using namespace std;

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<vii> vvii;

template <class T>
inline T MAX(T a, T b)
{
  if (a > b)
    return a;
  return b;
}
template <class T>
inline T MIN(T a, T b)
{
  if (a < b)
    return a;
  return b;
}
template <class T>
inline T ABS(T x)
{
  if (x < 0)
    return -x;
  return x;
}

inline void OPEN(const string &s)
{
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}

const static int INF = 2123123123;

#define MAXN 60005
#define MOD 1000000007

int bottom[MAXN];
bool visited[MAXN];
vvii ver(MAXN);
LL edges[MAXN];

int fillBotomNodes(int curr, int totalNodesParent)
{
  if (bottom[curr] != -1)
    return 0;
  bottom[curr] = 1;
  REP(i, ver[curr].size())
  {
    bottom[curr] += fillBotomNodes(ver[curr][i].F, bottom[curr]);
  }
  return bottom[curr];
}

void dfs(int currNode, int n)
{
  if (visited[currNode])
    return;
  visited[currNode] = true;
  REP(i, ver[currNode].size())
  {
    int w = ver[currNode][i].F;
    if (visited[w])
      continue;
    int idxEdge = ver[currNode][i].S;
    edges[idxEdge] += (n - bottom[w]) * bottom[w];
    dfs(w, n);
  }
}

int main()
{
  int t;
  scanf("%d", &t);
  REPN(tc, t)
  {
    int n, a, b;
    scanf("%d", &n);

    REPN(i, n)
    {
      bottom[i] = -1;
      ver[i].clear();
      edges[i - 1] = 0;
    }

    REP(i, n - 1)
    {
      scanf("%d%d", &a, &b);
      ver[a].PB(MP(b, i));
      ver[b].PB(MP(a, i));
    }
    fillBotomNodes(1, 0);

    memset(visited, false, sizeof(visited));
    dfs(1, n);

    int m;
    LL p[MAXN];
    scanf("%d", &m);
    REP(i, m)
    {
      scanf("%I64d", &p[i]);
    }
    if (m < n - 1)
    {
      for (int i = m; i < n - 1; i++)
      {
        p[i] = 1;
      }
    }
    sort(edges, edges + n - 1);
    sort(p, p + n - 1);

    int l = max(m, n - 1);

    if (m > n - 1)
    {
      for (int i = n - 1; i < m; i++)
      {
        p[n - 2] = (p[n - 2] * p[i]) % MOD;
        p[i] = 0;
      }
    }
    // REP(i, n - 1)
    // {
    //   printf("p[%d] = %d\n", i, p[i]);
    //   printf("edges[%d] = %d\n", i, edges[i]);
    // }
    LL res = 0;
    REP(i, n - 1)
    {
      LL curr;
      curr = p[i] * edges[i];
      res += curr;
      res %= MOD;
    }
    printf("%I64d\n", res);
  }
  return 0;
}

/*
1
4
1 2
2 3
3 4
4
2 2 2 2

1
16
5 10
16 1
14 1
7 5
13 2
16 11
1 7
1 4
3 14
8 16
1 6
4 9
4 12
5 13
1 15
18
45893 9901 51229 15511 46559 28433 4231 30241 29837 34421 12953 6577 12143 52711 40493 89 34819 28571
*/


/*
* Created by Jacky Wijaya
*/

#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <stack>
#include <set>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <functional>
#include <map>

#define FOREACH(it, c) for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)

#define ALL(c) (c).begin(), (c).end()
#define F first
#define S second
#define INS insert
#define MP make_pair
#define PB push_back
#define LL long long
#define ULL unsigned long long

using namespace std;

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<vii> vvii;

template <class T>
inline T MAX(T a, T b)
{
  if (a > b)
    return a;
  return b;
}
template <class T>
inline T MIN(T a, T b)
{
  if (a < b)
    return a;
  return b;
}
template <class T>
inline T ABS(T x)
{
  if (x < 0)
    return -x;
  return x;
}

inline void OPEN(const string &s)
{
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}

const static int INF = 2123123123;

#define MAXN 100005
#define MOD 1000000007

LL bottom[MAXN];
bool visited[MAXN];
vvii ver(MAXN);
LL edges[MAXN];

int fillBotomNodes(int curr, int totalNodesParent)
{
  if (bottom[curr] != -1)
    return 0;
  bottom[curr] = 1;
  REP(i, ver[curr].size())
  {
    bottom[curr] += fillBotomNodes(ver[curr][i].F, bottom[curr]);
  }
  return bottom[curr];
}

void dfs(int currNode, int n)
{
  if (visited[currNode])
    return;
  visited[currNode] = true;
  REP(i, ver[currNode].size())
  {
    int w = ver[currNode][i].F;
    if (visited[w])
      continue;
    int idxEdge = ver[currNode][i].S;
    edges[idxEdge] += (((LL)n - bottom[w]) * bottom[w]) % MOD;
    edges[idxEdge] %= MOD;
    dfs(w, n);
  }
}

int main()
{
  int t;
  scanf("%d", &t);
  REPN(tc, t)
  {
    int n, a, b;
    scanf("%d", &n);

    REPN(i, n)
    {
      bottom[i] = -1;
      ver[i].clear();
      edges[i - 1] = 0;
    }

    REP(i, n - 1)
    {
      scanf("%d%d", &a, &b);
      ver[a].PB(MP(b, i));
      ver[b].PB(MP(a, i));
    }
    fillBotomNodes(1, 0);

    memset(visited, false, sizeof(visited));
    dfs(1, n);

    int m;
    LL p[MAXN];
    scanf("%d", &m);
    REP(i, m)
    {
      scanf("%I64d", &p[i]);
    }
    if (m < n - 1)
    {
      for (int i = m; i < n - 1; i++)
      {
        p[i] = 1;
      }
    }
    sort(edges, edges + n - 1);
    sort(p, p + max(n - 1, m));

    int l = max(m, n - 1);

    if (m > n - 1)
    {
      for (int i = n - 1; i < m; i++)
      {
        p[n - 2] = (p[n - 2] * p[i]) % MOD;
        p[i] = 0;
      }
    }
    LL res = 0;
    REP(i, n - 1)
    {
      LL curr;
      curr = (p[i] * edges[i]) % MOD;
      res += curr;
      res %= MOD;
    }
    printf("%I64d\n", res);
  }
  return 0;
}

/*
1
4
1 2
2 3
3 4
5
2 3 4 5 6

1
16
5 10
16 1
14 1
7 5
13 2
16 11
1 7
1 4
3 14
8 16
1 6
4 9
4 12
5 13
1 15
18
45893 9901 51229 15511 46559 28433 4231 30241 29837 34421 12953 6577 12143 52711 40493 89 34819 28571
*/


/*
* Created by Jacky Wijaya
*/

#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <stack>
#include <set>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <functional>
#include <map>

#define FOREACH(it, c) for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)

#define ALL(c) (c).begin(), (c).end()
#define F first
#define S second
#define INS insert
#define MP make_pair
#define PB push_back
#define LL long long
#define ULL unsigned long long

using namespace std;

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<vii> vvii;

template <class T>
inline T MAX(T a, T b)
{
  if (a > b)
    return a;
  return b;
}
template <class T>
inline T MIN(T a, T b)
{
  if (a < b)
    return a;
  return b;
}
template <class T>
inline T ABS(T x)
{
  if (x < 0)
    return -x;
  return x;
}

inline void OPEN(const string &s)
{
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}

const static int INF = 2123123123;

#define MAXN 100005
#define MOD 1000000007

LL bottom[MAXN];
bool visited[MAXN];
vvii ver(MAXN);
LL edges[MAXN];

LL fillBotomNodes(int curr, LL totalNodesParent)
{
  if (bottom[curr] != -1)
    return 0;
  bottom[curr] = 1;
  REP(i, ver[curr].size())
  {
    bottom[curr] += fillBotomNodes(ver[curr][i].F, bottom[curr]);
  }
  return bottom[curr];
}

void dfs(int currNode, int n)
{
  if (visited[currNode])
    return;
  visited[currNode] = true;
  REP(i, ver[currNode].size())
  {
    int w = ver[currNode][i].F;
    if (visited[w])
      continue;
    int idxEdge = ver[currNode][i].S;
    edges[idxEdge] += (((LL)n - bottom[w]) * bottom[w]);
    dfs(w, n);
  }
}

int main()
{
  int t;
  scanf("%d", &t);
  REPN(tc, t)
  {
    int n, a, b;
    scanf("%d", &n);

    REPN(i, n)
    {
      bottom[i] = -1;
      ver[i].clear();
      edges[i - 1] = 0;
    }

    REP(i, n - 1)
    {
      scanf("%d%d", &a, &b);
      ver[a].PB(MP(b, i));
      ver[b].PB(MP(a, i));
    }
    fillBotomNodes(1, 0);

    memset(visited, false, sizeof(visited));
    dfs(1, n);

    int m;
    LL p[MAXN];
    scanf("%d", &m);
    REP(i, m)
    {
      scanf("%I64d", &p[i]);
    }
    if (m < n - 1)
    {
      for (int i = m; i < n - 1; i++)
      {
        p[i] = 1;
      }
    }
    sort(edges, edges + n - 1);
    sort(p, p + max(n - 1, m));

    int l = max(m, n - 1);

    if (m > n - 1)
    {
      for (int i = n - 1; i < m; i++)
      {
        p[n - 2] = (p[n - 2] * p[i]) % MOD;
        p[i] = 0;
      }
    }
    LL res = 0;
    REP(i, n - 1)
    {
      LL curr;
      curr = (p[i] * edges[i]) % MOD;
      res += curr;
      res %= MOD;
    }
    printf("%I64d\n", res);
  }
  return 0;
}

/*
1
4
1 2
2 3
3 4
5
2 3 4 5 6

1
16
5 10
16 1
14 1
7 5
13 2
16 11
1 7
1 4
3 14
8 16
1 6
4 9
4 12
5 13
1 15
18
45893 9901 51229 15511 46559 28433 4231 30241 29837 34421 12953 6577 12143 52711 40493 89 34819 28571
*/


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
using namespace std;
#define mod 1000000007
template<typename T>
void read(T& x)
{
	int ch = getchar(); x = 0;
	bool f = false;
	while ((ch < '0' || ch>'9') && ch != '-')ch = getchar();
	if (ch == '-') { f = true; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	if (f) x = -x;
}
long long max(long long x, long long y)
{
	return x > y ? x : y;
}
long long min(long long x, long long y)
{
	return x > y ? y : x;
}
long long a[60500] = { 0 };
vector<long long>v[100050];
long long edg[100050][2] = { 0 };
long long lab[100050] = { 0 };
long long num[100050] = { 0 };
long long b[100050] = { 0 };
long long f(long long r)
{
	lab[r] = 1;
	for (auto e : v[r])
	{
		if (lab[e] == 0)
		{
			f(e);
			num[r] = num[r] + num[e];
		}
	}
	num[r]++;
	return 0;
}
bool cmp(long long x, long long y)
{
	return x > y;
}
int main()
{
	long long n = 0, m = 0, t = 0;
	long long i = 0, j = 0, k = 0;
	long long x = 0, y = 0, z = 0;
	read(t);
	while (t--)
	{
		read(n);
		for (i = 1; i < n; i++)
		{
			read(x); read(y);
			edg[i][0] = x;
			edg[i][1] = y;
			v[x].push_back(y);
			v[y].push_back(x);
		}
		read(m);
		for (i = 1; i <= m; i++)
		{
			read(a[i]);
		}
		sort(a + 1, a + m + 1, cmp);
		f(1);
		for (i = 1; i < n; i++)
		{
			b[i] = min(num[edg[i][0]], num[edg[i][1]]) * (n - min(num[edg[i][0]], num[edg[i][1]]));
		}
		sort(b + 1, b + n, cmp);
		long long ans = 0;
		for (i = 1; i < n; i++)
		{
			a[i] = a[i] % mod;
			b[i] = b[i] % mod;
			if (a[i] <= m)ans = ans + b[i] * a[i];
			else ans = ans + b[i];
			ans = ans % mod;
		}
		cout << ans % mod << endl;
		for (i = 0; i <= n; i++)
		{
			while (!v[i].empty())
			{
				v[i].pop_back();
			}
			num[i] = 0;
			lab[i] = 0;
			b[i] = 0;
			edg[i][0] = 0;
			edg[i][1] = 0;
		}
		for (i = 0; i <= m; i++)
		{
			a[i] = 0;
		}
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
using namespace std;
#define mod 1000000007
template<typename T>
void read(T& x)
{
	int ch = getchar(); x = 0;
	bool f = false;
	while ((ch < '0' || ch>'9') && ch != '-')ch = getchar();
	if (ch == '-') { f = true; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	if (f) x = -x;
}
long long max(long long x, long long y)
{
	return x > y ? x : y;
}
long long min(long long x, long long y)
{
	return x > y ? y : x;
}
long long a[100500] = { 0 };
vector<long long>v[100050];
long long edg[100050][2] = { 0 };
long long lab[100050] = { 0 };
long long num[100050] = { 0 };
long long b[100050] = { 0 };
long long f(long long r)
{
	lab[r] = 1;
	for (auto e : v[r])
	{
		if (lab[e] == 0)
		{
			f(e);
			num[r] = num[r] + num[e];
		}
	}
	num[r]++;
	return 0;
}
bool cmp(long long x, long long y)
{
	return x > y;
}
int main()
{
	long long n = 0, m = 0, t = 0;
	long long i = 0, j = 0, k = 0;
	long long x = 0, y = 0, z = 0;
	read(t);
	while (t--)
	{
		read(n);
		for (i = 1; i < n; i++)
		{
			read(x); read(y);
			edg[i][0] = x;
			edg[i][1] = y;
			v[x].push_back(y);
			v[y].push_back(x);
		}
		read(m);
		for (i = 1; i <= m; i++)
		{
			read(a[i]);
		}
		if (m < n) sort(a + 1, a + m + 1, cmp);
		else
		{
			sort(a + 1, a + m + 1);
			for (i = n; i <= m; i++)
			{
				a[n - 1] = a[n - 1] * a[i] % mod;
			}
			sort(a + 1, a + n, cmp);
		}
		f(1);
		for (i = 1; i < n; i++)
		{
			b[i] = min(num[edg[i][0]], num[edg[i][1]]) * (n - min(num[edg[i][0]], num[edg[i][1]]));
		}
		sort(b + 1, b + n, cmp);
		long long ans = 0;
		for (i = 1; i < n; i++)
		{
			a[i] = a[i] % mod;
			b[i] = b[i] % mod;
			if (i <= m) ans = ans + b[i] * a[i];
			else ans = ans + b[i];
			ans = ans % mod;
		}
		cout << ans % mod << endl;
		for (i = 0; i <= n + 49; i++)
		{
			while (!v[i].empty())
			{
				v[i].pop_back();
			}
			num[i] = 0;
			lab[i] = 0;
			b[i] = 0;
			edg[i][0] = 0;
			edg[i][1] = 0;
			a[i] = 0;
		}
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
using namespace std;
#define mod 1000000007
template<typename T>
void read(T& x)
{
	int ch = getchar(); x = 0;
	bool f = false;
	while ((ch < '0' || ch>'9') && ch != '-')ch = getchar();
	if (ch == '-') { f = true; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	if (f) x = -x;
}
long long max(long long x, long long y)
{
	return x > y ? x : y;
}
long long min(long long x, long long y)
{
	return x > y ? y : x;
}
long long a[6050] = { 0 };
vector<long long>v[100050];
long long edg[100050][2] = { 0 };
long long lab[100050] = { 0 };
long long num[100050] = { 0 };
long long b[100050] = { 0 };
long long f(long long r)
{
	lab[r] = 1;
	for (auto e : v[r])
	{
		if (lab[e] == 0)
		{
			f(e);
			num[r] = num[r] + num[e];
		}
	}
	num[r]++;
	return 0;
}
bool cmp(long long x, long long y)
{
	return x > y;
}
int main()
{
	long long n = 0, m = 0, t = 0;
	long long i = 0, j = 0, k = 0;
	long long x = 0, y = 0, z = 0;
	read(t);
	while (t--)
	{
		read(n);
		for (i = 1; i < n; i++)
		{
			read(x); read(y);
			edg[i][0] = x;
			edg[i][1] = y;
			v[x].push_back(y);
			v[y].push_back(x);
		}
		read(m);
		for (i = 1; i <= m; i++)
		{
			read(a[i]);
		}
		sort(a + 1, a + n + 1, cmp);
		f(1);
		for (i = 1; i < n; i++)
		{
			b[i] = min(num[edg[i][0]], num[edg[i][1]]) * (n - min(num[edg[i][0]], num[edg[i][1]]));
		}
		sort(b + 1, b + n, cmp);
		long long ans = 0;
		for (i = 1; i < n; i++)
		{
			if (a[i] != 0)ans = ans + b[i] * a[i];
			else ans = ans + b[i];
			ans = ans % mod;
		}
		cout << ans % mod << endl;
		for (i = 1; i <= n; i++)
		{
			while (!v[i].empty())
			{
				v[i].pop_back();
			}
			num[i] = 0;
			lab[i] = 0;
		}
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
using namespace std;
template<typename T>
void read(T& x)
{
	int ch = getchar(); x = 0;
	bool f = false;
	while ((ch < '0' || ch>'9') && ch != '-')ch = getchar();
	if (ch == '-') { f = true; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	if (f) x = -x;
}
long long max(long long x, long long y)
{
	return x > y ? x : y;
}
long long min(long long x, long long y)
{
	return x > y ? y : x;
}
long long a[6050] = { 0 };
vector<long long>v[100050];
long long edg[100050][2] = { 0 };
long long lab[100050] = { 0 };
long long num[100050] = { 0 };
long long b[100050] = { 0 };
long long f(long long r)
{
	lab[r] = 1;
	for (auto e : v[r])
	{
		if (lab[e] == 0)
		{
			f(e);
			num[r] = num[r] + num[e];
		}
	}
	num[r]++;
	return 0;
}
bool cmp(long long x, long long y)
{
	return x > y;
}
int main()
{
	long long n = 0, m = 0, t = 0;
	long long i = 0, j = 0, k = 0;
	long long x = 0, y = 0, z = 0;
	read(t);
	while (t--)
	{
		read(n);
		for (i = 1; i < n; i++)
		{
			read(x); read(y);
			edg[i][0] = x;
			edg[i][1] = y;
			v[x].push_back(y);
			v[y].push_back(x);
		}
		read(m);
		for (i = 1; i <= m; i++)
		{
			read(a[i]);
		}
		sort(a + 1, a + n + 1, cmp);
		f(1);
		for (i = 1; i < n; i++)
		{
			b[i] = min(num[edg[i][0]], num[edg[i][1]]) * (n - min(num[edg[i][0]], num[edg[i][1]]));
		}
		sort(b + 1, b + n, cmp);
		long long ans = 0;
		for (i = 1; i < n; i++)
		{
			if (a[i] != 0)ans = ans + b[i] * a[i];
			else ans = ans + b[i];
		}
		cout << ans << endl;
		for (i = 1; i <= n; i++)
		{
			while (!v[i].empty())
			{
				v[i].pop_back();
			}
			num[i] = 0;
			lab[i] = 0;
		}
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
using namespace std;
#define mod 1000000007
template<typename T>
void read(T& x)
{
	int ch = getchar(); x = 0;
	bool f = false;
	while ((ch < '0' || ch>'9') && ch != '-')ch = getchar();
	if (ch == '-') { f = true; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	if (f) x = -x;
}
long long max(long long x, long long y)
{
	return x > y ? x : y;
}
long long min(long long x, long long y)
{
	return x > y ? y : x;
}
long long a[60500] = { 0 };
vector<long long>v[100050];
long long edg[100050][2] = { 0 };
long long lab[100050] = { 0 };
long long num[100050] = { 0 };
long long b[100050] = { 0 };
long long f(long long r)
{
	lab[r] = 1;
	for (auto e : v[r])
	{
		if (lab[e] == 0)
		{
			f(e);
			num[r] = num[r] + num[e];
		}
	}
	num[r]++;
	return 0;
}
bool cmp(long long x, long long y)
{
	return x > y;
}
int main()
{
	long long n = 0, m = 0, t = 0;
	long long i = 0, j = 0, k = 0;
	long long x = 0, y = 0, z = 0;
	read(t);
	while (t--)
	{
		read(n);
		for (i = 1; i < n; i++)
		{
			read(x); read(y);
			edg[i][0] = x;
			edg[i][1] = y;
			v[x].push_back(y);
			v[y].push_back(x);
		}
		read(m);
		for (i = 1; i <= m; i++)
		{
			read(a[i]);
		}
		sort(a + 1, a + n + 1, cmp);
		f(1);
		for (i = 1; i < n; i++)
		{
			b[i] = min(num[edg[i][0]], num[edg[i][1]]) * (n - min(num[edg[i][0]], num[edg[i][1]]));
		}
		sort(b + 1, b + n, cmp);
		long long ans = 0;
		for (i = 1; i < n; i++)
		{
			a[i] = a[i] % mod;
			b[i] = b[i] % mod;
			if (a[i] != 0)ans = ans + b[i] * a[i];
			else ans = ans + b[i];
			ans = ans % mod;
		}
		cout << ans % mod << endl;
		for (i = 1; i <= n; i++)
		{
			while (!v[i].empty())
			{
				v[i].pop_back();
			}
			num[i] = 0;
			lab[i] = 0;
			b[i] = 0;
			edg[i][0] = 0;
			edg[i][1] = 0;
		}
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
using namespace std;
#define mod 1000000007
template<typename T>
void read(T& x)
{
	int ch = getchar(); x = 0;
	bool f = false;
	while ((ch < '0' || ch>'9') && ch != '-')ch = getchar();
	if (ch == '-') { f = true; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	if (f) x = -x;
}
long long max(long long x, long long y)
{
	return x > y ? x : y;
}
long long min(long long x, long long y)
{
	return x > y ? y : x;
}
long long a[60500] = { 0 };
vector<long long>v[100050];
long long edg[100050][2] = { 0 };
long long lab[100050] = { 0 };
long long num[100050] = { 0 };
long long b[100050] = { 0 };
long long f(long long r)
{
	lab[r] = 1;
	for (auto e : v[r])
	{
		if (lab[e] == 0)
		{
			f(e);
			num[r] = num[r] + num[e];
		}
	}
	num[r]++;
	return 0;
}
bool cmp(long long x, long long y)
{
	return x > y;
}
int main()
{
	long long n = 0, m = 0, t = 0;
	long long i = 0, j = 0, k = 0;
	long long x = 0, y = 0, z = 0;
	read(t);
	while (t--)
	{
		read(n);
		for (i = 1; i < n; i++)
		{
			read(x); read(y);
			edg[i][0] = x;
			edg[i][1] = y;
			v[x].push_back(y);
			v[y].push_back(x);
		}
		read(m);
		for (i = 1; i <= m; i++)
		{
			read(a[i]);
		}
		sort(a + 1, a + n + 1, cmp);
		f(1);
		for (i = 1; i < n; i++)
		{
			b[i] = min(num[edg[i][0]], num[edg[i][1]]) * (n - min(num[edg[i][0]], num[edg[i][1]]));
		}
		sort(b + 1, b + n, cmp);
		long long ans = 0;
		for (i = 1; i < n; i++)
		{
			if (a[i] != 0)ans = ans + b[i] * a[i];
			else ans = ans + b[i];
			ans = ans % mod;
		}
		cout << ans % mod << endl;
		for (i = 1; i <= n; i++)
		{
			while (!v[i].empty())
			{
				v[i].pop_back();
			}
			num[i] = 0;
			lab[i] = 0;
		}
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
using namespace std;
#define mod 1000000007
template<typename T>
void read(T& x)
{
	int ch = getchar(); x = 0;
	bool f = false;
	while ((ch < '0' || ch>'9') && ch != '-')ch = getchar();
	if (ch == '-') { f = true; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	if (f) x = -x;
}
long long max(long long x, long long y)
{
	return x > y ? x : y;
}
long long min(long long x, long long y)
{
	return x > y ? y : x;
}
long long a[60500] = { 0 };
vector<long long>v[100050];
long long edg[100050][2] = { 0 };
long long lab[100050] = { 0 };
long long num[100050] = { 0 };
long long b[100050] = { 0 };
long long f(long long r)
{
	lab[r] = 1;
	for (auto e : v[r])
	{
		if (lab[e] == 0)
		{
			f(e);
			num[r] = num[r] + num[e];
		}
	}
	num[r]++;
	return 0;
}
bool cmp(long long x, long long y)
{
	return x > y;
}
int main()
{
	long long n = 0, m = 0, t = 0;
	long long i = 0, j = 0, k = 0;
	long long x = 0, y = 0, z = 0;
	read(t);
	while (t--)
	{
		read(n);
		for (i = 1; i < n; i++)
		{
			read(x); read(y);
			edg[i][0] = x;
			edg[i][1] = y;
			v[x].push_back(y);
			v[y].push_back(x);
		}
		read(m);
		for (i = 1; i <= m; i++)
		{
			read(a[i]);
		}
		sort(a + 1, a + m + 1, cmp);
		f(1);
		for (i = 1; i < n; i++)
		{
			b[i] = min(num[edg[i][0]], num[edg[i][1]]) * (n - min(num[edg[i][0]], num[edg[i][1]]));
		}
		sort(b + 1, b + n, cmp);
		long long ans = 0;
		for (i = 1; i < n; i++)
		{
			a[i] = a[i] % mod;
			b[i] = b[i] % mod;
			if (i <= m)ans = ans + b[i] * a[i];
			else ans = ans + b[i];
			ans = ans % mod;
		}
		cout << ans % mod << endl;
		for (i = 0; i <= n; i++)
		{
			while (!v[i].empty())
			{
				v[i].pop_back();
			}
			num[i] = 0;
			lab[i] = 0;
			b[i] = 0;
			edg[i][0] = 0;
			edg[i][1] = 0;
		}
		for (i = 0; i <= m; i++)
		{
			a[i] = 0;
		}
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
using namespace std;
#define mod 1000000007
template<typename T>
void read(T& x)
{
	int ch = getchar(); x = 0;
	bool f = false;
	while ((ch < '0' || ch>'9') && ch != '-')ch = getchar();
	if (ch == '-') { f = true; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	if (f) x = -x;
}
long long max(long long x, long long y)
{
	return x > y ? x : y;
}
long long min(long long x, long long y)
{
	return x > y ? y : x;
}
long long a[100500] = { 0 };
vector<long long>v[100050];
long long edg[100050][2] = { 0 };
long long lab[100050] = { 0 };
long long num[100050] = { 0 };
long long b[100050] = { 0 };
long long f(long long r)
{
	lab[r] = 1;
	for (auto e : v[r])
	{
		if (lab[e] == 0)
		{
			f(e);
			num[r] = num[r] + num[e];
		}
	}
	num[r]++;
	return 0;
}
bool cmp(long long x, long long y)
{
	return x > y;
}
int main()
{
	long long n = 0, m = 0, t = 0;
	long long i = 0, j = 0, k = 0;
	long long x = 0, y = 0, z = 0;
	read(t);
	while (t--)
	{
		read(n);
		for (i = 1; i < n; i++)
		{
			read(x); read(y);
			edg[i][0] = x;
			edg[i][1] = y;
			v[x].push_back(y);
			v[y].push_back(x);
		}
		read(m);
		for (i = 1; i <= m; i++)
		{
			read(a[i]);
		}
		if(m < n) sort(a + 1, a + m + 1, cmp);
		else
		{
			while (m >= n)
			{
				sort(a + 1, a + m + 1, cmp);
				a[n-1] = a[n-1] * a[m];
				m--;
			}
			sort(a + 1, a + m + 1, cmp);
		}
		f(1);
		for (i = 1; i < n; i++)
		{
			b[i] = min(num[edg[i][0]], num[edg[i][1]]) * (n - min(num[edg[i][0]], num[edg[i][1]]));
		}
		sort(b + 1, b + n, cmp);
		long long ans = 0;
		for (i = 1; i < n; i++)
		{
			a[i] = a[i] % mod;
			b[i] = b[i] % mod;
			if (i <= m) ans = ans + b[i] * a[i];
			else ans = ans + b[i];
			ans = ans % mod;
		}
		cout << ans % mod << endl;
		for (i = 0; i <= n + 49; i++)
		{
			while (!v[i].empty())
			{
				v[i].pop_back();
			}
			num[i] = 0;
			lab[i] = 0;
			b[i] = 0;
			edg[i][0] = 0;
			edg[i][1] = 0;
			a[i] = 0;
		}
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
using namespace std;
#define mod 1000000007
template<typename T>
void read(T& x)
{
	int ch = getchar(); x = 0;
	bool f = false;
	while ((ch < '0' || ch>'9') && ch != '-')ch = getchar();
	if (ch == '-') { f = true; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	if (f) x = -x;
}
long long max(long long x, long long y)
{
	return x > y ? x : y;
}
long long min(long long x, long long y)
{
	return x > y ? y : x;
}
long long a[100500] = { 0 };
vector<long long>v[100050];
long long edg[100050][2] = { 0 };
long long lab[100050] = { 0 };
long long num[100050] = { 0 };
long long b[100050] = { 0 };
long long f(long long r)
{
	lab[r] = 1;
	for (auto e : v[r])
	{
		if (lab[e] == 0)
		{
			f(e);
			num[r] = num[r] + num[e];
		}
	}
	num[r]++;
	return 0;
}
bool cmp(long long x, long long y)
{
	return x > y;
}
int main()
{
	long long n = 0, m = 0, t = 0;
	long long i = 0, j = 0, k = 0;
	long long x = 0, y = 0, z = 0;
	read(t);
	while (t--)
	{
		read(n);
		for (i = 1; i < n; i++)
		{
			read(x); read(y);
			edg[i][0] = x;
			edg[i][1] = y;
			v[x].push_back(y);
			v[y].push_back(x);
		}
		read(m);
		for (i = 1; i <= m; i++)
		{
			read(a[i]);
		}
		sort(a + 1, a + m + 1, cmp);
		f(1);
		for (i = 1; i < n; i++)
		{
			b[i] = min(num[edg[i][0]], num[edg[i][1]]) * (n - min(num[edg[i][0]], num[edg[i][1]]));
		}
		sort(b + 1, b + n, cmp);
		long long ans = 0;
		for (i = 1; i < n; i++)
		{
			a[i] = a[i] % mod;
			b[i] = b[i] % mod;
			if (i <= m) ans = ans + b[i] * a[i];
			else ans = ans + b[i];
			ans = ans % mod;
		}
		cout << ans % mod << endl;
		for (i = 0; i <= n; i++)
		{
			while (!v[i].empty())
			{
				v[i].pop_back();
			}
			num[i] = 0;
			lab[i] = 0;
			b[i] = 0;
			edg[i][0] = 0;
			edg[i][1] = 0;
			a[i] = 0;
		}
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
using namespace std;
#define mod 1000000007
template<typename T>
void read(T& x)
{
	int ch = getchar(); x = 0;
	bool f = false;
	while ((ch < '0' || ch>'9') && ch != '-')ch = getchar();
	if (ch == '-') { f = true; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	if (f) x = -x;
}
long long max(long long x, long long y)
{
	return x > y ? x : y;
}
long long min(long long x, long long y)
{
	return x > y ? y : x;
}
long long a[100500] = { 0 };
vector<long long>v[100050];
long long edg[100050][2] = { 0 };
long long lab[100050] = { 0 };
long long num[100050] = { 0 };
long long b[100050] = { 0 };
long long f(long long r)
{
	lab[r] = 1;
	for (auto e : v[r])
	{
		if (lab[e] == 0)
		{
			f(e);
			num[r] = num[r] + num[e];
		}
	}
	num[r]++;
	return 0;
}
bool cmp(long long x, long long y)
{
	return x > y;
}
int main()
{
	long long n = 0, m = 0, t = 0;
	long long i = 0, j = 0, k = 0;
	long long x = 0, y = 0, z = 0;
	read(t);
	while (t--)
	{
		read(n);
		for (i = 1; i < n; i++)
		{
			read(x); read(y);
			edg[i][0] = x;
			edg[i][1] = y;
			v[x].push_back(y);
			v[y].push_back(x);
		}
		read(m);
		for (i = 1; i <= m; i++)
		{
			read(a[i]);
		}
		if (m < n) sort(a + 1, a + m + 1, cmp);
		else
		{
			sort(a + 1, a + m + 1);
			for (i = n; i <= m; i++)
			{
				a[n-1] = a[n-1] * a[i];
			}
			sort(a + 1, a + n, cmp);
		}
		f(1);
		for (i = 1; i < n; i++)
		{
			b[i] = min(num[edg[i][0]], num[edg[i][1]]) * (n - min(num[edg[i][0]], num[edg[i][1]]));
		}
		sort(b + 1, b + n, cmp);
		long long ans = 0;
		for (i = 1; i < n; i++)
		{
			a[i] = a[i] % mod;
			b[i] = b[i] % mod;
			if (i <= m) ans = ans + b[i] * a[i];
			else ans = ans + b[i];
			ans = ans % mod;
		}
		cout << ans % mod << endl;
		for (i = 0; i <= n + 49; i++)
		{
			while (!v[i].empty())
			{
				v[i].pop_back();
			}
			num[i] = 0;
			lab[i] = 0;
			b[i] = 0;
			edg[i][0] = 0;
			edg[i][1] = 0;
			a[i] = 0;
		}
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
using namespace std;
#define mod 1000000007
template<typename T>
void read(T& x)
{
	int ch = getchar(); x = 0;
	bool f = false;
	while ((ch < '0' || ch>'9') && ch != '-')ch = getchar();
	if (ch == '-') { f = true; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	if (f) x = -x;
}
long long max(long long x, long long y)
{
	return x > y ? x : y;
}
long long min(long long x, long long y)
{
	return x > y ? y : x;
}
long long a[100500] = { 0 };
vector<long long>v[100050];
long long edg[100050][2] = { 0 };
long long lab[100050] = { 0 };
long long num[100050] = { 0 };
long long b[100050] = { 0 };
long long f(long long r)
{
	lab[r] = 1;
	for (auto e : v[r])
	{
		if (lab[e] == 0)
		{
			f(e);
			num[r] = num[r] + num[e];
		}
	}
	num[r]++;
	return 0;
}
bool cmp(long long x, long long y)
{
	return x > y;
}
int main()
{
	long long n = 0, m = 0, t = 0;
	long long i = 0, j = 0, k = 0;
	long long x = 0, y = 0, z = 0;
	read(t);
	while (t--)
	{
		read(n);
		for (i = 1; i < n; i++)
		{
			read(x); read(y);
			edg[i][0] = x;
			edg[i][1] = y;
			v[x].push_back(y);
			v[y].push_back(x);
		}
		read(m);
		for (i = 1; i <= m; i++)
		{
			read(a[i]);
		}
		if(m<n) sort(a + 1, a + m + 1, cmp);
		else
		{
			sort(a + 1, a + m + 1);
			for (i = n + 1; i <= m; i++)
			{
				a[n] = a[n] * a[i];
			}
			sort(a + 1, a + n + 1, cmp);
		}
		f(1);
		for (i = 1; i < n; i++)
		{
			b[i] = min(num[edg[i][0]], num[edg[i][1]]) * (n - min(num[edg[i][0]], num[edg[i][1]]));
		}
		sort(b + 1, b + n, cmp);
		long long ans = 0;
		for (i = 1; i < n; i++)
		{
			a[i] = a[i] % mod;
			b[i] = b[i] % mod;
			if (i <= m) ans = ans + b[i] * a[i];
			else ans = ans + b[i];
			ans = ans % mod;
		}
		cout << ans % mod << endl;
		for (i = 0; i <= n + 49; i++)
		{
			while (!v[i].empty())
			{
				v[i].pop_back();
			}
			num[i] = 0;
			lab[i] = 0;
			b[i] = 0;
			edg[i][0] = 0;
			edg[i][1] = 0;
			a[i] = 0;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
template <typename T>
inline T read(){
	T x=0;
	int f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=(x<<1)+(x<<3)+ch-'0';
		ch=getchar();
	}
	return x*f;
}
vector<int> son[1000001];
int weight[1000001];
inline void addedge(int u,int v){
	son[u].emplace_back(v);
	son[v].emplace_back(u);
}
vector<long long> con;
vector<long long> p;
int n,vis[1000001];
void dfs(int u){
	for(auto v:son[u]){
		if(vis[v])continue;
		vis[v]=true;
		dfs(v);
		weight[u]+=weight[v];
	}
	if(u!=1){
		con.emplace_back((long long)weight[u]*(n-weight[u]));
	}
}
void solve(){
	n=read<int>();
	int u,v;
	con.clear();
	p.clear();
	for(int i=1;i<=n;i++){
		son[i].clear();
		weight[i]=1;
		vis[i]=0;
	}
	for(int i=1;i<n;i++){
		u=read<int>();v=read<int>();
		addedge(u,v);
	}
	int m=read<int>();
	p.resize(m);
	for(int i=0;i<m;i++){
		p[i]=read<int>();
	}
	vis[1]=true;
	dfs(1);
	sort(con.begin(),con.end(),greater<long long>());
	sort(p.begin(),p.end(),greater<long long>());
	long long rslt=0;
	if(m<=n-1){
		while(p.size()<con.size())p.emplace_back(1);
		for(int i=0;i<con.size();i++){
			rslt = (rslt+(con[i]%mod*p[i]%mod))%mod;
		}
	}else{
		int duo=p.size()-con.size();
		for(int i=0;i<duo;i++){
			p[duo]=p[i]*p[duo]%mod;
		}
		if(n==100000){
			cout<<p[0]<<endl;
		}
		for(int i=0;i<con.size();i++){
			rslt = (rslt+(con[i]%mod*p[i+duo]%mod))%mod;
		}
	}
	cout<<rslt<<endl;
}
int main(){
	int t=read<int>();
	while(t--)solve();
}


#include<map>
#include<list>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define in inline
#define ll long long
#define INF 2147483647
#define ri register int
#define rc register char
using namespace std;
in int read(){
	rc c=' ';
	ri v=0,x=1;
	while((c<'0')||(c>'9')){if(c=='-') x=-1;c=getchar();}
	while((c>='0')&&(c<='9')){v=(v<<1)+(v<<3)+(c^48);c=getchar();}
	return v*x;
}
in void write(ri x){
	if(x<0) x=-x,putchar('-');
	if(x>9) write(x/10);
	putchar(x%10^48);
}
const int N=200010,mod=1e9+7;
int t,n,m,cnt,tot,head[N],siz[N],v[N],nxt[N];
ll ans,w1[N],w2[N];
in void add(ri u,ri vv){
	v[++cnt]=vv;
	nxt[cnt]=head[u];
	head[u]=cnt;
	v[++cnt]=u;
	nxt[cnt]=head[vv];
	head[vv]=cnt;
}
void dfs(ri u,ri fa){
	siz[u]=1;
	for(ri i=head[u];i;i=nxt[i])
		if(v[i]!=fa) dfs(v[i],u),siz[u]+=siz[v[i]];
	for(ri i=head[u];i;i=nxt[i])
		if(v[i]!=fa) w2[++tot]=(n-siz[v[i]])*siz[v[i]];
}
in bool cmp(ri x,ri y){
	return x>y;
}
int main(){
	t=read();
	while(t--){
		n=read(),ans=tot=0,cnt=1;
		for(ri i=1;i<=n;i++)
			head[i]=siz[i]=v[i<<1]=v[i<<1|1]=w1[i]=w2[i]=nxt[i<<1]=nxt[i<<1|1]=0;
		for(ri i=1;i<=n-1;i++)
			add(read(),read());
		dfs(1,0),m=read();
		for(ri i=1;i<=m;i++)
			w1[i]=read();
		sort(w1+1,w1+m+1,cmp);
		sort(w2+1,w2+tot+1,cmp);
		for(ri i=1;i<=m;i++)
			ans=(ans+w1[i]*w2[i]%mod)%mod;
		for(ri i=m+1;i<=tot;i++)
			ans=(ans+w2[i])%mod;
		write(ans),putchar('\n');
	}
	return ~~(0-0);
}


#include<bits/stdc++.h>
using namespace std;
int j,k,m,n,s,t;
const int N=1e6+10;
struct fu{
	int v,next;
}a[N<<1];
int mod=1e9+7;
int head[N],c[N],size[N],fa[N];
long long val[N],p[N];
void work(int aa,int bb)
{
	a[++k].next=head[aa];
	head[aa]=k;
	a[k].v=bb;
}
void dfs(int u)
{
	int i;
	for(i=head[u];i;i=a[i].next)
	{
		int v=a[i].v;
		if(v==fa[u])
		continue;
		fa[v]=u;
		dfs(v);
		size[u]+=size[v];
	}
	size[u]++;
	val[u]=(long long)size[u]*(n-size[u])%(long long)mod;
}
int main()
{
	scanf("%d",&t);
	int last=0;
	while(t--)
	{
		k=0;
		int i;
		for(i=1;i<=last;i++)
		head[i]=0,size[i]=0;
		scanf("%d",&n);
		int x,y;
		for(i=1;i<=n-1;i++)
		{
			scanf("%d%d",&x,&y);
			work(x,y);
			work(y,x);
		}
		dfs(1);
		scanf("%d",&m);
		for(i=1;i<=m;i++)
		scanf("%lld",&p[i]);
		if(m<n-1)
		for(i=m+1;i<=n-1;i++)
		p[i]=1;
		m=max(m,n-1);
		sort(p+1,p+m+1);
		sort(val+2,val+n+1);
		long long ans=0;
		for(i=m-1;i>=n-1;i--)
		p[i]=p[i+1]*p[i]%mod;
		for(i=n;i>=2;i--)
		ans=(ans+val[i]*(long long)p[i-1])%(long long)mod;
		printf("%lld\n",ans);
		last=n;
	}
	return 0;
}


#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int  inf = 0x7fffffff;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll   mod = 1e9 + 7;
const int  N = 5e5 + 5;

int t, n, m;
int s[N], c[N], p[N];
int h[N], tt;

struct node{
	int to, ne;
}g[N];
void add(int u,int v){
	g[++ tt]=(node){v, h[u]};
	h[u] = tt;
}

void dfs(int u,int fa){
	s[u] = 1;
	for(int i = h[u]; i; i = g[i].ne){
		int v = g[i].to;
		if(v == fa)	continue;
		dfs(v, u);
		s[u] += s[v];
		c[i/2] = s[v]*(n-s[v]);
	}
}

signed main()
{
    IOS;
	cin >> t;
	while(t --){
		cin >> n; tt = 1;
		for(int i = 0; i <= n*2;i ++) h[i] = 0, s[i] = 0;
		for(int i = 1; i <= m; i ++) p[i] = 0;
		for(int i = 1; i < n; i ++){
			int u, v; cin >> u >> v;
			add(u, v); add(v, u);
		}
		dfs(1,0);
		cin >> m;
		for(int i = 1; i <= m; i ++) cin >> p[i];
		sort(p+1, p+m+1); sort(c+1,c+n);
		int ans = 0;
		if(m <= n-1){
			for(int i = n-1, j = m; j >= 1; i --, j --)
				ans = (ans + c[i]*p[j]%mod )%mod;
			for(int i = n-m-1; i >= 1; i --)
				ans = (ans + c[i])%mod;
		}
		else{
			for(int i = m-1; i >= n-1;i --)
				p[i] = p[i]*p[i+1]%mod;
			for(int i = n-1; i >= 1; i --)
				ans = (ans + c[i]*p[i]%mod )%mod;
		}
		cout << ans << endl;
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const long long mo=1000000007;
long long cnt[100005],p[60005],h[100005],T,tot,n,m,siz[100005];
struct DX
{
	long long next,v;
}dx[200005];
void add(long long u,long long v)
{
	dx[++tot].v=v;
	dx[tot].next=h[u];
	h[u]=tot;
}
void dfs(long long u,long long fa)
{
	siz[u]=1;
	for(long long i=h[u];i;i=dx[i].next)
	{
		if(dx[i].v==fa)
			continue;
		dfs(dx[i].v,u);
		siz[u]+=siz[dx[i].v];
	}
	cnt[u-1]=siz[u]*(n-siz[u]);
}
int main()
{
	scanf("%lld",&T);
	while(T--)
	{
		scanf("%lld",&n);
		tot=0;
		memset(h,0,sizeof(h));
		for(long long i=1;i<n;i++)
		{
			long long a,b;
			scanf("%lld%lld",&a,&b);
			add(a,b);
			add(b,a);
		}
		scanf("%lld",&m);
		for(long long i=1;i<=m;i++)
			scanf("%lld",&p[i]);
		if(m<n)
		{
			for(long long i=m+1;i<n;i++)
				p[i]=1;
			sort(p+1,p+n);
		}
		else
		{
			sort(p+1,p+m+1);
			for(long long i=m;i>=n;i--)
				p[i-1]=(p[i-1]*p[i])%mo;
		}
		dfs(1,1);
		sort(cnt+1,cnt+n);
		long long ans=0;
		for(long long i=1;i<n;i++)
			ans=(ans+(p[i]%mo)*(cnt[i]%mo)%mo)%mo;
		printf("%lld\n",ans);
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <stdlib.h>
#include <stack>
#include <queue>
#define ri register int
#define int long long

inline int read() {
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9') {x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
	return x*f;
}

const int N=1e5+5,inf=1e9+7;

int T,n,m;
int to[N<<1],next[N<<1],head[N],val[N],cnt=1,tot=0;
int p[N],siz[N],son[N<<1],f[N<<1],ans=0;

bool cmp(int x,int y){
	return x>y;
}

void edge_add(int u,int v){
	to[cnt]=v;
	next[cnt]=head[u];
	head[u]=cnt++;
}

void Read(){
	n=read();
	for(ri i=1;i<n;i++){
		int u=read(),v=read();
		edge_add(u,v),edge_add(v,u);
	}
	m=read();
	for(ri i=1;i<=m;i++)p[i]=read();
	std::sort(p+1,p+m+1,cmp);
}

void dfs(int now,int fa){
	siz[now]=1;
	for(ri i=head[now];i;i=next[i]){
		int v=to[i];
		if(v==fa)continue;
		son[i]=v,f[i]=now;
		dfs(v,now);
		siz[now]+=siz[v];
	}
}

void work(){
	for(ri i=1;i<=(n<<1);i++){
		if(son[i]==0)continue;
		val[++tot]=siz[son[i]]*(n-siz[son[i]])%inf;
	}
//	for(ri i=1;i<n;i++)printf("%d ",val[i]);
//	printf("\n");
	std::sort(val+1,val+n,cmp);
	int ls=1,pp=1;n-=1;
	if(n>=m)pp=1;
	else {
		int ls=1;
		for(ri i=1;i<=m-n+1;i++)ls*=p[i],pp++,ls%=inf;
		p[pp]=ls;
	}
	for(ri i=1;i<=n;i++){
		if(pp<=m)ans+=val[i]*p[pp];
		else ans+=val[i];
//		printf("%d %d\n",val[i],p[pp]);
		ans%=inf;
		pp++;
	}
	printf("%lld\n",ans);
	n+=1;
}

void clear(){
	for(ri i=1;i<=(n<<1);i++)to[i]=0,next[i]=0,son[i]=0,f[i]=0;
	for(ri i=1;i<=n;i++)head[i]=0,siz[i]=0,val[i]=0,p[i]=0;
	cnt=1,ans=0,tot=0;
}

signed main(void){
	T=read();
	while(T--){
		Read();
		dfs(1,0);
		work();
		clear();
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const long long mo=1000000007;
vector<int> h[100005];
long long cnt[100005],p[60005],T,tot,n,m,siz[100005],ans;
void dfs(long long u,long long fa)
{
	siz[u]=1;
	for(vector<int>::iterator it=h[u].begin();it!=h[u].end();it++)
	{
		if((*it)==fa)
			continue;
		dfs(*it,u);
		siz[u]+=siz[*it];
	}
	cnt[u-1]=siz[u]*(n-siz[u]);
}
int main()
{
	scanf("%lld",&T);
	while(T--)
	{
		ans=0;
		scanf("%lld",&n);
		for(int i=1;i<=n;i++)
			h[i].clear();
		memset(cnt,0,sizeof(cnt));
		memset(p,0,sizeof(p));
		for(long long i=1;i<n;i++)
		{
			long long a,b;
			scanf("%lld%lld",&a,&b);
			h[a].push_back(b);
			h[b].push_back(a);
		}
		scanf("%lld",&m);
		for(long long i=1;i<=m;i++)
			scanf("%lld",&p[i]);
		if(m<n)
		{
			for(long long i=m+1;i<n;i++)
				p[i]=1;
			sort(p+1,p+n);
		}
		else
		{
			sort(p+1,p+m+1);
			for(long long i=m;i>=n;i--)
				p[i-1]=(p[i-1]*p[i])%mo;
		}
		dfs(1LL,1LL);
		sort(cnt+1,cnt+n);
		for(long long i=1;i<n;i++)
			ans=(ans+(p[i]%mo)*(cnt[i]%mo)%mo)%mo;
		printf("%lld\n",ans);
	}
	return 0;
}


#include<bits/stdc++.h>
#define int long long
#define N 100005
#define mo 1000000007
using namespace std;
struct{
	int to,nx;
}e[N<<1];
int cnt,head[N],size[N],ans,n,m,mx,T,x,y,tp,a[N];
priority_queue <int,vector<int>,less<int> >q;
void add(int x,int y){
	cnt++;
	e[cnt].to=y;
	e[cnt].nx=head[x];
	head[x]=cnt;
}
void dfs(int k,int fa) {
	size[k]=1;
	for (int i=head[k];i;i=e[i].nx)
	  if (e[i].to!=fa)
	  dfs(e[i].to,k),size[k]+=size[e[i].to];
}
void dfs1(int k,int fa){
	for (int i=head[k];i;i=e[i].nx)
	  if (e[i].to!=fa){
	  	q.push(size[e[i].to]*(n-size[e[i].to]));
	  	dfs1(e[i].to,k);
	  }
}
signed main(){
	cin>>T;
	while (T--){
		cin>>n;cnt=ans=mx=0;
		for (int i=1;i<=n;i++) head[i]=0;
		for (int i=1;i<n;i++){
			cin>>x>>y;
			add(x,y);
			add(y,x);
		}
		dfs(1,0);
		dfs1(1,0);
		cin>>m;
		for (int i=1;i<=m;i++) {
			cin>>a[i];
		}
		sort(a+1,a+m+1);
		n--;
		if (m>n) {
		    for (int i=n+1;i<=m;i++) (a[n]*=a[i])%=mo;
		    m=n;
		}
		while (!q.empty()){
			tp=q.top();q.pop();
			(ans+=max(1ll,a[m--])*tp%mo)%=mo;
			if (m<0) m=0;
		}
		cout<<ans<<endl;
	}
}


#include<bits/stdc++.h>
#define mod 1000000007
#define int long long
using namespace std;
int n,m;
int g[200010];
int head[200010],cnt=0;
struct ss{
	int to,next;
}e[400010];
int ch[200010];
inline void add(int a,int b){
	cnt++;
	e[cnt].to=b;
	e[cnt].next=head[a];
	head[a]=cnt;
}
int de[500001],fa[500000+1][21];
void dfs(int n,int f){
	fa[n][0]=f;
	de[n]=de[f]+1;
	for(int i=1;i<=20;i++){
		fa[n][i]=fa[fa[n][i-1]][i-1];
	}
	for(int i=head[n];i;i=e[i].next){
		if(e[i].to!=f){
			dfs(e[i].to,n);
		}
	}
	return;
}

int lca(int a,int b){
	if(de[a]>de[b]){
		swap(a,b);
	}
	for(int i=20;i>=0;i--){
		if(de[a]<=de[b]-(1<<i)){
			b=fa[b][i];
		}
	}
	if(a==b) return a;
	for(int i=20;i>=0;i--){
		 if(fa[a][i]==fa[b][i]){
		 	continue;
		 }else{
		 	a=fa[a][i],b=fa[b][i];
		 }
	}
	return fa[a][0];
}
void dff(int u,int f){
	for(int i=head[u];i!=0;i=e[i].next){
		if(e[i].to==f){
			continue;
		}
		dff(e[i].to,u);
		ch[u]+=ch[e[i].to];
	}
	return;
}
signed main(){
	int t;
	scanf("%lld",&t);
	while(t--){
		memset(head,0,sizeof(head));
		memset(e,0,sizeof(e));
		memset(fa,0,sizeof(fa));
		memset(de,0,sizeof(de));
		scanf("%lld",&n);
		for(int i=1;i<=n;i++){
			g[i]=1;
		}
		memset(ch,0,sizeof(ch));
		for(int i=1;i<n;i++){
			int a,b;
			scanf("%lld%lld",&a,&b);
			add(a,b);add(b,a);
		}
		scanf("%lld",&m);
		for(int i=1;i<=m;i++){
			scanf("%lld",&g[i]);
		}
		sort(g+1,g+n+1);
		dfs(1,0);
		for(int i=1;i<n;i++){
			for(int j=i+1;j<=n;j++){
				ch[i]++;
				ch[j]++;
				ch[lca(i,j)]-=2;
			}
		}
		dff(1,0);
		sort(ch+1,ch+n+1);
		int ans=0;
		for(int i=n;i>=1;i--){
			ans+=(ch[i]*g[i]);
			ans=ans%mod;
		}
		printf("%lld\n",ans);
	}

	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const long long mo=1000000007;
vector<int> h[100005];
long long cnt[100005],p[60005],T,tot,n,m,siz[100005],ans;
void dfs(long long x,long long fa)
{
	siz[x]=1;
	for(vector<int>::iterator it=h[x].begin();it!=h[x].end();it++)
	{
		if((*it)==fa)
			continue;
		dfs(*it,x);
		siz[x]+=siz[*it];
	}
	if(siz[x]*(n-siz[x])!=0)
		cnt[++tot]=siz[x]*(n-siz[x]);
}
int main()
{
	scanf("%lld",&T);
	while(T--)
	{
		long long a,b;
		ans=0;
		tot=0;
		scanf("%lld",&n);
		for(int i=1;i<=n;i++)
			h[i].clear();
		memset(cnt,0,sizeof(cnt));
		memset(p,0,sizeof(p));
		for(long long i=1;i<n;i++)
		{
			scanf("%lld%lld",&a,&b);
			h[a].push_back(b);
			h[b].push_back(a);
		}
		scanf("%lld",&m);
		for(long long i=1;i<=m;i++)
			scanf("%lld",&p[i]);
		if(m<n)
			sort(p+1,p+n);
		else
		{
			sort(p+1,p+m+1);
			for(long long i=m;i>=n;i--)
				p[i-1]=(p[i-1]*p[i])%mo;
		}
		dfs(1LL,1LL);
		sort(cnt+1,cnt+n);
		for(long long i=1;i<n;i++)
			ans=(ans+((p[i]?p[i]:1)%mo)*(cnt[i]%mo)%mo)%mo;
		printf("%lld\n",ans);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, M = 6e4 + 10, mod = 1e9 + 7;
struct EDGE {
	int to, next;
} e[N];
typedef long long ll;
int sz[N], p[M], head[N], num[N];
int n, m, idx;

void add_edge(int u, int v)
{
	idx++; e[idx].to = v; e[idx].next = head[u]; head[u] = idx;
}

void dfs(int now, int fa)
{
	sz[now] = 1;
	for (int i = head[now]; i; i = e[i].next) {
		if (e[i].to != fa) {
			dfs(e[i].to, now);
			sz[now] += sz[e[i].to];
		}
	}
	if (now != 1) num[now] = sz[now] * (n - sz[now]);
}

void solve()
{
	memset(head, 0, sizeof head);
	cin >> n;
	for (int i = 1; i <= n - 1; i++) {
		int u, v; scanf("%d %d", &u, &v);
		add_edge(u, v); add_edge(v, u);
	}
	cin >> m;
	for (int i = 1; i <= m; i++) scanf("%d", &p[i]);
	dfs(1, 0);
	sort(num + 1, num + 1 + n);
	sort(p + 1, p + 1 + m);
	ll ans = 0;
	if (m < n - 1) {
		// cout << "#1" << endl;
		for (int i = 1; i < n; i++) {
			// cout << "num[" << i << "] = " << num[i] << endl;
			ans += num[n - i + 1] * (m - i + 1 > 0 ? p[m - i + 1] : 1) % mod;
			ans %= mod;
		}
	} else {
		cout << "#2" << endl;
		ll res = 1;
		int k = m - n + 2;
		for (int i = 0; i < k; i++) res = res * p[m - i] % mod;
		ans = res;
		for (int i = 1; i <= n - 2; i++) ans = (ans + p[n - i] * num[n - i] % mod) % mod;
	}
	cout << ans << endl;
}

int main()
{
	int _; cin >> _; while (_--) {
		solve();
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const long long mo=1000000007;
long long cnt[1000005],p[60005],h[100005],T,tot,n,m,siz[100005];
struct DX
{
	long long next,v;
}dx[2000005];
void add(long long u,long long v)
{
	dx[++tot].v=v;
	dx[tot].next=h[u];
	h[u]=tot;
}
void dfs(long long u,long long fa)
{
	siz[u]=1;
	for(long long i=h[u];i;i=dx[i].next)
	{
		if(dx[i].v==fa)
			continue;
		dfs(dx[i].v,u);
		siz[u]+=siz[dx[i].v];
	}
	cnt[u-1]=siz[u]*(n-siz[u]);
}
int main()
{
	scanf("%lld",&T);
	while(T--)
	{
		scanf("%lld",&n);
		tot=0;
		memset(h,0,sizeof(h));
		for(long long i=1;i<n;i++)
		{
			long long a,b;
			scanf("%lld%lld",&a,&b);
			add(a,b);
			add(b,a);
		}
		scanf("%lld",&m);
		for(long long i=1;i<=m;i++)
			scanf("%lld",&p[i]);
		if(m<n)
		{
			for(long long i=m+1;i<n;i++)
				p[i]=1;
			sort(p+1,p+n);
		}
		else
		{
			sort(p+1,p+m+1);
			for(long long i=m;i>=n;i--)
				p[i-1]=(p[i-1]*p[i])%mo;
		}
		dfs(1,1);
		sort(cnt+1,cnt+n);
		long long ans=0;
		for(long long i=1;i<n;i++)
			ans=(ans+(p[i]%mo)*(cnt[i]%mo)%mo)%mo;
		printf("%lld\n",ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a,b,c,n,m,head[1000001],k1[1000001],size[1000001],num[1000001],cnt,u,pos,mod=1000000007;
struct node
{
	int next,to;
}k[1000001];
void add_edge(int q,int w)
{
	cnt++;
	k[cnt].to=w;
	k[cnt].next=head[q];
	head[q]=cnt;
}
int cmp(int x,int y)
{
	return x>y;
}
void dfs(int q,int si)
{
	size[q]=1;
	for (int i=head[q];i;i=k[i].next)
	{
		if (k[i].to!=si)
		{
			dfs(k[i].to,q);
			size[q]+=size[k[i].to];
		}
	}
	if (q!=1)
	num[++pos]=size[q]*(n-size[q]);
}
int main ( )
{
scanf("%d",&u);
for(int g=1;g<=u;g++)
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	k[i].next=k[i].to=head[i]=0;
	for(int i=1;i<=n-1;i++)
	{
		scanf("%d%d",&a,&b);
		add_edge(a,b);

	}
	scanf("%d",&m);
	for(int i=1;i<=m;i++)
	scanf("%d",&k1[i]);
	sort(k1+1,k1+1+m,cmp);
	sort(num+1,num+n,cmp);
	if (m>n-1)
	{
		int tot=1;
		for (int i=1;i<=m-n+2;i++)
		tot=(tot*k1[i])%mod;
		k1[m-n+2]=tot;
		for (int i=m-n+2;i<=m;i++)
		k1[i-m+n-1]=k1[i];
	}
	int ans=0;
	for (int i=1;i<=n-1;i++)
	{
		if (i<=m)
		ans=(ans+((num[i]*k1[i])%mod))%mod;
		else
		ans=(ans+num[i])%mod;
	}
	cout<<ans<<endl;
}
}


#include<bits/stdc++.h>
#define mod 1000000007
#define int long long
using namespace std;
int n,m;
int g[100010],num[100010];
int head[100010],cnt=0;
struct ss{
	int to,next;
}e[100010];
inline void add(int a,int b){
	cnt++;
	e[cnt].to=b;
	e[cnt].next=head[a];
	head[a]=cnt;
}
int dfs(int u,int f){
	int sum=1;
	for(int i=head[u];i!=0;i=e[i].next){
		if(e[i].to==f){
			continue;
		}
		sum+=dfs(e[i].to,u);
	}
	num[u]=sum*(n-sum);
	return sum;
}
bool cmp(int a,int b){
	return a>=b;
}
signed main(){
	int t;
	scanf("%lld",&t);
	while(t--){
		memset(head,0,sizeof(head));
		memset(e,0,sizeof(e));
		memset(num,0,sizeof(num));
		cnt=0;
		scanf("%lld",&n);
		for(int i=1,a,b;i<n;i++){
			scanf("%lld%lld",&a,&b);
			add(a,b);
			add(b,a);
			g[i]=1;
		}
		g[n]=1;
		scanf("%lld",&m);
		for(int i=1;i<=m;i++){
			scanf("%lld",&g[i]);
		}
		sort(g+1,g+m+1,cmp);
		for(int i=n+1;i<=m;i++){
			g[1]+=g[i];
			g[i]=1;
		}
		int yy=dfs(1,0);
		int ans=0;
		sort(num+1,num+n+1,cmp);
		for(int i=1;i<=n;i++){
			ans+=(num[i]*g[i])%mod;
			ans=ans%mod;
		}
		printf("%lld\n",ans);
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <stdlib.h>
#include <stack>
#include <queue>
#define ri register int
#define int long long

inline int read() {
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9') {x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
	return x*f;
}

const int N=1e5+5,inf=1e9+7;

int T,n,m;
int to[N<<1],next[N<<1],head[N],val[N],cnt=1,tot=0;
int p[N],siz[N],son[N<<1],f[N<<1],ans=0;

bool cmp(int x,int y){
	return x>y;
}

void edge_add(int u,int v){
	to[cnt]=v;
	next[cnt]=head[u];
	head[u]=cnt++;
}

void Read(){
	n=read();
	for(ri i=1;i<n;i++){
		int u=read(),v=read();
		edge_add(u,v),edge_add(v,u);
	}
	m=read();
	for(ri i=1;i<=m;i++)p[i]=read();
	std::sort(p+1,p+m+1,cmp);
}

void dfs(int now,int fa){
	siz[now]=1;
	for(ri i=head[now];i;i=next[i]){
		int v=to[i];
		if(v==fa)continue;
		son[i]=v,f[i]=now;
		dfs(v,now);
		siz[now]+=siz[v];
	}
}

void work(){
	for(ri i=1;i<=(n<<1);i++){
		if(son[i]==0)continue;
		val[++tot]=siz[son[i]]*(n-siz[son[i]]);
	}
//	for(ri i=1;i<n;i++)printf("%d ",val[i]);
//	printf("\n");
	std::sort(val+1,val+n,cmp);
	int ls=1,pp=1;n-=1;
	if(n>=m)pp=1;
	else {
		int ls=1;
		for(ri i=1;i<=m-n+1;i++)ls*=p[i],pp++,ls%=inf;
		p[pp]=ls;
	}
	for(ri i=1;i<=n;i++){
		if(pp<=m)ans+=val[i]*p[pp];
		else ans+=val[i];
//		printf("%d %d\n",val[i],p[pp]);
		ans%=inf;
		pp++;
	}
	printf("%lld\n",ans);
	n+=1;
}

void clear(){
	for(ri i=1;i<=(n<<1);i++)to[i]=0,next[i]=0,son[i]=0,f[i]=0;
	for(ri i=1;i<=n;i++)head[i]=0,siz[i]=0,val[i]=0,p[i]=0;
	cnt=1,ans=0,tot=0;
}

signed main(void){
	T=read();
	while(T--){
		Read();
		dfs(1,0);
		work();
		clear();
	}
	return 0;
}


#include<map>
#include<list>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define in inline
#define ll long long
#define INF 2147483647
#define ri register int
#define rc register char
using namespace std;
in int read(){
	rc c=' ';
	ri v=0,x=1;
	while((c<'0')||(c>'9')){if(c=='-') x=-1;c=getchar();}
	while((c>='0')&&(c<='9')){v=(v<<1)+(v<<3)+(c^48);c=getchar();}
	return v*x;
}
in void write(ll x){
	if(x<0) x=-x,putchar('-');
	if(x>9) write(x/10);
	putchar(x%10^48);
}
const int N=2000010,mod=1e9+7;
struct Edge{
	int v,nxt;
}e[N];
int t,n,m,cnt,tot,head[N],siz[N];
ll ans,w1[N],w2[N];
in void add(ri u,ri v){
	e[++cnt]=(Edge){v,head[u]};
	head[u]=cnt;
}
void dfs(ri u,ri fa){
	siz[u]=1;
	for(ri i=head[u];i;i=e[i].nxt){
		ri v=e[i].v;
		if(v!=fa) dfs(v,u),siz[u]+=siz[v];
	}
	if(u!=1) w2[++tot]=1ll*(n-siz[u])*siz[u];
}
in bool cmp(ll x,ll y){
	return x>y;
}
int main(){
	t=read();
	while(t--){
		n=read(),ans=cnt=tot=0;
		for(ri i=1;i<=n*2;i++)
			head[i]=e[i].v=e[i].nxt=0;
		for(ri i=1,u,v;i<=n-1;i++)
			u=read(),v=read(),add(u,v),add(v,u);
		dfs(1,0),m=read();
		for(ri i=1;i<=m;i++)
			w1[i]=read();
		sort(w1+1,w1+m+1,cmp);
		sort(w2+1,w2+n,cmp);
		if(m<=n-1){
			for(ri i=1;i<=m;i++)
				ans=(ans+w1[i]*w2[i])%mod;
			for(ri i=m+1;i<=n-1;i++)
				ans=(ans+w2[i])%mod;
		}
		else{
			for(ri i=2;i<=m-n+2;i++)
				w1[1]=w1[1]*w1[i]%mod;
			for(ri i=2;i<=n-1;i++)
				w1[i]=w1[i+m-n+1];
			for(ri i=1;i<=n-1;i++)
				ans=(ans+w1[i]*w2[i])%mod;
		}
		write(ans),putchar('\n');
	}
	return ~~(0-0);
}


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

const ll N=1e6+5,mod=1e9+7;
ll t,a,b,n,m,k,ans,ecnt,p[N],sz[N],con[N],head[N];
struct Edge
{
	ll v,next;
}e[N<<1];
inline bool cmp(ll A,ll B){return A>B;}
inline void add(ll u,ll v)
{
	e[++k].v=v;
	e[k].next=head[u];
	head[u]=k;
}
inline void dfs(ll u,ll fa)
{
	sz[u]=1;
	for(ll i=head[u];i;i=e[i].next)
	{
		ll v=e[i].v;
		if(v==fa) continue;
		dfs(v,u);
		sz[u]+=sz[v];
		con[++ecnt]=sz[v]*(n-sz[v]);
	}
}

int main()
{
	scanf("%lld",&t);
	while(t--)
	{
		scanf("%lld",&n);
		ans=ecnt=0;
		for(ll i=1;i<=n;i++) p[i]=sz[i]=con[i]=head[i]=0;
		for(ll i=1;i<n;i++)
		{
			scanf("%lld%lld",&a,&b);
			add(a,b);add(b,a);
		}
		scanf("%lld",&m);
		for(ll i=1;i<=m;i++) scanf("%lld",&p[i]);
		dfs(1,0);
		sort(p+1,p+1+m,cmp);
		sort(con+1,con+1+ecnt,cmp);
		if(m<=n-1)
		{
			for(ll i=m+1;i<=n;i++) p[i]=1;
			for(ll i=1;i<n;i++) ans=(ans+p[i]*con[i]%mod)%mod;
			printf("%lld\n",(ans+mod)%mod);
		}
		else
		{
			ll nw=m-n+2,tmp=1;
			for(ll i=1;i<=nw;i++) tmp=tmp*p[i]%mod;
			ans=tmp*con[1]%mod;
			for(ll i=2;i<n;i++) ans=(ans+p[nw+i-1]*con[i]%mod)%mod;
			printf("%lld\n",(ans+mod)%mod);
		}
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const long long mo=1000000007;
long long cnt[1000005],p[60005],h[1000005],T,tot,n,m,siz[1000005];
struct DX
{
	long long next,v;
}dx[2000005];
void add(long long u,long long v)
{
	dx[++tot].v=v;
	dx[tot].next=h[u];
	h[u]=tot;
}
void dfs(long long u,long long fa)
{
	siz[u]=1;
	for(long long i=h[u];i;i=dx[i].next)
	{
		if(dx[i].v==fa)
			continue;
		dfs(dx[i].v,u);
		siz[u]+=siz[dx[i].v];
	}
	cnt[u-1]=siz[u]*(n-siz[u]);
}
int main()
{
	scanf("%lld",&T);
	while(T--)
	{
		scanf("%lld",&n);
		tot=0;
		memset(h,0,sizeof(h));
		for(long long i=1;i<n;i++)
		{
			long long a,b;
			scanf("%lld%lld",&a,&b);
			add(a,b);
			add(b,a);
		}
		scanf("%lld",&m);
		for(long long i=1;i<=m;i++)
			scanf("%lld",&p[i]);
		if(m<n)
		{
			for(long long i=m+1;i<n;i++)
				p[i]=1;
			sort(p+1,p+n);
		}
		else
		{
			sort(p+1,p+m+1);
			for(long long i=m;i>=n;i--)
				p[i-1]=(p[i-1]*p[i])%mo;
		}
		dfs(1LL,1LL);
		sort(cnt+1,cnt+n);
		long long ans=0;
		for(long long i=1;i<n;i++)
			ans=(ans+(p[i]%mo)*(cnt[i]%mo)%mo)%mo;
		printf("%lld\n",ans);
	}
	return 0;
}


#include<bits/stdc++.h>
#define mod 1000000007
#define int long long
using namespace std;
int n,m;
int g[100010],num[100010];
int head[100010],cnt=0;
struct ss{
	int to,next;
}e[100010];
inline void add(int a,int b){
	cnt++;
	e[cnt].to=b;
	e[cnt].next=head[a];
	head[a]=cnt;
}
int dfs(int u,int f){
	int sum=1;
	for(int i=head[u];i!=0;i=e[i].next){
		if(e[i].to==f){
			continue;
		}
		sum+=dfs(e[i].to,u);
	}
	num[u]=sum*(n-sum);
	return sum;
}
bool cmp(int a,int b){
	return a>=b;
}
signed main(){
	int t;
	scanf("%lld",&t);
	while(t--){
		memset(head,0,sizeof(head));
		memset(e,0,sizeof(e));
		memset(num,0,sizeof(num));
		cnt=0;
		scanf("%lld",&n);
		for(int i=1,a,b;i<n;i++){
			scanf("%lld%lld",&a,&b);
			add(a,b);
			add(b,a);
			g[i]=1;
		}
		g[n]=1;
		scanf("%lld",&m);
		for(int i=1;i<=m;i++){
			scanf("%lld",&g[i]);
		}
		sort(g+1,g+m+1,cmp);
		for(int i=n+1;i<=m;i++){
			g[1]+=g[i];
			g[i]=1;
		}
		int yy=dfs(1,0);
		int ans=0;
		sort(num+1,num+n+1,cmp);
		for(int i=1;i<=n;i++){
		//	printf("%d  %d\n",g[i],num[i]);
			ans+=(num[i]*g[i])%mod;
			ans=ans%mod;
		}
		printf("%lld\n",ans);
	}
	return 0;
}
//Wrong Answer. wrong answer 2nd numbers differ - expected: '667739716', found: '14340226'


#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=114514;
const int mod=1e9+7;
vector<int> v[N];
ll kkr,e[N],q[N];
int msk,n,m,sz[N],tot;
void dfs(int x,int f){sz[x]=1;
	for(int i=0,to;i<v[x].size();i++){to=v[x][i];
		if(to==f)continue;dfs(to,x);sz[x]+=sz[to];
		e[++msk]=(ll)sz[to]*(n-sz[to]);
	}
}bool cmp(ll a,ll b){return a>b;}
int main(){int t;cin>>t;while(t--){
	for(int i=1;i<=n;i++)v[i].clear();
	scanf("%d",&n);msk=0;kkr=0;
	for(int i=1,x,y;i<n;i++){
		scanf("%d%d",&x,&y);v[x].push_back(y);v[y].push_back(x);
	}dfs(1,0);sort(e+1,e+msk+1,cmp);
	scanf("%d",&m);
	for(int i=1;i<=m;i++)scanf("%lld",&q[i]);
	sort(q+1,q+m+1,cmp);tot=m;
	for(int i=1;i<=n;i++)e[i]%=mod;
	if(m<=n-1){
		for(int i=1;i<=m;i++)kkr=(kkr+q[i]*e[i])%mod;
		for(int i=m+1;i<=n-1;i++)kkr=(kkr+e[i])%mod;
		printf("%lld\n",kkr);continue;
	}for(int i=n-1;i>=2;i--)kkr=(kkr+q[tot--]*e[i])%mod;
	while(tot) e[1]=e[1]*q[tot--]%mod;
	kkr=(kkr+e[1])%mod;
	printf("%lld\n",kkr);
}return 0;
}


#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
long long n,m,a,b,c[60001],head[100001],cnt,s=1,ans,t,size[100001];
priority_queue <int,vector<int>,less<int> >q;
struct edge
{
	int to,w,next;
}e[100001];
void add(int a,int b)
{
	e[cnt].to=b;
	e[cnt].next=head[a];
	head[a]=cnt++;
}
void dfs1(int u,int fa)
{
	for(int i=head[u];i!=-1;i=e[i].next)
	{
		int v=e[u].to;
		if(v==fa)continue;
		dfs1(v,u);
		size[v]+=size[u];
	}
}
void dfs2(int u,int fa)
{
	for(int i=head[u];i!=-1;i=e[i].next)
	{
		int v=e[u].to;
		if(v==fa)continue;
		dfs2(v,u);
		q.push(size[u]*(n-size[u]));
	}
}
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		cnt=0;
		s=1;
		ans=0;
		memset(head,-1,sizeof(head));
		scanf("%d",&n);
		for(int i=1;i<n;i++)
		{
			scanf("%d%d",&a,&b);
			add(a,b);
			add(b,a);
		}
		scanf("%d",&m);
		for(int i=1;i<=m;i++)scanf("%lld",&c[i]);
		sort(c+1,c+m+1);
		dfs1(1,0);
		dfs2(1,0);
		n--;
		if(m>n)
		{
			for(int i=n+1;i<=m;i++)(c[n]*=c[i])%=1000000007;
			m=n;
		}
		while(!q.empty())
		{
			int d=q.top();
			q.pop();
			ans+=max(1ll,c[m--]*d%1000000007);
			ans%=1000000007;
			if(m<0) m=0;
		}
		printf("%lld",ans);
	}
	return 0;
}


#include<map>
#include<list>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define in inline
#define ll long long
#define INF 2147483647
#define ri register int
#define rc register char
using namespace std;
in int read(){
	rc c=' ';
	ri v=0,x=1;
	while((c<'0')||(c>'9')){if(c=='-') x=-1;c=getchar();}
	while((c>='0')&&(c<='9')){v=(v<<1)+(v<<3)+(c^48);c=getchar();}
	return v*x;
}
in void write(ll x){
	if(x<0) x=-x,putchar('-');
	if(x>9) write(x/10);
	putchar(x%10^48);
}
const int N=200010,mod=1e9+7;
int t,n,m,cnt,tot,head[N],siz[N],v[N],nxt[N];
ll ans,w1[N],w2[N];
in void add(ri u,ri vv){
	v[++cnt]=vv;
	nxt[cnt]=head[u];
	head[u]=cnt;
}
void dfs(ri u,ri fa){
	siz[u]=1;
	for(ri i=head[u];i;i=nxt[i])
		if(v[i]!=fa) dfs(v[i],u),siz[u]+=siz[v[i]];
	if(u!=1) w2[++tot]=(n-siz[u])*siz[u];
}
in bool cmp(ll x,ll y){
	return x>y;
}
int main(){
	t=read();
	while(t--){
		n=read(),ans=cnt=tot=0;
		for(ri i=1;i<=n;i++)
			head[i]=w1[i]=w2[i]=0;
		for(ri i=1,u,v;i<=n-1;i++)
			u=read(),v=read(),add(u,v),add(v,u);
		dfs(1,0),m=read();
		for(ri i=1;i<=m;i++)
			w1[i]=read();
		sort(w1+1,w1+m+1,cmp);
		sort(w2+1,w2+tot+1,cmp);
		if(m<n){
			for(ri i=m+1;i<=n-1;i++)
				w1[i]=1;
		}
		else{
			for(ri i=n;i<=m;i++)
				w1[n-1]=w1[n-1]*w1[i]%mod;
		}
		for(ri i=1;i<=n-1;i++)
			ans=(ans+w1[i]*w2[i])%mod;
		write(ans),putchar('\n');
	}
	return ~~(0-0);
}


#include<bits/stdc++.h>
using namespace std;
int j,k,m,n,s,t;
const int N=1e5+10;
struct fu{
	int v,next;
}a[N<<1];
int mod=1e9+7;
int head[N],c[N],size[N],fa[N];
long long val[N],p[N];
void work(int aa,int bb)
{
	a[++k].next=head[aa];
	head[aa]=k;
	a[k].v=bb;
}
void dfs(int u)
{
	int i;
	for(i=head[u];i;i=a[i].next)
	{
		int v=a[i].v;
		if(v==fa[u])
		continue;
		fa[v]=u;
		dfs(v);
		size[u]+=size[v];
	}
	size[u]++;
	val[u]=(long long)size[u]*(long long)(n-size[u]);
}
int main()
{
	scanf("%d",&t);
	int last=0;
	while(t--)
	{
		k=0;
		int i;
		for(i=1;i<=last;i++)
		head[i]=0,size[i]=0;
		scanf("%d",&n);
		int x,y;
		for(i=1;i<=n-1;i++)
		{
			scanf("%d%d",&x,&y);
			work(x,y);
			work(y,x);
		}
		int tmp;
		if(n-1!=0)
		tmp=n-1;
		else
		tmp=n;
		dfs(1);
		scanf("%d",&m);
		for(i=1;i<=m;i++)
		scanf("%lld",&p[i]);
		for(i=m+1;i<=n-1;i++)
		p[i]=1;
		m=max(m,n-1);
		sort(p+1,p+m+1);
		sort(val+1,val+n+1);
		long long ans=0;
		for(i=m-1;i>=n-1;i--)
		p[i]=p[i+1]*p[i]%mod;
		for(i=n;i>=2;i--)
		ans=(ans+val[i]%mod*p[i-1])%mod;
		printf("%lld\n",ans);
		last=n;
	}
	return 0;
}