#include <bits/stdc++.h>
using namespace std;
long long n, m, a[100010], ans[100010], tot;
vector<long long> son[100010];
long long out, mod = 1e9 + 7;
long long cnt[100010], u, v;
void dfs(long long x, long long fa) {
  for (vector<long long>::iterator it = son[x].begin(); it != son[x].end();
       it++) {
    long long to = *it;
    if (to == fa) continue;
    dfs(to, x);
    ans[tot++] = cnt[to] * (n - cnt[to]);
    cnt[x] += cnt[to];
  }
}
inline void solve(int T) {
  cin >> n;
  tot = 1;
  for (register int i = 1; i <= n; i++) {
    son[i].clear();
    cnt[i] = 1;
  }
  for (register int i = 1; i <= n - 1; i++) {
    cin >> u >> v;
    son[u].push_back(v);
    son[v].push_back(u);
  }
  cin >> m;
  for (register int i = 1; i <= m; i++) cin >> a[i];
  if (m < n) {
    for (register int i = m + 1; i <= n - 1; i++) a[i] = 1;
    sort(a + 1, a + n);
  } else {
    sort(a + 1, a + m + 1);
    for (register int i = n; i <= m; i++) a[n - 1] = a[n - 1] * a[i] % mod;
  }
  dfs(1, 0);
  sort(ans + 1, ans + n);
  out = 0;
  for (register int i = 1; i <= n - 1; i++)
    out = (out + ans[i] % mod * a[i] % mod) % mod;
  cout << out << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int T = 1;
  cin >> T;
  for (register int i = 1; i <= T; i++) solve(i);
}
