#include <bits/stdc++.h>
using namespace std;
vector<vector<long long int>> v;
long long int n;
vector<long long int> value;
long long int size1[100005];
void count(long long int src, long long int parent) {
  size1[src] = 1;
  for (auto x : v[src]) {
    if (x != parent) {
      count(x, src);
      value.push_back(size1[x] * (n - size1[x]));
      size1[src] += size1[x];
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    v.clear();
    value.clear();
    memset(size1, (long long int)0, sizeof size1);
    cin >> n;
    v.assign(n + 1, vector<long long int>());
    for (long long int i = 0, a, b; i < n - 1; i++) {
      cin >> a >> b;
      v[a].push_back(b);
      v[b].push_back(a);
    }
    long long int m1, mod = 1000000007, ans = 0;
    cin >> m1;
    vector<long long int> fact;
    for (long long int i = 0; i < m1; i++) {
      long long int temp;
      cin >> temp;
      fact.push_back(temp);
    }
    count(1, -1);
    while (fact.size() < n - 1) fact.push_back(1);
    m1 = fact.size();
    sort(fact.begin(), fact.end());
    sort(value.begin(), value.end());
    for (long long int i = 0; i < n - 2; i++) {
      ans = (ans + ((value[i] % mod) * (fact[i] % mod)) % mod) % mod;
    }
    long long int left = 1;
    for (long long int i = n - 2; i < m1; i++) {
      left = ((left % mod) * (fact[i] % mod)) % mod;
    }
    ans = (ans + (left * value[n - 2]) % mod) % mod;
    cout << ans << endl;
  }
  return 0;
}
