#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;
const int N = 1e5 + 5;
long long int n, m;
vector<long long int> adj[N];
long long int a[N];
long long int cnt[N];
void dfs(long long int ch, long long int pa) {
  cnt[ch] = 1;
  for (auto f : adj[ch]) {
    if (pa == f) continue;
    dfs(f, ch);
    cnt[ch] += cnt[f];
  }
}
int main() {
  long long int t;
  cin >> t;
  while (t--) {
    for (long long int i = 1; i <= n; ++i) adj[i].clear();
    cin >> n;
    vector<pair<long long int, long long int>> vi;
    for (long long int i = 0; i < n - 1; ++i) {
      long long int u, v;
      cin >> u >> v;
      vi.push_back({u, v});
      adj[u].push_back(v);
      adj[v].push_back(u);
    }
    cin >> m;
    for (long long int i = 0; i < m; ++i) cin >> a[i];
    sort(a, a + m);
    if (m > n - 1) {
      long long int j = n - 1;
      while (j < m) {
        a[n - 2] = (a[n - 2] * a[j]) % M;
        ++j;
      }
    }
    dfs(1, 0);
    priority_queue<long long int> q;
    for (auto g : vi) {
      long long int c = min(cnt[g.first], cnt[g.second]);
      q.push(c * (n - c));
    }
    long long int ans = 0;
    long long int k = min(n - 2, m - 1);
    while (!q.empty() || k >= 0) {
      if (!q.empty() && k >= 0) {
        long long int f = (q.top()) % M;
        long long int r = a[k] % M;
        q.pop();
        --k;
        r = (r * f) % M;
        ans += r;
        ans %= M;
      } else if (!q.empty()) {
        while (!q.empty()) {
          ans += q.top();
          ans %= M;
          q.pop();
        }
      }
    }
    cout << ans << "\n";
  }
}
