#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
vector<long long> V[1000005];
long long vis[1000005];
long long X[1000005];
long long Y[1000005];
void dfs(int u, int fa) {
  vis[u] = 1;
  for (int i = 0; i < V[u].size(); i++) {
    int v = V[u][i];
    if (v != fa) {
      dfs(v, u);
      vis[u] += vis[v];
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  long long T;
  cin >> T;
  while (T--) {
    long long n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
      V[i].clear();
      vis[i] = 0;
    }
    for (int i = 1; i < n; i++) {
      long long x, y;
      cin >> x >> y;
      X[i] = x;
      Y[i] = y;
      V[x].push_back(y);
      V[y].push_back(x);
    }
    dfs(1, 1);
    priority_queue<long long> Q;
    for (int i = 1; i < n; i++) {
      long long x = 0;
      long long y = 0;
      long long cnt = 0;
      x = vis[X[i]];
      y = vis[Y[i]];
      if (x < y) {
        cnt = x * (n - x);
      } else {
        cnt = y * (n - y);
      }
      Q.push(cnt);
    }
    long long m;
    cin >> m;
    vector<long long> W;
    for (int i = 1; i <= m; i++) {
      long long x;
      cin >> x;
      W.push_back(x);
    }
    sort(W.begin(), W.end());
    while (W.size() < (n - 1)) {
      W.push_back(1);
    }
    while (W.size() > (n - 1)) {
      long long e = W.back();
      W.pop_back();
      e = e * W.back();
      W.pop_back();
      e = e % mod;
      W.push_back(e);
    }
    sort(W.begin(), W.end());
    long long sum = 0;
    while (Q.size() > 0) {
      long long x = Q.top();
      Q.pop();
      x = x * W.back();
      W.pop_back();
      x = x % mod;
      sum += x;
      sum = sum % mod;
    }
    cout << sum << "\n";
  }
  return 0;
}
