#include <bits/stdc++.h>
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,abm,mmx,avx,avx2,popcnt,fma")
#pragma GCC optimize("fast-math")
using namespace std;
const long long mod = 1e9 + 7, mod2 = 998244353;
const long double eps = 1e-6;
const long long inf = 1e18;
const int base = 1e9;
const long double PI = 3.14159265358979;
int n, m, t;
vector<vector<int>> g;
vector<long long> edges;
long long p[100 * 1000];
unsigned int bitcount(unsigned long long val) {
  return __builtin_popcount(val) + __builtin_popcount(val >> 32);
}
void local() { freopen("test.in", "r", stdin); }
void remote() {}
void read() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cin >> t;
}
int dfs(int v, int p = -1) {
  if (g[v].size() == 1 && p != -1) return 1;
  int sz = 1;
  for (const int &e : g[v]) {
    if (e != p) {
      int val = dfs(e, v);
      edges.push_back(1ll * val * (n - val));
      sz += val;
    }
  }
  return sz;
}
int main() {
  remote();
  read();
  while (t--) {
    cin >> n;
    g.clear();
    edges.clear();
    g.resize(n);
    for (int i = 0; i < n - 1; i++) {
      int u, v;
      cin >> u >> v;
      u--;
      v--;
      g[u].push_back(v);
      g[v].push_back(u);
    }
    cin >> m;
    for (int i = 0; i < m; i++) cin >> p[i];
    for (int i = m; i < n - 1; i++) p[i] = 1;
    sort(p, p + m, greater<int>());
    dfs(0);
    sort(edges.begin(), edges.end(), greater<long long>());
    int start = 0;
    while (m > n - 1) {
      start++;
      p[start] *= p[start - 1], p[start] %= mod;
      m--;
    }
    long long res = 0;
    for (int i = 0; i < n - 1; i++) {
      res += edges[i] * p[start + i];
      res %= mod;
    }
    cout << res << '\n';
    ;
  }
}
