#include <bits/stdc++.h>
using namespace std;
vector<long long> graph[100000 + 5];
vector<long long> cont;
long long dfs(long long u, long long p, long long n) {
  long long cnt = 0;
  for (auto z : graph[u]) {
    if (z != p) {
      long long temp = dfs(z, u, n);
      cont.push_back(1LL * temp * (n - temp));
      cnt += temp;
    }
  }
  cnt += 1;
  return cnt;
}
inline long long mul(long long a, long long b, long long mod = 1000000007) {
  long long c = 1LL * a * b;
  if (c >= mod) {
    c %= mod;
  }
  return c;
}
inline long long add(long long a, long long b, long long mod = 1000000007) {
  long long c = a + b;
  if (c >= mod) {
    c %= mod;
  }
  return c;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long t;
  cin >> t;
  while (t-- > 00) {
    long long n, u, v, i, m;
    cin >> n;
    for (i = 1; i <= n - 1; ++i) {
      cin >> u >> v;
      graph[u].push_back(v);
      graph[v].push_back(u);
    }
    dfs(1, 0, n);
    cin >> m;
    long long p[m + 5];
    for (i = 0; i < m; ++i) {
      cin >> p[i];
    }
    sort(p, p + m);
    sort(cont.begin(), cont.end());
    assert(cont.size() == n - 1);
    long long ans = 0;
    if (n - 1 >= m) {
      for (i = n - 2; m > 0 and i >= 0; --i, --m) {
        ans = add(ans, mul(cont[i], p[m - 1]));
      }
      for (; i >= 0; --i) {
        ans = add(ans, cont[i]);
      }
    } else {
      for (i = 0; i < n - 2; ++i) {
        ans = add(ans, mul(cont[i], p[i]));
      }
      long long temp = cont[i];
      for (; i < m; ++i) {
        temp = mul(temp, p[i]);
      }
      ans = add(ans, temp);
    }
    cout << ans << "\n";
    for (i = 1; i <= n; ++i) {
      graph[i].clear();
    }
    cont.clear();
  }
  return 0;
}
