#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5 + 5;
ll mod = 1e9 + 7;
vector<int> g[N];
bool vis[N];
ll sz[N];
void dfs(int s) {
  vis[s] = true;
  sz[s] = 1;
  for (auto u : g[s]) {
    if (!vis[u]) {
      dfs(u);
      sz[s] += sz[u];
    }
  }
}
void solve() {
  ll n;
  cin >> n;
  for (int i = 1; i <= n; ++i) g[i].clear();
  fill(sz, sz + n + 1, 0);
  fill(vis, vis + n + 1, false);
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  int m;
  cin >> m;
  vector<ll> p(m);
  for (int i = 0; i < m; ++i) cin >> p[i];
  while (p.size() < n - 1) {
    p.push_back(1);
  }
  dfs(1);
  vector<ll> times;
  for (int i = 1; i <= n; ++i) {
    times.push_back(sz[i] * (n - sz[i]));
  }
  sort(times.rbegin(), times.rend());
  sort(p.rbegin(), p.rend());
  ll val = 1;
  while (p.size() > n - 1) {
    val = val * p[0];
    val %= mod;
    p.erase(p.begin());
  }
  p[0] = p[0] * val;
  p[0] %= mod;
  ll ans = 0;
  for (int i = 0; i < n - 1; ++i) {
    times[i] %= mod;
    p[i] %= mod;
    ans = ans + times[i] * p[i];
    ans %= mod;
  }
  cout << ans << '\n';
  return;
}
int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
