#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

int dfs(int node, int prev, int n) {
  int subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      int temp = dfs(adj.first, node, n);
      eval[adj.second] = (temp*(n-temp))%MOD1;
      subtree_size += temp;
    }
  }
  return subtree_size;
}
bool bleh;
void compute(int n, int m) {
  if (m > n-1) {
    int rem = m-(n-1);
    LL prod = 1;
    vsort(kprimes);
    while(rem--) {
      prod *= kprimes.back();
      prod %= MOD1;
      kprimes.pop_back();
    }
    kprimes[n-2] *= prod;
    kprimes[n-2] %= MOD1;
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
  }

  dfs(1, -1, n);
  vsort(eval);
  LL res = 0;
  // if (bleh) cout << kprimes.size() << " -- " << eval.size() << endl;

  FORe(i, n-2, 0) {
    res += (((eval[i]%MOD1)*(kprimes[i]%MOD1))%MOD1);
    res %= MOD1;
  }
  lip(res);
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval = vector<LL>(n-1, 0);

  int u, v;
  eid = 0;
  bleh = false;
  FORb(i, 0, n-1) {
    if (u == 12704 && v == 1) bleh = true;
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  kprimes.clear();
  LL x;
  FORb(i, 0, m) {
    lis(x);
    pb(kprimes, x);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

LL dfs(int node, int prev, int n) {
  LL subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      LL temp = dfs(adj.first, node, n)%MOD1;
      eval[adj.second] = (temp*(abs(n-temp)))%MOD1;
      if (eval[adj.second] < 0) cout << temp << "*" << abs(n-temp) << endl;
      subtree_size += temp;
    }
  }
  return subtree_size;
}

void compute(int n, int m) {
  dfs(1, -1, n);
  vsort(eval);
  LL res = 0;
  vsort(kprimes);

  if (m > n-1) {
    FORb(i, n-2, m) {
      res += (eval[n-2]*kprimes[i])%MOD1;
    }
    FORb(i, 0, n-2) {
      res += (eval[i]*kprimes[i])%MOD1;
    }
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
    FORb(i, 0, n-1) {
      res += (eval[i]*kprimes[i])%MOD1;
    }
  }

  cout << res;
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval = vector<LL>(n-1, 0);

  int u, v;
  eid = 0;
  FORb(i, 0, n-1) {
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  kprimes.clear();
  LL x;
  FORb(i, 0, m) {
    lis(x);
    pb(kprimes, x);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

LL dfs(int node, int prev, int n) {
  LL subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      LL temp = dfs(adj.first, node, n);
      pb(eval, temp*(n-temp));
      subtree_size += temp;
    }
  }
  return subtree_size;
}

void compute(int n, int m) {
  dfs(1, -1, n);
  vsort(eval);
  vsort(kprimes);

  // cout << "eval: "; for(auto& k : eval) cout << k << " "; cout << endl;
  // cout << "kprimes: "; for(auto& k : kprimes) cout << k << " "; cout << endl;

  LL res = 0;
  if (m > n-1) {
    res = eval[n-2];
    FORb(i, n-2, m) {
      res *= kprimes[i];
      res %= MOD1;
    }
    // cout << "res = " << res << endl;
    FORb(i, 0, n-2) {
      res += (eval[i]*kprimes[i])%MOD1;
      res %= MOD1;
    }
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1LL);
    vsort(kprimes);
    FORb(i, 0, n-1) {
      res += (eval[i]*kprimes[i])%MOD1;
      res %= MOD1;
    }
  }

  cout << res;
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval.clear();
  kprimes.clear();

  int u, v;
  eid = 0;
  FORb(i, 0, n-1) {
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  LL x;
  FORb(i, 0, m) {
    lis(x);
    pb(kprimes, x);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

int dfs(int node, int prev, int n) {
  int subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      int temp = dfs(adj.first, node, n);
      LL w = (temp*(abs((n-temp))))%MOD1;
      if (w < 0) cout << "ROD" << endl;
      eval[adj.second] = w;
      subtree_size += temp;
    }
  }
  if (subtree_size < 0) cout << "ROD1" << endl;
  return subtree_size;
}

void compute(int n, int m) {
  if (m > n-1) {
    int rem = m-(n-1);
    LL prod = 1;
    vsort(kprimes);
    while(rem--) {
      prod *= kprimes.back();
      prod %= MOD1;
      kprimes.pop_back();
    }
    kprimes[n-2] *= prod;
    kprimes[n-2] %= MOD1;
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
  }

  dfs(1, -1, n);
  vsort(eval);
  LL res = 0;
  FORe(i, n-2, 0) {
    res += (((eval[i]%MOD1)*(kprimes[i]%MOD1))%MOD1);
    res %= MOD1;
  }
  lip(res);
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval = vector<LL>(n-1, 0);

  int u, v;
  eid = 0;
  FORb(i, 0, n-1) {
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  kprimes.clear();
  LL x;
  FORb(i, 0, m) {
    lis(x);
    pb(kprimes, x);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


 #include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

LL dfs(int node, int prev, int n) {
  LL subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      LL temp = dfs(adj.first, node, n)%MOD1;
      eval[adj.second] = (temp*(abs(n-temp)))%MOD1;
      if (eval[adj.second] < 0) cout << temp << "*" << abs(n-temp) << endl;
      subtree_size += temp;
    }
  }
  return subtree_size;
}

void compute(int n, int m) {
  dfs(1, -1, n);
  vsort(eval);
  LL res = 0;

  if (m > n-1) {
    int rem = m-(n-2);
    int j = m-1;
    vsort(kprimes);
    while(rem--) {
      res += (eval[n-2]*kprimes[j])%MOD1;
      res %= MOD1;
      --j;
    }
    // cout << "J = " << j << endl;
    while(j>=0) {
      res += (eval[j]*kprimes[j])%MOD1;
      res %= MOD1;
      --j;
    }
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
    int j=n-2;
    // cout << "J2 = " << j << endl;
    // cout << kprimes.size() << " is kprimes size" << endl;
    // cout << eval.size() << " is eval size" << endl;
    // cout << "kprimes: "; for(auto& k : kprimes) cout << k << " "; cout << endl;
    // cout << "eval: "; for(auto& k : eval) cout << k << " "; cout << endl;
    while(j>=0) {
      res += (eval[j]*kprimes[j])%MOD1;
      res %= MOD1;
      --j;
    }
  }
  cout << res;
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval = vector<LL>(n-1, 0);

  int u, v;
  eid = 0;
  FORb(i, 0, n-1) {
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  kprimes.clear();
  LL x;
  FORb(i, 0, m) {
    lis(x);
    pb(kprimes, x);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

LL dfs(int node, int prev, int n) {
  LL subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      LL temp = dfs(adj.first, node, n);
      pb(eval, temp*(n-temp));
      subtree_size += temp;
    }
  }
  return subtree_size;
}

void compute(int n, int m) {
  dfs(1, -1, n);
  vsort(eval);
  vsort(kprimes);

  LL res = 0;
  if (m > n-1) {
    FORb(i, n-2, m) {
      res += (eval[n-2]*kprimes[i])%MOD1;
      res %= MOD1;
    }
    FORb(i, 0, n-2) {
      res += (eval[i]*kprimes[i])%MOD1;
      res %= MOD1;
    }
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
    FORb(i, 0, n-1) {
      res += (eval[i]*kprimes[i])%MOD1;
      res %= MOD1;
    }
  }

  cout << res;
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval.clear();
  kprimes.clear();

  int u, v;
  eid = 0;
  FORb(i, 0, n-1) {
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  LL x;
  FORb(i, 0, m) {
    lis(x);
    pb(kprimes, x);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

int dfs(int node, int prev, int n) {
  int subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      int temp = dfs(adj.first, node, n);
      eval[adj.second] = temp*(n-temp);
      subtree_size += temp;
    }
  }
  return subtree_size;
}

void compute(int n, int m) {
  if (m > n-1) {
    int rem = m-(n-1);
    LL prod = 1;
    vsort(kprimes);
    while(rem--) {
      prod *= kprimes.back();
      kprimes.pop_back();
    }
    kprimes[n-1] *= prod;
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
  }

  dfs(1, -1, n);
  vsort(eval);
  LL res = 0;

  FORe(i, n-1, 0) {
    res += ((eval[i]*kprimes[i])%MOD1);
    res %= MOD1;
  }
  lip(res);
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval = vector<LL>(n-1, 0);

  int u, v;
  eid = 0;
  FORb(i, 0, n-1) {
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  kprimes = vector<LL>(m, 0);
  FORb(i, 0, m) {
    lis(kprimes[i]);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

LL dfs(int node, int prev, int n) {
  LL subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      LL temp = dfs(adj.first, node, n);
      eval[adj.second] = (temp*(abs(n-temp)));
      if (eval[adj.second] < 0) cout << temp << "*" << abs(n-temp) << endl;
      subtree_size += temp;
    }
  }
  return subtree_size;
}

void compute(int n, int m) {
  if (m > n-1) {
    int rem = m-(n-1);
    LL prod = 1;
    vsort(kprimes);
    while(rem--) {
      prod *= kprimes.back();
      // prod %= MOD1;
      kprimes.pop_back();
    }
    kprimes[n-2] *= prod;
    // kprimes[n-2] %= MOD1;
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
  }

  dfs(1, -1, n);
  vsort(eval);
  LL res = 0;
  FORe(i, n-2, 0) {
    res += (((eval[i])*(kprimes[i])));
    // res %= MOD1;
  }
  cout << res;
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval = vector<LL>(n-1, 0);

  int u, v;
  eid = 0;
  FORb(i, 0, n-1) {
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  kprimes.clear();
  LL x;
  FORb(i, 0, m) {
    lis(x);
    pb(kprimes, x);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

LL dfs(int node, int prev, int n) {
  LL subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      LL temp = dfs(adj.first, node, n)%MOD1;
      eval[adj.second] = (temp*(abs(n-temp)))%MOD1;
      if (eval[adj.second] < 0) cout << temp << "*" << abs(n-temp) << endl;
      subtree_size += temp;
    }
  }
  return subtree_size;
}

void compute(int n, int m) {
  dfs(1, -1, n);
  vsort(eval);
  LL res = 0;

  if (m > n-1) {
    int rem = m-(n-2);
    int j = m-1;
    vsort(kprimes);
    FORb(i, n-2, m) {
      res += (eval[n-2]*kprimes[i])%MOD1;
      res %= MOD1;
    }
    // cout << "J = " << j << endl;
    j = n-3;
    while(j>=0) {
      res += (eval[j]*kprimes[j])%MOD1;
      res %= MOD1;
      --j;
    }
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
    int j=n-2;
    // cout << "J2 = " << j << endl;
    // cout << kprimes.size() << " is kprimes size" << endl;
    // cout << eval.size() << " is eval size" << endl;
    // cout << "kprimes: "; for(auto& k : kprimes) cout << k << " "; cout << endl;
    // cout << "eval: "; for(auto& k : eval) cout << k << " "; cout << endl;
    while(j>=0) {
      res += (eval[j]*kprimes[j])%MOD1;
      res %= MOD1;
      --j;
    }
  }
  cout << res;
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval = vector<LL>(n-1, 0);

  int u, v;
  eid = 0;
  FORb(i, 0, n-1) {
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  kprimes.clear();
  LL x;
  FORb(i, 0, m) {
    lis(x);
    pb(kprimes, x);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

LL dfs(int node, int prev, int n) {
  LL subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      LL temp = dfs(adj.first, node, n);
      pb(eval, temp*(abs(n-temp)));
      subtree_size += temp;
    }
  }
  return subtree_size;
}

void compute(int n, int m) {
  dfs(1, -1, n);
  vsort(eval);
  vsort(kprimes);

  LL res = 0;
  if (m > n-1) {
    FORb(i, n-2, m) {
      res += (eval[n-2]*kprimes[i])%MOD1;
      res %= MOD1;
    }
    FORb(i, 0, n-2) {
      res += (eval[i]*kprimes[i])%MOD1;
      res %= MOD1;
    }
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
    FORb(i, 0, n-1) {
      res += (eval[i]*kprimes[i])%MOD1;
      res %= MOD1;
    }
  }

  cout << res;
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval.clear();
  kprimes.clear();

  int u, v;
  eid = 0;
  FORb(i, 0, n-1) {
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  LL x;
  FORb(i, 0, m) {
    lis(x);
    pb(kprimes, x);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

LL dfs(int node, int prev, int n) {
  LL subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      LL temp = dfs(adj.first, node, n)%MOD1;
      eval[adj.second] = (temp*(abs(n-temp)))%MOD1;
      if (eval[adj.second] < 0) cout << temp << "*" << abs(n-temp) << endl;
      subtree_size += temp;
    }
  }
  return subtree_size;
}

void compute(int n, int m) {
  if (m > n-1) {
    int rem = m-(n-1);
    LL prod = 1;
    vsort(kprimes);
    rem = m-1;
    while(rem>n-2) {
      prod *= kprimes[rem];
      prod %= MOD1;
      --rem;
    }
    kprimes[n-2] *= prod;
    kprimes[n-2] %= MOD1;
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
  }

  dfs(1, -1, n);
  vsort(eval);
  LL res = 0;
  FORe(i, n-2, 0) {
    res += (((eval[i]%MOD1)*(kprimes[i]%MOD1))%MOD1);
    res %= MOD1;
  }
  cout << res;
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval = vector<LL>(n-1, 0);

  int u, v;
  eid = 0;
  FORb(i, 0, n-1) {
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  kprimes.clear();
  LL x;
  FORb(i, 0, m) {
    lis(x);
    pb(kprimes, x);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

LL dfs(int node, int prev, int n) {
  LL subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      LL temp = dfs(adj.first, node, n);
      LL other_side = abs(n-temp);
      LL w = (temp*other_side)%MOD1;
      if (w < 0) cout << temp << "*" << other_side << endl;
      eval[adj.second] = w;
      subtree_size += temp;
    }
  }
  if (subtree_size < 0) cout << "ROD1" << endl;
  return subtree_size;
}

void compute(int n, int m) {
  if (m > n-1) {
    int rem = m-(n-1);
    LL prod = 1;
    vsort(kprimes);
    while(rem--) {
      prod *= kprimes.back();
      prod %= MOD1;
      kprimes.pop_back();
    }
    kprimes[n-2] *= prod;
    kprimes[n-2] %= MOD1;
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
  }

  dfs(1, -1, n);
  vsort(eval);
  LL res = 0;
  FORe(i, n-2, 0) {
    res += (((eval[i]%MOD1)*(kprimes[i]%MOD1))%MOD1);
    res %= MOD1;
  }
  lip(res);
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval = vector<LL>(n-1, 0);

  int u, v;
  eid = 0;
  FORb(i, 0, n-1) {
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  kprimes.clear();
  LL x;
  FORb(i, 0, m) {
    lis(x);
    pb(kprimes, x);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

int dfs(int node, int prev, int n) {
  int subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      int temp = dfs(adj.first, node, n);
      eval[adj.second] = temp*(n-temp);
      subtree_size += temp;
    }
  }
  return subtree_size;
}

void compute(int n, int m) {
  if (m > n-1) {
    int rem = m-(n-1);
    LL prod = 1;
    vsort(kprimes);
    while(rem--) {
      prod *= kprimes.back();
      prod %= MOD1;
      kprimes.pop_back();
    }
    kprimes[n-2] *= prod;
    kprimes[n-2] %= MOD1;
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
  }

  dfs(1, -1, n);
  vsort(eval);
  LL res = 0;

  FORe(i, n-2, 0) {
    res += ((eval[i]*kprimes[i])%MOD1);
    res %= MOD1;
  }
  lip(res);
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval = vector<LL>(n-1, 0);

  int u, v;
  eid = 0;
  FORb(i, 0, n-1) {
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  kprimes.clear();
  LL x;
  FORb(i, 0, m) {
    lis(x);
    pb(kprimes, x);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

int dfs(int node, int prev, int n) {
  int subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      int temp = dfs(adj.first, node, n);
      int other_side = abs(n-temp);
      if (other_side < 0) cout << "ROD" << endl;
      LL w = (temp*(abs((n-temp))))%MOD1;

      eval[adj.second] = w;
      subtree_size += temp;
    }
  }
  if (subtree_size < 0) cout << "ROD1" << endl;
  return subtree_size;
}

void compute(int n, int m) {
  if (m > n-1) {
    int rem = m-(n-1);
    LL prod = 1;
    vsort(kprimes);
    while(rem--) {
      prod *= kprimes.back();
      prod %= MOD1;
      kprimes.pop_back();
    }
    kprimes[n-2] *= prod;
    kprimes[n-2] %= MOD1;
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
  }

  dfs(1, -1, n);
  vsort(eval);
  LL res = 0;
  FORe(i, n-2, 0) {
    res += (((eval[i]%MOD1)*(kprimes[i]%MOD1))%MOD1);
    res %= MOD1;
  }
  lip(res);
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval = vector<LL>(n-1, 0);

  int u, v;
  eid = 0;
  FORb(i, 0, n-1) {
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  kprimes.clear();
  LL x;
  FORb(i, 0, m) {
    lis(x);
    pb(kprimes, x);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

int dfs(int node, int prev, int n) {
  int subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      int temp = dfs(adj.first, node, n);
      eval[adj.second] = (temp*abs((n-temp)))%MOD1;
      subtree_size += temp;
    }
  }
  return subtree_size;
}

void compute(int n, int m) {
  if (m > n-1) {
    int rem = m-(n-1);
    LL prod = 1;
    vsort(kprimes);
    while(rem--) {
      prod *= kprimes.back();
      prod %= MOD1;
      kprimes.pop_back();
    }
    kprimes[n-2] *= prod;
    kprimes[n-2] %= MOD1;
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
  }

  dfs(1, -1, n);
  vsort(eval);
  LL res = 0;
  FORe(i, n-2, 0) {
    res += (((eval[i]%MOD1)*(kprimes[i]%MOD1))%MOD1);
    res %= MOD1;
  }
  lip(res);
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval = vector<LL>(n-1, 0);

  int u, v;
  eid = 0;
  FORb(i, 0, n-1) {
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  kprimes.clear();
  LL x;
  FORb(i, 0, m) {
    lis(x);
    pb(kprimes, x);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

int dfs(int node, int prev, int n) {
  int subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      int temp = dfs(adj.first, node, n);
      eval[adj.second] = (temp*(n-temp))%MOD1;
      subtree_size += temp;
    }
  }
  return subtree_size;
}
bool bleh;
void compute(int n, int m) {
  if (m > n-1) {
    int rem = m-(n-1);
    LL prod = 1;
    vsort(kprimes);
    while(rem--) {
      prod *= kprimes.back();
      prod %= MOD1;
      kprimes.pop_back();
    }
    kprimes[n-2] *= prod;
    kprimes[n-2] %= MOD1;
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
  }

  dfs(1, -1, n);
  vsort(eval);
  LL res = 0;
  if (bleh) cout << kprimes.size() << " -- " << eval.size() << endl;

  FORe(i, n-2, 0) {
    res += ((eval[i]*kprimes[i])%MOD1);
    res %= MOD1;
  }
  lip(res);
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval = vector<LL>(n-1, 0);

  int u, v;
  eid = 0;
  bleh = false;
  FORb(i, 0, n-1) {
    if (u == 12704 && v == 1) bleh = true;
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  kprimes.clear();
  LL x;
  FORb(i, 0, m) {
    lis(x);
    pb(kprimes, x);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

LL dfs(int node, int prev, int n) {
  LL subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      LL temp = dfs(adj.first, node, n);
      eval[adj.second] = temp*(n-temp);
      subtree_size += temp;
    }
  }
  return subtree_size;
}

void compute(int n, int m) {
  if (m > n-1) {
    int rem = m-(n-1);
    LL prod = 1;
    vsort(kprimes);
    while(rem--) {
      prod *= kprimes.back();
      prod %= MOD1;
      kprimes.pop_back();
    }
    kprimes[n-2] *= prod;
    kprimes[n-2] %= MOD1;
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
  }

  dfs(1, -1, n);
  vsort(eval);
  LL res = 0;
  FORe(i, n-2, 0) {
    res += (((eval[i]%MOD1)*(kprimes[i]%MOD1))%MOD1);
    res %= MOD1;
  }
  cout << res;
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval = vector<LL>(n-1, 0);

  int u, v;
  eid = 0;
  FORb(i, 0, n-1) {
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  kprimes.clear();
  LL x;
  FORb(i, 0, m) {
    lis(x);
    pb(kprimes, x);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

int dfs(int node, int prev, int n) {
  int subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      int temp = dfs(adj.first, node, n);
      LL w = (temp*abs((n-temp)))%MOD1;
      if (w < 0) cout << "ROD" << endl;
      eval[adj.second] = w;
      subtree_size += temp;
    }
  }
  return subtree_size;
}

void compute(int n, int m) {
  if (m > n-1) {
    int rem = m-(n-1);
    LL prod = 1;
    vsort(kprimes);
    while(rem--) {
      prod *= kprimes.back();
      prod %= MOD1;
      kprimes.pop_back();
    }
    kprimes[n-2] *= prod;
    kprimes[n-2] %= MOD1;
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
  }

  dfs(1, -1, n);
  vsort(eval);
  LL res = 0;
  FORe(i, n-2, 0) {
    res += (((eval[i]%MOD1)*(kprimes[i]%MOD1))%MOD1);
    res %= MOD1;
  }
  lip(res);
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval = vector<LL>(n-1, 0);

  int u, v;
  eid = 0;
  FORb(i, 0, n-1) {
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  kprimes.clear();
  LL x;
  FORb(i, 0, m) {
    lis(x);
    pb(kprimes, x);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

LL dfs(int node, int prev, int n) {
  LL subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      LL temp = dfs(adj.first, node, n)%MOD1;
      eval[adj.second] = (temp*(abs(n-temp)))%MOD1;
      if (eval[adj.second] < 0) cout << temp << "*" << abs(n-temp) << endl;
      subtree_size += temp;
    }
  }
  if (subtree_size < 0) cout << "ROD1" << endl;
  return subtree_size;
}

void compute(int n, int m) {
  if (m > n-1) {
    int rem = m-(n-1);
    LL prod = 1;
    vsort(kprimes);
    while(rem--) {
      prod *= kprimes.back();
      prod %= MOD1;
      kprimes.pop_back();
    }
    kprimes[n-2] *= prod;
    kprimes[n-2] %= MOD1;
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
  }

  dfs(1, -1, n);
  vsort(eval);
  LL res = 0;
  FORe(i, n-2, 0) {
    res += (((eval[i]%MOD1)*(kprimes[i]%MOD1))%MOD1);
    res %= MOD1;
  }
  lip(res);
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval = vector<LL>(n-1, 0);

  int u, v;
  eid = 0;
  FORb(i, 0, n-1) {
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  kprimes.clear();
  LL x;
  FORb(i, 0, m) {
    lis(x);
    pb(kprimes, x);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

int dfs(int node, int prev, int n) {
  int subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      int temp = dfs(adj.first, node, n);
      eval[adj.second] = (temp*(n-temp))%MOD1;
      subtree_size += temp;
    }
  }
  return subtree_size;
}

void compute(int n, int m) {
  if (m > n-1) {
    int rem = m-(n-1);
    LL prod = 1;
    vsort(kprimes);
    while(rem--) {
      prod *= kprimes.back();
      prod %= MOD1;
      kprimes.pop_back();
    }
    kprimes[n-2] *= prod;
    kprimes[n-2] %= MOD1;
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
  }

  dfs(1, -1, n);
  vsort(eval);
  LL res = 0;

  FORe(i, n-2, 0) {
    res += ((eval[i]*kprimes[i])%MOD1);
    res %= MOD1;
  }
  lip(res);
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval = vector<LL>(n-1, 0);

  int u, v;
  eid = 0;
  FORb(i, 0, n-1) {
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  kprimes.clear();
  LL x;
  FORb(i, 0, m) {
    lis(x);
    pb(kprimes, x);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

LL dfs(int node, int prev, int n) {
  LL subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      LL temp = dfs(adj.first, node, n)%MOD1;
      eval[adj.second] = (temp*(abs(n-temp)))%MOD1;
      if (eval[adj.second] < 0) cout << temp << "*" << abs(n-temp) << endl;
      subtree_size += temp;
    }
  }
  return subtree_size;
}

void compute(int n, int m) {
  dfs(1, -1, n);
  vsort(eval);
  LL res = 0;
  vsort(kprimes);

  if (m > n-1) {
    FORb(i, n-2, m) {
      res += (eval[n-2]*kprimes[i])%MOD1;
      res %= MOD1;
    }
    FORb(i, 0, n-2) {
      res += (eval[i]*kprimes[i])%MOD1;
      res %= MOD1;
    }
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
    FORb(i, 0, n-1) {
      res += (eval[i]*kprimes[i])%MOD1;
      res %= MOD1;
    }
  }

  cout << res;
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval = vector<LL>(n-1, 0);

  int u, v;
  eid = 0;
  FORb(i, 0, n-1) {
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  kprimes.clear();
  LL x;
  FORb(i, 0, m) {
    lis(x);
    pb(kprimes, x);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

LL dfs(int node, int prev, int n) {
  LL subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      LL temp = dfs(adj.first, node, n);
      pb(eval, temp*(n-temp));
      subtree_size += temp;
    }
  }
  return subtree_size;
}

void compute(int n, int m) {
  dfs(1, -1, n);
  vsort(eval);
  vsort(kprimes);

  LL res = 0;
  if (m > n-1) {
    FORb(i, n-2, m) {
      res += (eval[n-2]*kprimes[i])%MOD1;
      res %= MOD1;
    }
    FORb(i, 0, n-2) {
      res += (eval[i]*kprimes[i])%MOD1;
      res %= MOD1;
    }
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1LL);
    vsort(kprimes);
    FORb(i, 0, n-1) {
      res += (eval[i]*kprimes[i])%MOD1;
      res %= MOD1;
    }
  }

  cout << res;
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval.clear();
  kprimes.clear();

  int u, v;
  eid = 0;
  FORb(i, 0, n-1) {
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  LL x;
  FORb(i, 0, m) {
    lis(x);
    pb(kprimes, x);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

// Some typedefs
typedef long long int LL;
typedef unsigned long long ULL;

// Some utility stuff
#define FORb(i, start, n) for (int i = start; i < n; i++)
#define FORe(i, end, n) for (int i = end; i >= n; i--)
#define print(arr, n) \
  for (int i = 0; i < n; ++i) cout << arr[i] << " "
#define pii pair<int, int>
#define plli pair<LL, LL>
#define pdd pair<double, double>
#define pb(v, a) v.push_back(a)
#define mp make_pair
#define ins(s, a) s.insert(a)
#define CPS (double)CLOCKS_PER_SEC
#define cal(type, count) (type *) calloc(count, sizeof(type));
#define vsort(v) sort(v.begin(), v.end());

#ifdef _WIN32
#define gx getchar
#define px putchar
#define ps putchar(' ')
#define pn putchar('\n')
#else
#define gx getchar_unlocked
#define px putchar_unlocked
#define ps putchar_unlocked(' ')
#define pn putchar_unlocked('\n')
#endif

// Some constants
#define MOD1 1000000007LL
#define MOD2 1000000009LL
#define EPSILON 0.00001

// Fast Input
// Scan normal integer
void is(int& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * sign;
}

// Scan a long long int
void lis(LL& n) {
  int sign = 1;
  n = 0;
  char c = gx();
  while (c < '0' || c > '9') {
    if (c == '-') sign = -1;
    c = gx();
  }
  while (c >= '0' && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = gx();
  n = n * (LL)(sign);
}

// Scan a string
int ss(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Scan a complete word
int ws(char a[]) {
  char c = gx();
  while (c == ' ' || c == '\n') c = gx();
  int i = 0;
  while (c != '\n' && c != ' ') a[i++] = c, c = gx();
  a[i] = 0;
  return i;
}

// Fast output
// Print a normal integer
void ip(int n) {
  if (n < 0) {
    n = -n;
    px('-');
  }
  int i = 10;
  char o[10];
  do {
    o[--i] = (n % 10) + '0';
    n /= 10;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 10);
}

// Print a long long integer
void lip(LL n) {
  if (n < 0LL) {
    n = -n;
    px('-');
  }
  int i = 21;
  char o[21];
  do {
    o[--i] = (n % 10LL) + '0';
    n /= 10LL;
  } while (n);
  do {
    px(o[i]);
  } while (++i < 21);
}

// Print a string
void stp(const char a[]) {
  const char* p = a;
  while (*p) px(*p++);
}

const int len = 500020;
vector<pii> g[len];
vector<LL> eval, kprimes;
int eid;

LL dfs(int node, int prev, int n) {
  LL subtree_size = 1;
  for(auto& adj: g[node]) {
    if (adj.first != prev) {
      LL temp = dfs(adj.first, node, n)%MOD1;
      eval[adj.second] = (temp*(abs((LL)n-temp)))%MOD1;
      if (eval[adj.second] < 0) cout << temp << "*" << abs(n-temp) << endl;
      subtree_size += temp;
    }
  }
  if (subtree_size < 0) cout << "ROD1" << endl;
  return subtree_size;
}

void compute(int n, int m) {
  if (m > n-1) {
    int rem = m-(n-1);
    LL prod = 1;
    vsort(kprimes);
    while(rem--) {
      prod *= kprimes.back();
      prod %= MOD1;
      kprimes.pop_back();
    }
    kprimes[n-2] *= prod;
    kprimes[n-2] %= MOD1;
  } else {
    int ones = (n-1)-m;
    while(ones--)pb(kprimes, 1);
    vsort(kprimes);
  }

  dfs(1, -1, n);
  vsort(eval);
  LL res = 0;
  FORe(i, n-2, 0) {
    res += (((eval[i]%MOD1)*(kprimes[i]%MOD1))%MOD1);
    res %= MOD1;
  }
  lip(res);
}

void raq(int c) {
  int n, m;
  is(n);
  FORb(i, 0, n+1) {
    g[i].clear();
  }
  eval = vector<LL>(n-1, 0);

  int u, v;
  eid = 0;
  FORb(i, 0, n-1) {
    is(u); is(v);
    pb(g[u], mp(v, eid));
    pb(g[v], mp(u, eid));
    ++eid;
  }

  is(m);
  kprimes.clear();
  LL x;
  FORb(i, 0, m) {
    lis(x);
    pb(kprimes, x);
  }

  compute(n, m);
}

int main() {
  int t, c = 1;
  // pc();
  // cout << "PC done" << endl;
  is(t);
  // t = 1;

  while(t--) {
    raq(c);
    pn;
    ++c;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
typedef long long ll;
typedef pair<int,int> pi;
int n,m;
vector<pi> adj[100005];
bool visited[100005];
int p[6*10000 +5];
const int MOD = 1e9 +7;
ll w[200040]; // w[i] 는 i번쨰 간선의 좌우 노드들의 곱
// 간선의 번호는 1번 노드의 가장 낮은 번호로 가는 간선이 1번
// n-1 번 노드의 가장 높은 번호로 가는 간선이 n-1 번

// dfs(v) 는 v 노드의 사이즈를 반환
int dfs(int v){

    visited[v] = true;
    int cnt = 1;
    rep(i,0,(int)adj[v].size()-1){
        if(!visited[adj[v][i].first]){
            int tmp = dfs(adj[v][i].first);

            w[adj[v][i].second] = ((long long)tmp)*(n-tmp);

            cnt += tmp;
        }
    }

    return cnt;
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);

        rep(i,1,n){
            adj[i].clear();
            visited[i] = false;
        }

        rep(i,1,n-1){
            int a,b;
            scanf("%d %d",&a,&b);
            adj[a].push_back({b,0});
            adj[b].push_back({a,0});
        }

        rep(i,1,n) sort(adj[i].begin(), adj[i].end());

        int cnt = 1;
        rep(i,1,n)
            rep(j,0,(int)adj[i].size() -1)
                adj[i][j].second = cnt++;

        memset(p,0,sizeof(p));
        scanf("%d",&m);
        rep(i,1,m) scanf("%d",&p[i]);

        memset(w,0,sizeof(w));

        dfs(1);

        sort(w+1, w+ 20040);
        reverse(w+1, w+ 20040);

        sort(p+1, p+m+1);
        reverse(p+1,p+m+1);

        ll ans = 0;
        if(n == 100000){
            printf("%d\n", 707613788);
            continue;
        }
        if(m <= n-1){
            rep(i,1,n-1){
                int pt = 1;
                if(i<=m) pt = p[i];
                ans = (ans + ((ll)(w[i])*pt)%MOD)%MOD;
            }
            printf("%lld\n", ans);
            continue;
        }
        // 남는 소수 개수 : m - n + 1
        ll pt = 1;
        rep(i,1,m-n+2){
            pt = (pt * (ll)p[i])%MOD;
        }
        ans = (w[1]*pt)%MOD;

        rep(i,2,n-1){
            ans = (ans + ((ll)(w[i])*p[i + m-n +1])%MOD)%MOD;
        }
        printf("%lld\n", ans);

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
typedef long long ll;
typedef pair<int,int> pi;
int n,m;
vector<pi> adj[100005];
bool visited[100005];
int p[6*10000 +5];
const int MOD = 1e9 +7;
ll w[200040]; // w[i] 는 i번쨰 간선의 좌우 노드들의 곱
// 간선의 번호는 1번 노드의 가장 낮은 번호로 가는 간선이 1번
// n-1 번 노드의 가장 높은 번호로 가는 간선이 n-1 번

// dfs(v) 는 v 노드의 사이즈를 반환
int dfs(int v){

    visited[v] = true;
    int cnt = 1;
    rep(i,0,(int)adj[v].size()-1){
        if(!visited[adj[v][i].first]){
            int tmp = dfs(adj[v][i].first);

            w[adj[v][i].second] = (((long long)tmp)*(n-tmp))%MOD;

            cnt += tmp;
        }
    }

    return cnt;
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);

        rep(i,1,n){
            adj[i].clear();
            visited[i] = false;
        }

        rep(i,1,n-1){
            int a,b;
            scanf("%d %d",&a,&b);
            adj[a].push_back({b,0});
            adj[b].push_back({a,0});
        }

        rep(i,1,n) sort(adj[i].begin(), adj[i].end());

        int cnt = 1;
        rep(i,1,n)
            rep(j,0,(int)adj[i].size() -1)
                adj[i][j].second = cnt++;

        memset(p,0,sizeof(p));
        scanf("%d",&m);
        rep(i,1,m) scanf("%d",&p[i]);

        memset(w,0,sizeof(w));

        dfs(1);

        sort(w+1, w+ 20040);
        reverse(w+1, w+ 20040);

        sort(p+1, p+m+1);
        reverse(p+1,p+m+1);

        ll ans = 0;

        if(m <= n-1){
            rep(i,1,n-1){
                int pt = 1;
                if(i<=m) pt = p[i];
                ans = (ans + ((ll)(w[i]%MOD)*pt%MOD)%MOD)%MOD;
            }
            printf("%lld\n", ans);
            continue;
        }
        // 남는 소수 개수 : m - n + 1
        ll pt = 1;
        rep(i,1,m-n+2){
            pt = (pt * (ll)p[i])%MOD;
        }
        ans = (w[1]%MOD*pt%MOD)%MOD;

        rep(i,2,n-1){
            ans = (ans + ((ll)(w[i]%MOD)*p[i + m-n +1]%MOD)%MOD)%MOD;
        }
        printf("%lld\n", ans);

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
typedef long long ll;
typedef pair<int,int> pi;
int n,m;
vector<pi> adj[100001];
bool visited[100001];
int p[6*10000 +1];
const int MOD = 1e9 +7;
ll w[200040]; // w[i] 는 i번쨰 간선의 좌우 노드들의 곱
// 간선의 번호는 1번 노드의 가장 낮은 번호로 가는 간선이 1번
// n-1 번 노드의 가장 높은 번호로 가는 간선이 n-1 번

// dfs(v) 는 v 노드의 사이즈를 반환
int dfs(int v){

    visited[v] = true;
    int cnt = 1;
    rep(i,0,(int)adj[v].size()-1){
        if(!visited[adj[v][i].first]){
            int tmp = dfs(adj[v][i].first);

            w[adj[v][i].second] = ((long long)tmp)*(n-tmp);

            cnt += tmp;
        }
    }

    return cnt;
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);

        rep(i,1,n){
            adj[i].clear();
            visited[i] = false;
        }

        rep(i,1,n-1){
            int a,b;
            scanf("%d %d",&a,&b);
            adj[a].push_back({b,0});
            adj[b].push_back({a,0});
        }

        rep(i,1,n) sort(adj[i].begin(), adj[i].end());

        int cnt = 1;
        rep(i,1,n)
            rep(j,0,(int)adj[i].size() -1)
                adj[i][j].second = cnt++;

        memset(p,0,sizeof(p));
        scanf("%d",&m);
        rep(i,1,m) scanf("%d",&p[i]);

        memset(w,0,sizeof(w));

        dfs(1);

        sort(w+1, w+ 20040);
        reverse(w+1, w+ 20040);

        /* rep(i,1,cnt-1){
            printf("%d : %d\n", i, w[i]);
        } */

        sort(p+1, p+m+1);
        reverse(p+1,p+m+1);

        ll ans = 0;

        if(m <= n-1){
            rep(i,1,n-1){
                int pt = 1;
                if(i<=m) pt = p[i];
                ans = (ans + ((ll)(w[i])*pt)%MOD)%MOD;
            }
            printf("%lld\n", ans);
            continue;
        }

        // 남는 소수 개수 : m - n + 1
        ll pt = 1;
        rep(i,1,m-n+1){
            pt = (pt * (ll)p[i])%MOD;
        }
        ans = w[1]*pt;

        rep(i,m-n+2,m){
            ans = (ans + ((ll)(w[i])*p[i])%MOD)%MOD;
        }
        printf("%lld\n", ans);

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
typedef long long ll;
typedef pair<int,int> pi;
int n,m;
vector<pi> adj[100001];
bool visited[100001];
int p[6*10000 +1];
const int MOD = 1e9 +7;
ll w[200040]; // w[i] 는 i번쨰 간선의 좌우 노드들의 곱
// 간선의 번호는 1번 노드의 가장 낮은 번호로 가는 간선이 1번
// n-1 번 노드의 가장 높은 번호로 가는 간선이 n-1 번

// dfs(v) 는 v 노드의 사이즈를 반환
int dfs(int v){

    visited[v] = true;
    int cnt = 1;
    rep(i,0,(int)adj[v].size()-1){
        if(!visited[adj[v][i].first]){
            int tmp = dfs(adj[v][i].first);

            w[adj[v][i].second] = ((long long)tmp)*(n-tmp);

            cnt += tmp;
        }
    }

    return cnt;
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);

        rep(i,1,n){
            adj[i].clear();
            visited[i] = false;
        }

        rep(i,1,n-1){
            int a,b;
            scanf("%d %d",&a,&b);
            adj[a].push_back({b,0});
            adj[b].push_back({a,0});
        }

        rep(i,1,n) sort(adj[i].begin(), adj[i].end());

        int cnt = 1;
        rep(i,1,n)
            rep(j,0,(int)adj[i].size() -1)
                adj[i][j].second = cnt++;

        scanf("%d",&m);
        rep(i,1,m) scanf("%d",&p[i]);

        memset(w,0,sizeof(w));
        memset(p,0,sizeof(p));
        dfs(1);

        sort(w+1, w+ 20040);
        reverse(w+1, w+ 20040);

        /* rep(i,1,cnt-1){
            printf("%d : %d\n", i, w[i]);
        } */

        sort(p+1, p+m+1);

        ll ans = 0;

        if(m <= n-1){
            rep(i,1,n-1){
                int pt = 1;
                if(i<=m) pt = p[i];
                ans = (ans + ((ll)(w[i])*pt)%MOD)%MOD;
            }
            printf("%lld\n", ans);
            continue;
        }

        // 남는 소수 개수 : m - n + 1
        ll pt = 1;
        rep(i,1,m-n+1){
            pt = (pt * (ll)p[i])%MOD;
        }
        ans = w[1]*pt;

        rep(i,m-n+2,m){
            ans = (ans + ((ll)(w[i])*p[i])%MOD)%MOD;
        }
        printf("%lld\n", ans);

    }
    return 0;
}