#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e5 + 10;
vector<long long> g[maxn];
vector<long long> vp;
long long n, m;
long long dfs(long long u, long long p) {
  long long ans = 1;
  for (long long v : g[u]) {
    if (v == p) {
      continue;
    }
    long long x = dfs(v, u);
    vp.push_back(x * (n - x));
    ans += x;
  }
  return ans;
}
void solve() {
  cin >> n;
  for (long long i = 1; i <= n - 1; ++i) {
    long long a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  cin >> m;
  vector<long long> p(m);
  for (long long i = 0; i < m; ++i) {
    cin >> p[i];
  }
  sort(p.begin(), p.end());
  long long temp = dfs(1, -1);
  sort(vp.begin(), vp.end());
  long long res = 0;
  long long i = n - 2;
  if (m <= n - 1) {
    for (; i >= 0; --i) {
      if (p.size() == 0) {
        break;
      }
      res += (vp[i] * p.back()) % 1000000007;
      res %= 1000000007;
      p.pop_back();
    }
    while (i >= 0) {
      res += vp[i];
      res %= 1000000007;
      --i;
    }
  } else {
    long long temp = 1;
    long long z = (m - (n - 1));
    z++;
    while (z--) {
      temp *= p.back();
      temp %= 1000000007;
      p.pop_back();
    }
    res += (vp[n - 2] * temp) % 1000000007;
    res %= 1000000007;
    i = n - 3;
    for (; i >= 0; --i) {
      res += (vp[i] * p.back()) % 1000000007;
      res %= 1000000007;
      p.pop_back();
    }
  }
  cout << res % 1000000007 << "\n";
  for (long long i = 1; i <= n; ++i) {
    g[i].clear();
  }
  vp.clear();
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long t;
  cin >> t;
  for (long long i = 1; i <= t; ++i) {
    solve();
  }
}
