#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;
long long mod(long long x) { return ((x % M + M) % M); }
long long add(long long a, long long b) { return mod(mod(a) + mod(b)); }
long long mul(long long a, long long b) { return mod(mod(a) * mod(b)); }
vector<long long> val;
vector<vector<long long>> adj;
long long n;
long long dfs(long long u, long long p) {
  long long ans = 1;
  for (auto v : adj[u]) {
    if (v != p) {
      long long nt = dfs(v, u);
      val.push_back(nt * (n - nt));
      ans += nt;
    }
  }
  return ans;
}
void solve() {
  cin >> n;
  val.clear();
  adj = vector<vector<long long>>(n);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(0LL, -1LL);
  sort(val.begin(), val.end(), greater<long long>());
  int m;
  cin >> m;
  vector<long long> fact(m);
  for (int i = 0; i < m; i++) cin >> fact[i];
  for (int i = m; i < n - 1; i++) fact.push_back(1LL);
  sort(fact.begin(), fact.end(), greater<long long>());
  long long ans = 0;
  if ((long long)fact.size() == n - 1) {
    for (int i = 0; i < n - 1; i++) {
      ans = add(ans, (long long)(mul(fact[i], val[i])));
    }
  } else {
    long long z = fact.size() - (n - 1);
    int x = 0;
    for (long long i = 0; i < (long long)fact.size(); i++) {
      if (i < (z))
        fact[i + 1] = mul(fact[i], fact[i + 1]);
      else
        ans = add(ans, (long long)(mul(fact[i], val[x++])));
    }
  }
  cout << ans << "\n";
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed;
  cout << setprecision(10);
  int t = 1;
  cin >> t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}
