#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
long long M = 1e9 + 7;
std::vector<long long> e[N], sz(N);
std::vector<long long> es;
long long n;
void dfs(long long v, long long p) {
  sz[v]++;
  for (long long i : e[v]) {
    if (i != p) {
      dfs(i, v);
      es.push_back((n - sz[i]) * sz[i]);
      sz[v] += sz[i];
    }
  }
}
void solve() {
  long long x, y, m, ans = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) e[i].clear(), sz[i] = 0;
  es.clear();
  for (int i = 0; i < n - 1; i++) {
    cin >> x >> y;
    e[x].push_back(y);
    e[y].push_back(x);
  }
  dfs(1, 1);
  cin >> m;
  std::vector<long long> p(m);
  for (long long &i : p) cin >> i;
  while (p.size() < es.size()) {
    p.push_back(1);
  }
  sort((p).begin(), (p).end());
  sort((es).begin(), (es).end());
  for (int i = 0; i < es.size() - 1; i++) {
    ans = (ans + es[i] * p[i] % M) % M;
  }
  long long tot = 1;
  for (int i = es.size() - 1; i < p.size(); i++) {
    tot = tot * p[i] % M;
  }
  cout << (ans + tot * es.back() % M) % M << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long TESTS = 1;
  cin >> TESTS;
  while (TESTS--) {
    solve();
  }
  return 0;
}
