#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e5 + 5;
const long long mod = 1e9 + 7;
long long t, n, m, p, ans, num;
long long sz[maxn], head[maxn], cnt, v[maxn];
priority_queue<long long> q;
struct edge {
  long long to, next;
} e[maxn << 1];
bool cmp(long long x, long long y) { return x > y; }
void adde(long long u, long long v) {
  e[++cnt].to = v;
  e[cnt].next = head[u];
  head[u] = cnt;
}
void dfs(long long u, long long fa) {
  sz[u] = 1;
  for (register long long i = head[u]; i; i = e[i].next) {
    long long w = e[i].to;
    if (w == fa) continue;
    dfs(w, u);
    sz[u] += sz[w];
    v[++num] = sz[w] * (n - sz[w]);
  }
}
signed main() {
  scanf("%lld", &t);
  while (t--) {
    memset(head, 0, sizeof(head));
    ans = 0;
    cnt = 0;
    num = 0;
    memset(sz, 0, sizeof(sz));
    memset(v, 0, sizeof(v));
    scanf("%lld", &n);
    for (register long long i = 1, u, w; i < n; ++i)
      scanf("%lld%lld", &u, &w), adde(u, w), adde(w, u);
    dfs(1, 0);
    scanf("%lld", &m);
    for (register long long i = 1; i <= m; ++i) scanf("%lld", &p), q.push(p);
    sort(v + 1, v + num + 1, cmp);
    p = q.top();
    q.pop();
    m--;
    while (n - 1 <= m) p = q.top() % mod * p % mod, q.pop(), m--;
    m++;
    while (n - 1 > m) q.push(1ll), m++;
    ans = (ans + v[1] % mod * p % mod) % mod;
    for (register long long i = 2; i < n; ++i) {
      p = q.top(), q.pop();
      ans = (ans + v[i] % mod * p % mod) % mod;
    }
    printf("%lld\n", ans);
  }
  return 0;
}
