#include <bits/stdc++.h>
using namespace std;
struct node {
  long long x;
  long long y;
  long long ans;
} a[200005];
int cmp(node xx, node yy) { return xx.ans < yy.ans; }
long long b[100005];
vector<int> q[100005];
long long vis[200005];
long long dp[200005];
long long mod = 1e9 + 7;
int dfs(int num) {
  vis[num] = 1;
  int sum = 1;
  for (int i = 0; i < q[num].size(); i++) {
    if (vis[q[num][i]] == 0) {
      sum = sum + dfs(q[num][i]);
    }
  }
  dp[num] = sum;
  return sum;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      vis[i] = 0;
      q[i].clear();
    }
    for (int i = 1; i <= n - 1; i++) {
      scanf("%lld%lld", &a[i].x, &a[i].y);
      q[a[i].x].push_back(a[i].y);
      q[a[i].y].push_back(a[i].x);
    }
    int nh = dfs(1);
    for (int i = 1; i <= n - 1; i++) {
      a[i].ans =
          min(dp[a[i].x], dp[a[i].y]) * (n - min(dp[a[i].x], dp[a[i].y]));
    }
    sort(a + 1, a + n, cmp);
    int m;
    scanf("%d", &m);
    for (int i = 1; i <= m; i++) {
      scanf("%lld", &b[i]);
    }
    if (m < n - 1) {
      for (int i = m + 1; i <= n - 1; i++) {
        b[i] = 1;
      }
      sort(b + 1, b + n);
    } else if (m > n - 1) {
      sort(b + 1, b + m + 1);
      long long nh = 1;
      for (int i = m; i >= n; i--) {
        nh = nh * b[i] % mod;
      }
      b[n - 1] = b[n - 1] * nh % mod;
      sort(b + 1, b + n - 1);
    }
    sort(b + 1, b + n);
    long long sum = 0;
    for (int i = 1; i <= n - 1; i++) {
      sum = (sum + (a[i].ans * b[i] % mod)) % mod;
    }
    printf("%lld\n", sum);
  }
  return 0;
}
