#include <bits/stdc++.h>
using namespace std;
const int64_t N = 1e6 + 6, M = 1e9 + 7;
vector<vector<int64_t>> g;
vector<int64_t> a, t;
int64_t n;
void dfs(int64_t v) {
  a[v] = 1;
  for (int64_t e : g[v])
    if (!a[e]) {
      dfs(e);
      a[v] += a[e];
      t.push_back(a[e] * (n - a[e]));
    }
}
int main() {
  ios::sync_with_stdio(false), cin.tie(nullptr);
  int T;
  cin >> T;
  while (T--) {
    cin >> n;
    g.clear();
    g.resize(n + 1);
    a.clear();
    a.resize(n + 1);
    t.clear();
    for (int64_t i = 1; i < n; i++) {
      int64_t u, v;
      cin >> u >> v;
      g[u].push_back(v);
      g[v].push_back(u);
    }
    int64_t i = 0, m, ans = 0, x = 1;
    cin >> m;
    vector<int64_t> p(m);
    for (int64_t &e : p) cin >> e;
    if (m < n) p.resize(n - 1, 1), m = n - 1;
    dfs(1);
    sort(p.begin(), p.end());
    sort(t.begin(), t.end());
    for (; i < n - 2; i++) ans = (ans + (t[i] % M * p[i] % M)) % M;
    for (; i < m; i++) x = (x * p[i]) % M;
    cout << (ans + t.back() * x % M) % M << '\n';
  }
}
