#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
long long cl(long double a) {
  if (a > (long long)a) {
    return (long long)a + 1;
  } else {
    return (long long)a;
  }
}
long long flr(long double a) {
  if (a < 0.0) {
    return (long long)a - 1;
  }
  return (long long)a;
}
long long n, m, x, y;
const long long M = 1e5 + 100;
vector<long long> gr[M];
bool vis[M] = {false};
long long p[M];
vector<long long> op;
long long dp[M] = {0};
void dfs(long long cur) {
  vis[cur] = true;
  dp[cur] = 1;
  for (auto x : gr[cur]) {
    if (!vis[x]) {
      dfs(x);
      dp[cur] += dp[x];
      op.push_back(dp[x] * (n - dp[x]));
    }
  }
}
void solve() {
  cin >> n;
  op.clear();
  for (long long i = 0; i < n + 1; i++) {
    gr[i].clear();
    vis[i] = false;
    dp[i] = 0;
  }
  for (long long i = 0; i < n - 1; i++) {
    cin >> x;
    cin >> y;
    gr[x].push_back(y);
    gr[y].push_back(x);
  }
  dfs(1);
  cin >> m;
  for (long long i = 0; i < m; i++) cin >> p[i];
  sort(p, p + m, greater<long long>());
  sort(op.begin(), op.end(), greater<long long>());
  long long ans = 0;
  long long j = 0;
  long long i = 0;
  if (m > n - 1) {
    long long cur = 1;
    while (m - j > n - 2) {
      cur *= p[j];
      cur %= mod;
      j++;
    }
    ans += cur * op[0];
    ans %= mod;
    i++;
    while (i < op.size()) {
      ans += p[j] * op[i];
      ans %= mod;
      i++;
      j++;
    }
    cout << ans << "\n";
  } else {
    long long i = 0;
    while (i < op.size()) {
      if (i < m)
        ans += p[i] * op[i];
      else
        ans += op[i];
      ans %= mod;
      i++;
    }
    cout << ans << "\n";
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  t = 1;
  cin >> t;
  while (t--) solve();
}
