#include <bits/stdc++.h>
using namespace std;
long long int n;
vector<long long int> adj[(long long int)(1e5 + 5)], d;
long long int dfs(long long int c, long long int pre) {
  long long int a = 1;
  for (auto x : adj[c]) {
    if (x == pre) continue;
    long long int b = dfs(x, c);
    d.push_back((n - b) * b);
    a += b;
  }
  return a;
}
void solve() {
  for (long long int i = 0; i <= (long long int)(1e5 + 5); i++) adj[i].clear();
  d.clear();
  cin >> n;
  for (long long int i = 1; i < n; i++) {
    long long int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(1, -1);
  long long int m;
  cin >> m;
  vector<long long int> pr(max(m, n), 1);
  for (long long int i = 0; i < m; i++) cin >> pr[i];
  sort(d.begin(), d.end());
  reverse(d.begin(), d.end());
  sort(pr.begin(), pr.end());
  reverse(pr.begin(), pr.end());
  long long int ans = 0, cur = 0;
  if (n - 1 < m)
    for (; cur < m - n + 1; cur++)
      pr[cur + 1] = (pr[cur + 1] * pr[cur]) % (long long int)(1e9 + 7);
  for (long long int i = 0; i < n - 1; i++)
    ans = (ans + (pr[i + cur] * d[i] % (long long int)(1e9 + 7))) %
          (long long int)(1e9 + 7);
  cout << ans << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t = 1;
  cin >> t;
  while (t--) solve();
  return 0;
}
