#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3, "Ofast", "inline")
using namespace std;
const int N = 1e5 + 10;
const int mod = 1e9 + 7;
vector<int> v[N];
long long d[N], a[N], p[N];
long long k, n;
void dfs(int x, int fa) {
  for (auto g : v[x]) {
    if (g != fa) {
      dfs(g, x);
      a[x] += a[g];
      k++;
      d[k] = ((n - a[g]) * a[g]);
    }
  }
}
int main() {
  int t;
  scanf("%lld", &t);
  while (t--) {
    k = 0;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      a[i] = 1;
      v[i].clear();
    }
    for (int i = 1; i <= n - 1; i++) {
      int u, w;
      scanf("%d%d", &u, &w);
      v[u].push_back(w);
      v[w].push_back(u);
    }
    dfs(1, 1);
    int m;
    scanf("%d", &m);
    for (int i = 1; i <= m; i++) {
      scanf("%lld", &p[i]);
    }
    sort(p + 1, p + 1 + m);
    if (m > n - 1) {
      for (int i = m; i >= n; i--) {
        p[n - 1] = (p[n - 1] * p[i]) % mod;
      }
      m = n - 1;
    }
    sort(d + 1, d + 1 + n - 1);
    long long ans = 0;
    for (int i = n - 1; i >= 1; i--) {
      if (m >= 1) {
        ans = (ans + (d[i] * p[m]) % mod) % mod;
        m--;
      } else {
        ans = (ans + d[i]) % mod;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
