#include <bits/stdc++.h>
using namespace std;
vector<int> g[100010];
vector<long long> arestas;
int peso[100010];
int n;
void dfs(int node, int parent) {
  peso[node] = 1;
  for (int i = 0; i < (int)g[node].size(); i++) {
    int v = g[node][i];
    if (v != parent) {
      dfs(v, node);
      peso[node] += peso[v];
      arestas.push_back(1LL * peso[v] * (n - peso[v]));
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    arestas.clear();
    for (int i = 0; i <= n; i++) {
      g[i].clear();
      peso[i] = 0;
    }
    for (int i = 0; i < n - 1; i++) {
      int x, y;
      cin >> x >> y;
      g[x].push_back(y);
      g[y].push_back(x);
    }
    dfs(1, -1);
    int m;
    cin >> m;
    vector<int> fatores;
    for (int i = 0; i < m; i++) {
      int x;
      cin >> x;
      fatores.push_back(x);
    }
    int bla = (int)fatores.size();
    if (n - 1 > bla) {
      for (int i = 0; i < (n - 1) - bla; i++) {
        fatores.push_back(1);
      }
    }
    sort(fatores.begin(), fatores.end());
    sort(arestas.begin(), arestas.end());
    int p = 0;
    long long res = 0;
    while (p < n - 2) {
      res += 1LL * fatores[p] * (arestas[p] % 1000000007) % 1000000007;
      res %= 1000000007;
      p++;
    }
    long long aux = arestas[n - 2] % 1000000007;
    while (p < (int)fatores.size()) {
      aux *= 1LL * fatores[p] % 1000000007;
      aux %= 1000000007;
      p++;
    }
    res += aux;
    res %= 1000000007;
    cout << res << endl;
  }
}
