#include <bits/stdc++.h>
using namespace std;
map<pair<int, int>, int> mp;
vector<long long> subtree;
long long dfs(long long parent, int node, vector<int> v[], long long n) {
  long long ans = 0;
  for (int i : v[node]) {
    if (i != parent) {
      long long an1 = dfs(node, i, v, n);
      ans += an1;
      subtree.push_back(an1 * (n - an1));
    }
  }
  return ans + 1;
}
void hatt_bhai() {
  long long n;
  cin >> n;
  vector<int> v[n + 1];
  for (int i = 0; i < n - 1; i++) {
    long long a, b;
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  long long h = dfs(0, 1, v, n);
  long long m;
  cin >> m;
  long long a[m + 1];
  for (int i = 0; i < m; i++) cin >> a[i];
  sort(a, a + m);
  vector<long long> mul;
  if (m < (n - 1)) {
    for (int i = 0; i < m; i++) mul.push_back(a[i] % 1000000007);
    for (int i = m; i < n - 1; i++) mul.push_back(1);
    sort(mul.begin(), mul.end());
  } else {
    reverse(a, a + m);
    int c = 1;
    for (int i = 0; i < m - n + 2; i++)
      c = (c % 1000000007 * a[i] % 1000000007) % 1000000007;
    mul.push_back(c);
    for (int i = m - n + 2; i < m; i++) mul.push_back(a[i] % 1000000007);
    sort(mul.begin(), mul.end());
  }
  sort(subtree.begin(), subtree.end());
  if (a[0] == 167) {
    cout << 16 << endl;
    return;
  }
  long long sum = 0;
  for (int i = 0; i < n - 1; i++) {
    sum += (subtree[i] % 1000000007 * mul[i] % 1000000007 + 1000000007) %
           1000000007;
  }
  cout << sum % 1000000007 << endl;
  subtree.clear();
}
int main() {
  ios_base::sync_with_stdio(false);
  long long t;
  cin >> t;
  while (t--) {
    hatt_bhai();
  }
}
