#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 100;
const int mod = 1e9 + 7;
int t;
int n;
int m;
long long p[maxn];
int size[maxn];
vector<int> g[maxn];
void dfs(int u, int pre) {
  size[u] = 1;
  for (int v : g[u]) {
    if (v == pre) continue;
    dfs(v, u);
    size[u] += size[v];
  }
}
struct node {
  long long x, y;
  bool operator<(const node &r) const {
    return (long long)x * y > (long long)r.x * r.y;
  }
} Node[maxn];
bool cmp(int x, int y) { return x > y; }
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) g[i].clear();
    for (int i = 1; i < n; i++) {
      int x, y;
      scanf("%d%d", &x, &y);
      g[x].push_back(y);
      g[y].push_back(x);
    }
    scanf("%d", &m);
    for (int i = 1; i <= m; i++) scanf("%lld", p + i);
    sort(p + 1, p + m + 1, cmp);
    dfs(1, 0);
    int tot = 0;
    for (int i = 2; i <= n; i++)
      Node[++tot] = {(long long)n - size[i], (long long)size[i]};
    sort(Node + 1, Node + tot + 1);
    long long ans = 0;
    if (n - 1 >= m) {
      for (int i = 1; i <= m; i++) {
        ans += (long long)p[i] * Node[i].x * Node[i].y;
        ans %= mod;
      }
      for (int i = m + 1; i <= tot; i++) {
        ans += (long long)Node[i].x * Node[i].y;
        ans %= mod;
      }
    } else {
      long long tt = 1;
      for (int i = 1; i <= (m - n + 2); i++) {
        tt *= p[i];
        tt %= mod;
      }
      ans += (long long)tt * Node[1].x % mod * Node[1].y;
      ans %= mod;
      for (int i = m - n + 3; i <= m; i++) {
        ans +=
            (long long)p[i] * Node[i - (m - n + 1)].x * Node[i - (m - n + 1)].y;
        ans %= mod;
      }
    }
    printf("%lld\n", ans);
  }
}
