#include <bits/stdc++.h>
using namespace std;
vector<long long int> v[100005];
long long int n;
vector<long long int> ed;
long long int dfs(long long int i, long long int p) {
  long long int sum = 1;
  for (long long int j = 0; j < v[i].size(); ++j) {
    long long int node = v[i][j];
    if (node == p) continue;
    long long int tot = dfs(node, i);
    sum += tot;
    ed.push_back(tot * (n - tot));
  }
  return sum;
}
void solve() {
  cin >> n;
  for (long long int i = 1; i < n + 1; ++i) v[i].clear();
  for (long long int i = 0; i < n - 1; ++i) {
    long long int x, y;
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  long long int m;
  cin >> m;
  vector<long long int> prime;
  for (long long int i = 0; i < m; ++i) {
    long long int x;
    cin >> x;
    prime.push_back(x);
  }
  ed.clear();
  long long int sum = dfs(1, 0);
  sort(prime.begin(), prime.end());
  sort(ed.begin(), ed.end());
  if (m > (n - 1)) {
    vector<long long int> temp;
    for (long long int i = 0; i < n - 2; ++i) {
      temp.push_back(prime[i]);
    }
    long long int pw = 1;
    for (long long int i = n - 2; i < m; ++i) pw = (pw * prime[i]) % 1000000007;
    temp.push_back(pw);
    prime = temp;
  } else if (m < n - 1) {
    long long int diff = n - 1 - m;
    for (long long int i = 0; i < diff; ++i) prime.push_back(1);
    sort(prime.begin(), prime.end());
  }
  long long int ans = 0;
  for (long long int i = 0; i < n - 1; ++i) {
    ed[i] = (ed[i]) % 1000000007;
    long long int temp = (prime[i] * (ed[i])) % 1000000007;
    ans = (ans + temp) % 1000000007;
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return (0);
}
