#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 100005;
int n;
int head[N];
int cnt = 0;
int a[N];
long long d[N];
long long b[N];
struct Edge {
  int to, next;
} edge[2 * N];
void init(int n) {
  for (int i = 0; i <= n; i++) {
    edge[i].next = -1;
    edge[i + n].next = -1;
    head[i] = -1;
    d[i] = 0;
  }
}
void addedge(int u, int v) {
  edge[cnt].to = v;
  edge[cnt].next = head[u];
  head[u] = cnt++;
}
void dfs(int u, int fa) {
  a[u] = 1;
  for (int i = head[u]; ~i; i = edge[i].next) {
    int to = edge[i].to;
    if (to == fa) continue;
    dfs(to, u);
    a[u] += a[to];
    d[to] = max(d[u], (long long)a[to] * (n - a[to]));
  }
}
bool cmp(long long a, long long b) { return a > b; }
int main() {
  int t;
  cin >> t;
  while (t--) {
    scanf("%d", &n);
    init(n);
    int u, v;
    for (int i = 1; i < n; i++) {
      scanf("%d %d", &u, &v);
      addedge(u, v);
      addedge(v, u);
    }
    dfs(1, -1);
    int m;
    cin >> m;
    for (int i = 1; i <= m; i++) {
      scanf("%lld", &b[i]);
    }
    sort(b + 1, b + 1 + m, cmp);
    sort(d + 1, d + 1 + n, cmp);
    long long ans = 0;
    if (n - 1 >= m) {
      for (int i = 1; i < n; i++) {
        if (i > m) b[i] = 1;
        ans = (ans + d[i] * b[i]) % mod;
      }
      cout << ans << endl;
    } else {
      long long ans = 0;
      long long temp = 1;
      for (int i = 1; i <= m - n + 2; i++) {
        temp = (temp * b[i]) % mod;
      }
      ans = (ans + d[1] * temp) % mod;
      for (int i = 2; i < n; i++) {
        ans = (ans + d[i] * b[i + m - n + 1]) % mod;
      }
      cout << ans << endl;
    }
  }
  return 0;
}
