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