#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 1;
long long mod = 1e9 + 7;
long long n, m, k;
vector<int> adj[N];
long long sz[N];
void dfs(int id, int p) {
  sz[id] = 1;
  for (auto c : adj[id]) {
    if (c == p) continue;
    dfs(c, id);
    sz[id] += sz[c];
  }
}
long long pr[N];
void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) adj[i].clear();
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++) {
    sz[i] = sz[i] * (n - sz[i]);
  }
  sort(sz + 1, sz + n + 1);
  cin >> m;
  for (int i = 1; i <= m; i++) cin >> pr[i];
  pr[++m] = 1;
  while (m < n) {
    pr[++m] = 1;
  }
  sort(pr + 1, pr + m + 1);
  while (m > n) {
    pr[m - 1] = pr[m - 1] * pr[m] % mod;
    m--;
  }
  long long ans = 0;
  for (int i = 2; i <= n; i++) {
    ans += sz[i] % mod * pr[i];
    ans %= mod;
  }
  cout << ans % mod << '\n';
}
int main() {
  int t;
  cin >> t;
  while (t--) solve();
}
