#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000000;
const long long MOD = 1000000007;
vector<long long> egs, son;
vector<long long> G[210000];
long long n;
void dfss(long long p, long long fa = 0) {
  for (auto adj : G[p]) {
    if (adj == fa) continue;
    dfss(adj, p);
    son[p] += son[adj];
  }
}
void reroot(long long p, long long fa = 0) {
  for (auto adj : G[p]) {
    if (adj == fa) continue;
    egs.push_back((son[p] - son[adj]) * son[adj]);
    son[adj] += son[p] - son[adj];
    reroot(adj, p);
  }
}
void solve() {
  cin >> n;
  egs.clear();
  son.assign(n + 1, 1LL);
  for (long long i = 0; i <= n; i++) G[i].clear();
  for (long long i = 0; i < n - 1; i++) {
    long long a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  dfss(1);
  reroot(1);
  long long np;
  cin >> np;
  vector<long long> snp;
  for (int i = 0; i < np; i++) {
    int tmp;
    cin >> tmp;
    if (tmp == 1) continue;
    snp.push_back(tmp);
  }
  while (snp.size() < egs.size()) snp.push_back(1);
  sort(snp.begin(), snp.end());
  sort(egs.begin(), egs.end());
  long long sp = snp.size() - 1;
  if (snp.size() > egs.size()) {
    long long now = 1;
    while (sp >= egs.size()) {
      now *= snp[sp--];
      now %= MOD;
    }
    snp[sp] *= now;
    snp[sp] %= MOD;
  }
  long long ans = 0;
  for (int i = egs.size() - 1; i >= 0; i--) {
    ans += ((egs[i] % MOD) * snp[sp--]) % MOD;
    ans %= MOD;
  }
  cout << ans % MOD << endl;
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  long long t;
  cin >> t;
  for (long long i = 0; i < t; i++) solve();
}
