#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
const long long int cmod = 998244353;
const long long int N = 3e5 + 5;
const long long int inf = 1e16 + 2;
vector<long long int> graph[N];
long long int p[N], s[N];
long long int dfs(long long int idx, long long int par) {
  for (auto x : graph[idx]) {
    if (x == par) continue;
    p[idx] += dfs(x, idx);
  }
  p[idx]++;
  return p[idx];
}
long long int n;
void dfs2(long long int idx, long long int par) {
  s[idx] = (n - p[idx]) * (p[idx]);
  for (auto x : graph[idx]) {
    if (x == par) continue;
    dfs2(x, idx);
  }
}
void solve(long long int Case) {
  cin >> n;
  for (long long int i = 0; i < n; i++) p[i] = 0, s[i] = 0, graph[i].clear();
  for (long long int i = 0; i < n - 1; i++) {
    long long int a, b;
    cin >> a >> b;
    a--, b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  long long int k;
  cin >> k;
  priority_queue<long long int> K;
  for (long long int i = 0; i < k; i++) {
    long long int a;
    cin >> a;
    K.push(a);
  }
  if (k < n - 1) {
    for (long long int i = k; i < n - 1; i++) K.push(1);
  } else {
    long long int a = 1;
    while (K.size() >= n - 1) {
      a = (a * K.top()) % mod;
      K.pop();
    }
    K.push(a);
  }
  dfs(0, -1);
  dfs2(0, -1);
  vector<long long int> v;
  for (long long int i = 1; i < n; i++) v.push_back(s[i]);
  sort(v.rbegin(), v.rend());
  long long int ans = 0;
  for (auto x : v) {
    long long int a = K.top();
    ans = (ans + ((x) % mod * a % mod) % mod) % mod;
    K.pop();
  }
  cout << ans << '\n';
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int testcase = 1;
  cin >> testcase;
  long long int Case = 1;
  while (testcase--) {
    solve(Case++);
  }
  return 0;
}
