#include <bits/stdc++.h>
using namespace std;
void solve();
string conc(long long int a, long long int b) {
  return to_string(a) + "|" + to_string(b);
}
long long int rev(long long int x) {
  long long int ans = 0;
  while (x != 0) {
    ans = ans * 10 + (x % 10);
    x /= 10;
  }
  return ans;
}
long long int numdig(long long int x) {
  long long int ans = 0;
  while (x != 0) {
    ans++;
    x /= 10;
  }
  return ans;
}
void printvec(vector<long long int> &v) {
  for (long long int i = 0; i < v.size(); i++) cout << v[i] << " ";
  cout << "\n";
}
void printmat(vector<vector<long long int>> &mat) {
  for (long long int i = 0; i < mat.size(); i++) {
    for (long long int j = 0; j < mat[0].size(); j++) cout << mat[i][j] << " ";
    cout << "\n";
  }
}
void dfs(vector<vector<long long int>> &adj, vector<bool> &vis, long long int u,
         vector<long long int> &sub) {
  sub[u] = 1;
  vis[u] = true;
  for (auto v : adj[u]) {
    if (!vis[v]) {
      dfs(adj, vis, v, sub);
      sub[u] += sub[v];
    }
  }
}
void solve() {
  long long int n;
  cin >> n;
  vector<vector<long long int>> adj(n + 1);
  for (long long int i = 0; i < n - 1; i++) {
    long long int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  long long int m;
  cin >> m;
  vector<long long int> primes(m);
  for (long long int i = 0; i < m; i++) cin >> primes[i];
  vector<long long int> sub(n + 1, 0);
  vector<bool> vis(n + 1, false);
  dfs(adj, vis, 1, sub);
  vector<long long int> mults;
  for (long long int i = 2; i <= n; i++)
    mults.push_back((sub[i]) * (n - sub[i]));
  sort(mults.begin(), mults.end(), greater<long long int>());
  sort(primes.begin(), primes.end(), greater<long long int>());
  if (primes.size() < n - 1) {
    while (primes.size() != (n - 1)) primes.push_back(1);
  }
  long long int idx = 0;
  if (primes.size() > n - 1) {
    while (primes.size() - idx != n - 1) {
      primes[idx + 1] *= primes[idx];
      primes[idx + 1] %= 1000000007;
      idx++;
    }
  }
  long long int ans = 0;
  for (long long int i = 0; i < n - 1; i++) {
    long long int ta = (primes[idx++] % 1000000007) * (mults[i] % 1000000007);
    ta %= 1000000007;
    ans += ta;
    ans %= 1000000007;
  }
  cout << ans << "\n";
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t;
  t = 1;
  cin >> t;
  while (t--) solve();
  return 0;
}
