#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int n, m;
bool visited[100003];
long long A[100003];
long long sz[100003];
vector<int> vec[100003];
vector<long long> num;
void dfs(int u) {
  visited[u] = 1;
  sz[u] = 1;
  for (int v : vec[u]) {
    if (visited[v]) continue;
    dfs(v);
    sz[u] += sz[v];
    num.push_back(1LL * sz[v] * (n - sz[v]));
  }
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) vec[i].clear();
    for (int i = 1; i <= n; i++) visited[i] = 0;
    num.clear();
    for (int i = 1; i < n; i++) {
      int u, v;
      scanf("%d %d", &u, &v);
      vec[u].push_back(v);
      vec[v].push_back(u);
    }
    scanf("%d", &m);
    for (int i = 0; i < m; i++) scanf("%d", &A[i]);
    sort(A, A + m);
    dfs(1);
    sort(num.begin(), num.end());
    for (int i = 0; i < n - 1; i++) num[i] %= MOD;
    if (m > n - 1) {
      long long res = 0;
      for (int i = 0; i < n - 2; i++) {
        res += (1LL * A[i] * num[i]) % MOD;
        res %= MOD;
      }
      long long temp = 1;
      for (int i = n - 2; i < m; i++) temp = (1LL * temp * A[i]) % MOD;
      res = (res + temp * num[n - 2]) % MOD;
      printf("%lld\n", res);
    } else {
      long long res = 0;
      for (int i = 0; i < m; i++) {
        res += (1LL * A[m - 1 - i] * num[n - 2 - i]) % MOD;
        res %= MOD;
      }
      for (int i = m; i < n - 1; i++) {
        res += num[n - 2 - i];
        res %= MOD;
      }
      printf("%lld\n", res);
    }
  }
}
