#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const long long INF_LL = 0x3f3f3f3f3f3f3f3f;
vector<int> a[N];
long long p[N];
int siz[N];
vector<long long> f;
int n;
void DFS(int u, int pa) {
  siz[u] = 1;
  for (auto v : a[u]) {
    if (v == pa) continue;
    DFS(v, u);
    siz[u] += siz[v];
    f.push_back(1LL * siz[v] * (n - siz[v]));
  }
}
void Solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) a[i].clear();
  f.clear();
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    a[u].push_back(v);
    a[v].push_back(u);
  }
  int m;
  cin >> m;
  for (int i = 1; i <= m; i++) cin >> p[i];
  for (int i = m + 1; i <= n - 1; i++) p[i] = 1;
  m = max(m, n - 1);
  DFS(1, 0);
  sort(f.begin(), f.end());
  sort(p + 1, p + m + 1);
  for (int i = (n - 1) + 1; i <= m; i++) p[n - 1] = p[n - 1] * p[i] % MOD;
  long long ans = 0;
  for (int i = 1; i < n; i++) {
    p[i] %= MOD;
    ans = (ans + f[i - 1] % MOD * p[i]) % MOD;
  }
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(0);
  int t;
  cin >> t;
  while (t--) Solve();
  return 0;
}
