#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000 * 1000 * 1000 + 7;
void dfs(int v, vector<vector<int>> &gr, vector<int> &c, vector<long long> &cnt,
         int p, int n) {
  c[v] = 1;
  for (auto it : gr[v]) {
    if (it != p) {
      dfs(it, gr, c, cnt, v, n);
      c[v] += c[it];
    }
  }
  for (auto it : gr[v]) {
    if (it != p) {
      cnt.push_back(1LL * (n - c[it]) * c[it]);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    int n;
    cin >> n;
    vector<vector<int>> gr;
    vector<int> c;
    c.resize(n + 5);
    gr.resize(n + 5);
    for (int j = 0; j < n - 1; j++) {
      int u, v;
      cin >> u >> v;
      gr[u].push_back(v);
      gr[v].push_back(u);
    }
    vector<long long> cnt;
    dfs(1, gr, c, cnt, -1, n);
    sort(cnt.begin(), cnt.end());
    int m;
    cin >> m;
    vector<long long> mmm;
    for (int j = 0; j < m; j++) {
      int a;
      cin >> a;
      mmm.push_back(a);
    }
    while (mmm.size() < cnt.size()) {
      mmm.push_back(1);
    }
    sort(mmm.begin(), mmm.end());
    while (mmm.size() > cnt.size()) {
      long long a = mmm.back();
      mmm.pop_back();
      long long b = mmm.back();
      mmm.pop_back();
      mmm.push_back(a * b % MOD);
    }
    long long res = 0;
    for (int j = 0; j < cnt.size(); j++) {
      res = res + 1LL * cnt[j] * mmm[j];
      res %= MOD;
    }
    cout << res << '\n';
  }
  return 0;
}
