#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
const long double Pi = acos(-1), e = 2.718, ebs = 0.00000001;
const int N = 5e3 + 10, mod = 1e9 + 7, SEED = 1331, B = 40;
vector<vector<long long> > G;
vector<bool> vis;
vector<long long> cnt, freq;
vector<pair<long long, long long> > E;
long long dfs(int u) {
  vis[u] = 1;
  for (auto v : G[u]) {
    if (!vis[v]) cnt[u] += dfs(v);
  }
  return cnt[u];
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    G.assign(n + 1, vector<long long>());
    vis.assign(n + 1, 0);
    cnt.assign(n + 1, 1);
    E.clear();
    freq.clear();
    for (int i = 1; i < n; i++) {
      long long u, v;
      cin >> u >> v;
      G[u].push_back(v);
      G[v].push_back(u);
      E.push_back({u, v});
    }
    int m;
    cin >> m;
    vector<long long> v(m);
    for (auto &x : v) cin >> x;
    if (m < n - 1) {
      while (v.size() < n - 1) v.push_back(1);
    }
    sort(v.begin(), v.end());
    if (m > n - 1) {
      long long x = 1;
      for (int i = n - 2; i < m; i++) {
        x *= v[i];
        x %= mod;
      }
      v.erase(v.begin() + n - 2, v.end());
      v.push_back(x);
    }
    dfs(1);
    long long ans = 0;
    for (auto e : E) {
      long long x = min(cnt[e.first], cnt[e.second]);
      freq.push_back(x * (n - x));
    }
    sort(freq.begin(), freq.end());
    for (int i = 0; i < n - 1; i++) {
      ans += freq[i] * v[i];
      ans %= mod;
    }
    cout << ans << '\n';
  }
  return 0;
}
