#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 11;
vector<long long int> adj[N], val;
long long int cnt[N], nodes;
long long int add(long long int a, long long int b) {
  a %= 1000000007;
  b %= 1000000007;
  a = (a + b) % 1000000007;
  return a;
}
long long int mul(long long int a, long long int b) {
  a = (a % 1000000007 * b % 1000000007) % 1000000007;
  return a;
}
void dfs(long long int n, long long int p) {
  cnt[n] = 1;
  for (long long int i = 0; i < adj[n].size(); ++i) {
    long long int x = adj[n][i];
    if (x == p) continue;
    dfs(x, n);
    cnt[n] += cnt[x];
  }
  val.push_back(cnt[n] * (nodes - cnt[n]));
}
void kishu() {
  long long int n;
  cin >> n;
  for (long long int i = 0; i <= n; ++i) adj[i].clear();
  for (long long int i = 1; i < n; ++i) {
    long long int x, y;
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  nodes = n;
  val.clear();
  dfs(1, 0);
  sort(val.begin(), val.end());
  long long int m;
  cin >> m;
  vector<long long int> cost(m);
  for (long long int i = 0; i < m; ++i) cin >> cost[i];
  for (long long int i = m; i < n - 1; ++i) cost.push_back(1);
  sort(cost.begin(), cost.end());
  while (cost.size() > n - 1) {
    long long int x = cost.back();
    cost.pop_back();
    long long int y = cost.back();
    cost.pop_back();
    cost.push_back(mul(x, y));
  }
  long long int ans = 0;
  for (long long int i = 1; i < val.size(); ++i) {
    ans = add(ans, mul(cost[i - 1], val[i]));
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
  long long int t = 1;
  cin >> t;
  while (t--) {
    kishu();
  }
  return 0;
}
