#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
vector<long long> vec[100005];
long long num[60005];
vector<long long> cnt;
long long size[100005];
long long n;
void dfs(long long u, long long fa) {
  size[u] = 1;
  for (long long i = 0; i < vec[u].size(); i++) {
    if (vec[u][i] == fa) continue;
    dfs(vec[u][i], u);
    size[u] += size[vec[u][i]];
  }
  cnt.push_back((n - size[u]) * size[u]);
}
bool cmp(long long a, long long b) { return a > b; }
int main() {
  long long t;
  scanf("%lld", &t);
  while (t--) {
    scanf("%lld", &n);
    for (long long i = 0; i <= n; i++) vec[i].clear();
    cnt.clear();
    for (long long i = 0; i < n - 1; i++) {
      long long u, v;
      scanf("%lld%lld", &u, &v);
      vec[u].push_back(v);
      vec[v].push_back(u);
    }
    dfs(1, 0);
    sort(cnt.begin(), cnt.end(), cmp);
    long long m;
    scanf("%lld", &m);
    for (long long i = 0; i < m; i++) scanf("%lld", &num[i]);
    sort(num, num + m, cmp);
    long long p = 0;
    n--;
    long long sum = 0;
    if (m - n > 0) {
      while (p < m - n) {
        num[p + 1] = num[p] * num[p + 1] % mod;
        p++;
      }
      for (int i = p; i < m; i++) {
        sum = (sum + num[i] * cnt[i - p] % mod) % mod;
      }
    } else {
      for (long long i = 0; i < n; i++) {
        if (i >= m)
          sum = (sum + cnt[i]) % mod;
        else
          sum = (sum + num[i] * cnt[i] % mod) % mod;
      }
    }
    printf("%lld\n", sum);
  }
  return 0;
}
