#include <bits/stdc++.h>
using namespace std;
vector<int> adj[100000];
vector<long long> nvs, pr;
long long n, m;
bool vis[100000];
long long dfs(int node, int child) {
  long long ans = 1, rv = 0;
  for (int i = 0; i < adj[node].size(); i++) {
    if (adj[node][i] == child) continue;
    long long k = dfs(adj[node][i], node);
    rv += k;
    nvs.push_back((k * (n - k)));
  }
  return rv + 1;
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    nvs.clear();
    pr.clear();
    cin >> n;
    for (int i = 1; i < n; i++) {
      int u, v;
      cin >> u >> v;
      adj[u - 1].push_back(v - 1);
      adj[v - 1].push_back(u - 1);
    }
    cin >> m;
    for (int i = 0; i < n; i++)
      if (adj[i].size() == 1) {
        long long a = dfs(i, -1);
        break;
      }
    for (int i = 0; i < m; i++) {
      long long p;
      cin >> p;
      pr.push_back(p);
    }
    sort(pr.begin(), pr.end());
    for (int i = 0; i < n; i++) adj[i].clear();
    while (pr.size() > n - 1) {
      long long t = *pr.rbegin();
      pr.pop_back();
      *pr.rbegin() *= t;
      *pr.rbegin() %= ((long long)1e9 + 7);
    }
    sort(nvs.begin(), nvs.end());
    int j = pr.size() - 1;
    long long ans = 0;
    for (int i = n - 2; i >= 0; i--, j--) {
      ans += ((nvs[i]) * (j < 0 ? 1 : pr[j])) % ((long long)1e9 + 7);
      ans %= ((long long)1e9 + 7);
    }
    cout << ans << endl;
  }
  return 0;
}
