#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
vector<vector<int>> adj;
vector<long long> subtree;
int dfs(int v, int parent) {
  int sum = 1;
  for (auto u : adj[v])
    if (u != parent) {
      subtree.push_back(dfs(u, v));
      sum += subtree.back();
    }
  return sum;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    adj = vector<vector<int>>(n + 1);
    for (int i = 1; i < n; i++) {
      int u, v;
      cin >> u >> v;
      --u, --v;
      adj[u].push_back(v);
      adj[v].push_back(u);
    }
    dfs(0, -1);
    for (auto &it : subtree) {
      it = 1LL * it * (n - it);
    }
    sort(subtree.begin(), subtree.end());
    int m;
    cin >> m;
    vector<int> primes(m);
    for (auto &x : primes) cin >> x;
    n = n - 1;
    if (m < n) {
      int dif = n - m;
      for (int i = 0; i < dif; i++) primes.push_back(1), m++;
    }
    sort(primes.begin(), primes.end());
    if (n < m) {
      int dif = m - n;
      for (int i = 0; i < dif; i++) {
        int x = primes.back();
        primes.pop_back();
        primes[primes.size() - 1] = (1LL * primes[primes.size() - 1] * x) % mod;
      }
    }
    long long res = 0;
    for (int i = 0; i < n; i++) {
      res = (res + (1LL * subtree[i] % mod * primes[i] % mod)) % mod;
    }
    cout << res % mod << "\n";
    subtree.clear();
    adj.clear();
  }
}
