#include <bits/stdc++.h>
using namespace std;
vector<int> e[100010];
long long ans;
int a[100010], s[100010], n, m, T, u, v;
long long b[100010];
const int mo = 1000000007;
void dfs(int x, int p) {
  s[x] = 1;
  for (auto i : e[x]) {
    if (i != p) {
      dfs(i, x);
      s[x] += s[i];
      b[++b[0]] = (long long)s[i] * (n - s[i]);
    }
  }
}
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      e[i].clear();
    }
    for (int i = 1; i < n; i++) {
      scanf("%d%d", &u, &v);
      e[u].push_back(v);
      e[v].push_back(u);
    }
    scanf("%d", &m);
    for (int i = 1; i <= m; i++) {
      scanf("%d", &a[i]);
    }
    sort(a + 1, a + m + 1);
    if (m >= n) {
      while (m >= n) {
        a[m - 1] = (long long)a[m - 1] * a[m] % mo;
        m--;
      }
    }
    memset(s, 0, sizeof(s));
    b[0] = 0;
    dfs(1, 0);
    sort(b + 1, b + n);
    ans = 0;
    m++;
    for (int i = 1; i < n; i++) {
      if (i >= m)
        ans = (ans + b[n - i]) % mo;
      else {
        ans = (ans + (long long)b[n - i] * a[m - i]) % mo;
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
