#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> adj;
vector<int> child;
void dfs(int v, int p = -1) {
  child[v] = 1;
  for (int to : adj[v]) {
    if (to == p) continue;
    dfs(to, v);
    child[v] += child[to];
  }
}
void solve() {
  int n, m;
  cin >> n;
  adj = vector<vector<int>>(n);
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  cin >> m;
  vector<long long> p(m);
  for (long long &i : p) cin >> i;
  child = vector<int>(n);
  dfs(0);
  vector<long long> weights(n - 1);
  for (int i = 1; i < n; i++) {
    weights[i - 1] = child[i] * 1LL * (n - child[i]);
  }
  sort(weights.rbegin(), weights.rend());
  for (int i = 0; i < n - 1 - m; i++) {
    p.push_back(1);
  }
  sort(p.begin(), p.end());
  while ((int)p.size() > n - 1) {
    long long back = p.back();
    p.pop_back();
    p.back() = (p.back() * back) % 1000000007;
  }
  sort(p.rbegin(), p.rend());
  int ans = 0;
  for (int i = 0; i < n - 1; i++) {
    ans = (ans + (p[i] * weights[i]) % 1000000007);
    ans %= 1000000007;
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
}
