#include <bits/stdc++.h>
using namespace std;
long long read() {
  long long x = 0, F = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') F = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * F;
}
int ans;
int T, n, m, siz[1200000 + 5], num[1200000 + 5];
vector<long long> P;
vector<int> G[1200000 + 5];
int add(int a, int b) {
  return (a + b >= 1000000007) ? a + b - 1000000007 : a + b;
}
int dec(int a, int b) { return (a - b < 0) ? a - b + 1000000007 : a - b; }
int mul(int a, int b) { return (1LL * a * b) % 1000000007; }
void dfs(int x, int fa) {
  siz[x] = 1;
  for (int i = 0; i < G[x].size(); i++) {
    int v = G[x][i];
    if (v == fa) continue;
    dfs(v, x);
    P.push_back(1LL * siz[v] * (n - siz[v]));
    siz[x] += siz[v];
  }
}
int main() {
  T = read();
  while (T--) {
    P.clear(), ans = 0;
    n = read();
    for (int i = 1; i < n; i++) {
      int u = read(), v = read();
      G[u].push_back(v);
      G[v].push_back(u);
    }
    dfs(1, 0);
    sort(P.begin(), P.end());
    m = read();
    for (int i = 1; i <= m; i++) num[i] = read();
    sort(num + 1, num + m + 1);
    for (int i = m; i > n - 1; i--)
      num[i - 1] = mul(num[i - 1], num[i]), num[i] = 0;
    m = min(m, n - 1);
    reverse(num + 1, num + m + 1);
    for (int i = n - 1; i >= 1; i--)
      ans = add(ans, mul(num[n - i] ? num[n - i] : 1, P[i - 1] % 1000000007));
    printf("%d\n", ans);
    for (int i = 1; i <= n; i++) G[i].clear();
    for (int i = 1; i <= m; i++) num[i] = 0;
  }
}
