#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
long long t, n, m, p[N], d[N], a[N];
vector<long long> adj[N], v;
long long dfs(long long s, long long pre) {
  p[s] = 1;
  for (auto u : adj[s]) {
    if (u != pre) {
      p[s] += dfs(u, s);
    }
  }
  if (s > 1) v.push_back(p[s] * (n - p[s]));
  return p[s];
}
void rest() {
  for (int i = 1; i <= n; i++) {
    adj[i].clear();
  }
  v.clear();
}
int solve() {
  scanf("%lld", &t);
  while (t--) {
    scanf("%lld", &n);
    for (int i = 0; i < n - 1; i++) {
      long long u, v;
      scanf("%lld%lld", &u, &v);
      adj[u].push_back(v);
      adj[v].push_back(u);
    }
    scanf("%lld", &m);
    for (int i = 0; i < m; i++) {
      scanf("%lld", &a[i]);
    }
    sort(a, a + m);
    long long ret = dfs(1, 1), res = 0;
    sort(v.begin(), v.end());
    long long sz = v.size();
    if (m <= sz) {
      int i, j;
      for (i = m - 1, j = sz - 1; i >= 0; i--, j--) {
        res = (res + (v[j] * a[i]) % 1000000007) % 1000000007;
      }
      while (j >= 0) {
        res = (res + v[j]) % 1000000007;
        j--;
      }
    } else {
      int i, j;
      for (i = 0, j = 0; j < sz - 1; i++, j++) {
        res = (res + (v[j] * a[i]) % 1000000007) % 1000000007;
      }
      long long x = 1;
      while (i < m) {
        x = (x * a[i]) % 1000000007;
        i++;
      }
      res = (res + (x * v[sz - 1]) % 1000000007) % 1000000007;
    }
    printf("%lld\n", res);
    rest();
  }
  return 0;
}
int main() { return solve(); }
