#include <bits/stdc++.h>
using namespace std;
const long double pi = 3.1415926535897932384626433;
const int mod = 1000000007;
const long long inf = 3e18;
vector<vector<long long> > adj(100001);
bool vis[100001];
long long st[100001];
vector<long long> v;
void dfs(long long x, long long n) {
  vis[x] = true;
  st[x] = 1;
  for (auto it : adj[x]) {
    if (!vis[it]) {
      dfs(it, n);
      st[x] += st[it];
    }
  }
  v.push_back(st[x] * (n - st[x]));
}
void solve() {
  long long n;
  cin >> n;
  long long ans = 0;
  v.clear();
  memset(st, 0, sizeof(st));
  memset(vis, 0, sizeof(vis));
  for (long long i = 1; i <= 100000; i++) {
    adj[i].clear();
  }
  for (long long i = 0; i < n - 1; i++) {
    long long a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  long long d;
  cin >> d;
  vector<long long> ar(n - 1);
  if (d < n - 1) {
    for (long long i = 0; i < n - 1; i++) {
      if (i < d) {
        cin >> ar[i];
      } else {
        ar[i] = 1;
      }
    }
    sort(ar.rbegin(), ar.rend());
  } else {
    vector<long long> z(d);
    for (long long i = 0; i < d; i++) {
      cin >> z[i];
    }
    sort(z.rbegin(), z.rend());
    long long s = 1;
    long long f;
    for (f = 0; f < d - n + 2; f++) {
      s = (s * z[f] % mod) % mod;
    }
    for (long long i = 0; i < n - 2; i++, f++) {
      ar[i] = z[f];
    }
    ar[n - 2] = s;
    sort(ar.rbegin(), ar.rend());
  }
  dfs(1, n);
  sort(v.rbegin(), v.rend());
  for (long long i = 0; i < v.size(); i++) {
    ans = ((ans + ((v[i] % mod) * (ar[i] % mod)) % mod)) % mod;
  }
  cout << ans << "\n";
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t;
  cin >> t;
  while (t--) {
    solve();
  }
}
