#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
int t, n, m, x, y;
vector<int> g[100005];
int v[100005], w[100005], dp[100005];
void dfs(int nod, int tata = 0) {
  dp[nod] = 1;
  for (auto &fiu : g[nod]) {
    if (fiu != tata) dfs(fiu, nod), dp[nod] += dp[fiu];
  }
}
bool comp(int a, int b) {
  return 1LL * dp[a] * (n - dp[a]) > 1LL * dp[b] * (n - dp[b]);
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  for (; t; t--) {
    cin >> n;
    for (int i = 1; i <= n; i++) g[i].clear(), w[i] = i;
    for (int i = 1; i < n; i++) {
      cin >> x >> y;
      g[x].push_back(y);
      g[y].push_back(x);
    }
    dfs(1);
    cin >> m;
    for (int i = 1; i <= m; i++) cin >> v[i];
    sort(v + 1, v + m + 1);
    sort(w + 1, w + n + 1, comp);
    int j = m, ans = 0, p = 1;
    while (j > n - 1) p = 1LL * p * v[j] % MOD, j--;
    p = 1LL * p * v[j] % MOD;
    ans = (ans + 1LL * dp[w[1]] * (n - dp[w[1]]) * p) % MOD, j--;
    for (int i = 2; i <= n; i++) {
      if (!j)
        ans = (ans + 1LL * dp[w[i]] * (n - dp[w[i]])) % MOD;
      else
        ans = (ans + 1LL * dp[w[i]] * (n - dp[w[i]]) * v[j]) % MOD, j--;
    }
    cout << ans << "\n";
  }
  return 0;
}
