#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const long long mod = 1e9 + 7;
const long long MOD = 998244353;
const long long MAX = 2e5 + 1;
inline long long add(long long a, long long b) {
  return ((a % mod) + (b % mod)) % mod;
}
inline long long sub(long long a, long long b) {
  return ((a % mod) - (b % mod) + mod) % mod;
}
inline long long mul(long long a, long long b) {
  return ((a % mod) * (b % mod)) % mod;
}
long long pwr(long long x, long long n) {
  if (n == 0) {
    return 1;
  }
  if (n & 1) {
    return mul(x, pwr(mul(x, x), (n - 1) / 2));
  } else {
    return pwr(mul(x, x), n / 2);
  }
}
long long modInv(long long n) { return pwr(n, mod - 2); }
long long gcd(long long a, long long b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
long long n, m, a[MAX], subtree[MAX];
vector<long long> g[MAX];
long long dfs(long long u = 0, long long par = -1) {
  long long cnt = 1;
  for (auto v : g[u]) {
    if (v != par) {
      cnt += dfs(v, u);
    }
  }
  return subtree[u] = cnt;
}
void speed_solve() {
  cin >> n;
  for (long long i = 0; i < n; i++) g[i].clear();
  vector<pair<long long, long long>> edges;
  for (long long i = 0; i < n - 1; i++) {
    long long u, v;
    cin >> u >> v;
    u--, v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  cin >> m;
  for (long long i = 0; i < m; i++) cin >> a[i];
  sort(a, a + m);
  reverse(a, a + m);
  if (m < n - 1) {
    for (long long i = m; i < n - 1; i++) a[i] = 1;
  } else if (m > n - 1) {
    long long b[n - 1], k = 1;
    b[0] = 1;
    for (long long i = 0; i < m; i++) {
      if (i < m - n + 2) {
        b[0] = mul(b[0], a[i]);
      } else {
        b[k++] = a[i];
      }
    }
    for (long long i = 0; i < n - 1; i++) {
      a[i] = b[i];
    }
  }
  dfs();
  vector<long long> contribution(n - 1);
  for (long long i = 0; i < n - 1; i++) {
    contribution[i] = subtree[i + 1] * (n - subtree[i + 1]);
  }
  sort(contribution.begin(), contribution.end());
  reverse(contribution.begin(), contribution.end());
  long long ans = 0;
  for (long long i = 0; i < n - 1; i++) {
    ans = add(ans, mul(contribution[i], a[i]));
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long test_cases = 1;
  cin >> test_cases;
  for (long long i = 0; i < test_cases; i++) {
    speed_solve();
  }
  return 0;
}
