#include <bits/stdc++.h>
using namespace std;
int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1};
int dy[] = {0, 1, 0, -1, -1, 1, -1, 1};
const int mod = 1e9 + 7;
long long n;
vector<vector<int>> arr;
vector<long long> p;
long long dfs(int now, int anc) {
  long long num = 0;
  for (int i = 0; i < arr[now].size(); i++) {
    int next = arr[now][i];
    if (next == anc) continue;
    long long x = dfs(next, now);
    p.push_back(x * (n - x));
    num += x;
  }
  return num + 1;
}
int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t, a, b, mm;
  cin >> t;
  while (t--) {
    long long ans = 0;
    cin >> n;
    arr = vector<vector<int>>(n);
    for (long long i = 1; i < n; i++) {
      cin >> a >> b;
      a--;
      b--;
      arr[a].push_back(b);
      arr[b].push_back(a);
    }
    cin >> mm;
    vector<long long> k(mm);
    for (int i = 0; i < mm; i++) {
      cin >> k[i];
    }
    dfs(0, -1);
    if (mm < n) {
      while (k.size() < p.size()) k.push_back(1);
      sort(k.begin(), k.end());
      sort(p.begin(), p.end());
      for (long long i = 0; i < p.size(); i++) {
        ans = (ans + (k[i] * p[i]) % mod) % mod;
      }
    } else {
      sort(k.begin(), k.end());
      sort(p.begin(), p.end());
      for (int i = p.size(); i < mm; i++) {
        k[n - 2] = (k[n - 2] * k[i]) % mod;
      }
      for (long long i = 0; i < p.size(); i++) {
        ans = (ans + (k[i] * p[i]) % mod) % mod;
      }
    }
    cout << ans << '\n';
    arr.clear();
    p.clear();
  }
  return 0;
}
