#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int N = 1e6 + 7;
const int INF = 1e9 + 7;
const int T = (1 << 20);
int n, k;
int dfs(int start, int przodek, vector<vector<int> >& G,
        vector<long long>& kraw) {
  int roz = 1;
  for (auto& u : G[start]) {
    if (u == przodek) continue;
    int pom = dfs(u, start, G, kraw);
    kraw.push_back(1ll * pom * (n - pom));
    roz += pom;
  }
  return roz;
}
void solve() {
  cin >> n;
  vector<vector<int> > G(n);
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  cin >> k;
  vector<long long> tab(k);
  for (auto& u : tab) cin >> u;
  sort(tab.begin(), tab.end());
  while ((int)tab.size() > n - 1) {
    tab[(int)tab.size() - 2] = tab[(int)tab.size() - 2] * tab.back() % INF;
    tab.pop_back();
  }
  reverse(tab.begin(), tab.end());
  while ((int)tab.size() < n - 1) tab.push_back(1);
  vector<long long> kraw;
  dfs(0, -1, G, kraw);
  sort(kraw.begin(), kraw.end());
  for (auto& u : kraw) u %= INF;
  reverse(kraw.begin(), kraw.end());
  long long ans = 0;
  for (int i = 0; i < n - 1; i++) ans = (ans + kraw[i] * tab[i]) % INF;
  cout << ans << '\n';
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
}
