#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e5 + 50, N = 60000 + 20, mod = 1e9 + 7, INF = 1e9 + 1,
                lg = 32;
long long sz[maxn];
vector<long long> num, p;
vector<pair<long long, long long> > edges[maxn];
void dfs(long long v, long long par, long long n) {
  sz[v] = 1;
  for (auto i : edges[v]) {
    long long u = i.first, id = i.second;
    if (u != par) {
      dfs(u, v, n);
      sz[v] += sz[u];
      num.push_back(1ll * sz[u] * (n - sz[u]));
    }
  }
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n;
    num.clear();
    p.clear();
    for (long long i = 0; i < n; ++i) {
      edges[i].clear();
      sz[i] = 0;
    }
    for (long long i = 0; i < n - 1; ++i) {
      long long x, y;
      cin >> x >> y;
      x--;
      y--;
      edges[x].push_back({y, i});
      edges[y].push_back({x, i});
    }
    dfs(0, -1, n);
    cin >> m;
    for (long long i = 0; i < m; ++i) {
      long long x;
      cin >> x;
      p.push_back(x);
    }
    sort(num.begin(), num.end());
    if (m <= n - 1) {
      long long ans = 0;
      long long need = (n - 1) - m;
      for (long long i = 0; i < need; ++i) {
        p.push_back(1ll);
      }
      sort(p.begin(), p.end());
      for (long long i = 0; i < n - 1; ++i) {
        ans = (ans + ((num[i] % mod) * p[i])) % mod;
      }
      cout << ans << '\n';
    } else {
      long long ans = 0;
      sort(p.begin(), p.end());
      long long bad = m - (n - 1);
      bad++;
      vector<long long> w;
      w.clear();
      long long curr = 1;
      for (long long i = 0; i < bad; ++i) {
        curr = (curr * p[m - 1 - i]) % mod;
      }
      for (long long i = 0; i < m - bad; ++i) {
        w.push_back(p[i]);
      }
      sort(w.begin(), w.end());
      w.push_back(curr);
      for (long long i = 0; i < n - 1; ++i) {
        ans = (ans + ((num[i] % mod) * (w[i] % mod))) % mod;
      }
      cout << ans << '\n';
    }
  }
}
