#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
vector<vector<long long>> edges;
vector<long long> multi;
long long dfs(long long root, long long parent = -1) {
  long long count = 0;
  for (auto& child : edges[root]) {
    if (child == parent) continue;
    long long temp = dfs(child, root);
    count += temp;
    multi.push_back(temp);
  }
  return count + 1;
}
void solve() {
  long long n;
  cin >> n;
  edges.clear();
  edges.resize(n, vector<long long>(0));
  multi.clear();
  for (int i = 0, _n = (n - 1); i < _n; i++) {
    long long u, v;
    cin >> u >> v;
    u--;
    v--;
    edges[u].push_back(v);
    edges[v].push_back(u);
  }
  dfs(0);
  for (int i = 0, _n = (multi.size()); i < _n; i++) {
    multi[i] *= (n - multi[i]);
  }
  sort((multi).begin(), (multi).end(), greater<long long>());
  for (int i = 0, _n = (n - 1); i < _n; i++) multi[i] %= MOD;
  long long m;
  cin >> m;
  vector<long long> factors(m);
  for (int i = 0, _n = (m); i < _n; i++) cin >> factors[i];
  sort((factors).begin(), (factors).end(), greater<long long>());
  long long ans = 0;
  if (factors.size() <= n - 1) {
    for (int i = 0, _n = (factors.size()); i < _n; i++) {
      ans = (ans % MOD) + (factors[i] * multi[i]) % MOD;
      ans %= MOD;
    }
    for (int i = (factors.size()), _b = (n - 2); i <= _b; i++) {
      ans += (multi[i]) % MOD;
      ans %= MOD;
    }
  } else {
    long long extra = factors.size() - n + 1;
    long long first = 1;
    for (int i = 0, _n = (extra); i < _n; i++) {
      first *= factors[i];
      first %= MOD;
    }
    factors[extra] *= first;
    factors[extra] %= MOD;
    for (int i = 0, _n = (n - 1); i < _n; i++) {
      ans = (ans % MOD + (multi[i] * factors[extra + i]) % MOD) % MOD;
      ans %= MOD;
    }
  }
  cout << (ans + MOD) % MOD << '\n';
  return;
}
int main(int argc, char** argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
