#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC target( avx,avx2,fma )   #pragma GCC optimization( unroll-loops )   const int mod = 1e9 + 7;   using ll = int64_t;   using namespace std;   template <int MOD_>   struct modnum {     static constexpr int MOD = MOD_;     static_assert(MOD_ > 0,  MOD must be positive );     int v;       private:     using ll = long long;     static int minv(int a, int m) {       a %= m;       assert(a);       return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a);     }       public:     modnum() : v(0) {}     modnum(ll v_) : v(int(v_ % MOD)) {       if (v < 0) v += MOD;     }     explicit operator int() const { return v; }     friend std::ostream& operator<<(std::ostream& out, const modnum& n) {       return out << int(n);     }     friend std::istream& operator>>(std::istream& in, modnum& n) {       ll v_;       in >> v_;       n = modnum(v_);       return in;     }     friend bool operator==(const modnum& a, const modnum& b) {       return a.v == b.v;     }     friend bool operator!=(const modnum& a, const modnum& b) {       return a.v != b.v;     }     modnum inv() const {       modnum res;       res.v = minv(v, MOD);       return res;     }     friend modnum inv(const modnum& m) { return m.inv(); }     modnum neg() const {       modnum res;       res.v = v ? MOD - v : 0;       return res;     }     friend modnum neg(const modnum& m) { return m.neg(); }     modnum operator-() const { return neg(); }     modnum operator+() const { return modnum(*this); }     modnum& operator++() {       v++;       if (v == MOD) v = 0;       return *this;     }     modnum& operator--() {       if (v == 0) v = MOD;       v--;       return *this;     }     modnum& operator+=(const modnum& o) {       v += o.v;       if (v >= MOD) v -= MOD;       return *this;     }     modnum& operator-=(const modnum& o) {       v -= o.v;       if (v < 0) v += MOD;       return *this;     }     modnum& operator*=(const modnum& o) {       v = int(ll(v) * ll(o.v) % MOD);       return *this;     }     modnum& operator/=(const modnum& o) { return *this *= o.inv(); }     friend modnum operator++(modnum& a, int) {       modnum r = a;       ++a;       return r;     }     friend modnum operator--(modnum& a, int) {       modnum r = a;       --a;       return r;     }     friend modnum operator+(const modnum& a, const modnum& b) {       return modnum(a) += b;     }     friend modnum operator-(const modnum& a, const modnum& b) {       return modnum(a) -= b;     }     friend modnum operator*(const modnum& a, const modnum& b) {       return modnum(a) *= b;     }     friend modnum operator/(const modnum& a, const modnum& b) {       return modnum(a) /= b;     }   };   using mi = modnum<mod>;   void solve() {     int n, m;     cin >> n;     vector<vector<int>> adj(n);     for (int i = 0; i < n - 1; i++) {       int a, b;       cin >> a >> b;       a--, b--;       adj[a].push_back(b);       adj[b].push_back(a);     }     cin >> m;     vector<mi> fac(m);     for (int i = 0; i < m; i++) {       cin >> fac[i];     }     auto comp = [&](mi X, mi Y) -> bool { return X.v < Y.v; };     sort(fac.begin(), fac.end(), comp);     if (int(fac.size()) < n - 1) {       reverse(fac.begin(), fac.end());       while (int(fac.size()) < n - 1) {         if (int(fac.size()) < n - 1) {           fac.push_back(mi(1));         }       }       reverse(fac.begin(), fac.end());     }     if (int(fac.size()) > n - 1) {       mi cur = 1;       while (int(fac.size()) >= n - 1) {         cur *= fac.back();         fac.pop_back();       }       fac.push_back(cur);     }     vector<ll> sz(n, 0), num;     function<void(int, int)> dfs = [&](int node, int par) -> void {       sz[node] = 1;       for (auto i : adj[node]) {         if (i != par) {           dfs(i, node);           sz[node] += sz[i];           num.push_back((ll)sz[i] * (n - sz[i]));         }       }     };     mi answer = 0;     dfs(0, 0);     sort(num.begin(), num.end());     assert(int(num.size()) >= n - 1);     assert(int(fac.size()) == n - 1);     for (int i = 0; i < n - 1; i++) {       mi cur = (fac[i] * num[i]);       answer += cur;     }     cout << answer <<   n ;   }   int main() {     std::ios_base::sync_with_stdio(false);     std::cin.tie(nullptr);     int t(1);     cin >> t;     while (t--) {       solve();     }   }