#include <bits/stdc++.h>
using namespace std;
const long long N = 3e5 + 5;
const long long mod = 1e9 + 7;
vector<vector<long long> > al;
vector<long long> sum;
vector<long long> mul;
long long n;
void dfs(long long u, long long p = -1) {
  sum[u] = 1;
  for (long long to : al[u]) {
    if (to == p) continue;
    dfs(to, u);
    sum[u] += sum[to];
  }
  if (u != 1) mul.push_back((sum[u] * (n - sum[u])));
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  long long t;
  cin >> t;
  while (t--) {
    cin >> n;
    al.clear();
    al.resize(n + 1);
    sum.clear();
    sum.resize(n + 1, 0);
    mul.clear();
    long long u, v;
    for (long long i = 1; i < n; ++i) {
      cin >> u >> v;
      al[u].push_back(v);
      al[v].push_back(u);
    }
    long long m;
    cin >> m;
    vector<long long> p(m);
    for (long long i = 0; i < m; ++i) {
      cin >> p[i];
    }
    dfs(1);
    sort(p.begin(), p.end());
    sort(mul.begin(), mul.end());
    assert(mul.size() == n - 1);
    long long res = 0;
    if (m <= n - 1) {
      while (p.size() < n - 1) {
        p.push_back(1);
      }
      sort(p.begin(), p.end());
      for (long long i = n - 2; i >= 0; --i) {
        res = (res + p[i] * mul[i]) % mod;
      }
    } else {
      long long j = 0;
      for (long long i = 0; i < n - 2; ++i) {
        res = (res + p[j++] * mul[i]) % mod;
      }
      long long tot = 1;
      while (j < m) {
        tot = (tot * p[j++]) % mod;
      }
      res = (res + tot * mul[n - 2]) % mod;
    }
    cout << (res + mod) % mod << endl;
  }
}
