package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
	"sort"
)

// github.com/EndlessCheng/codeforces-go
func CF1401D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	const mod int64 = 1e9 + 7
	type nb struct{ to, i int }

	var T, n, v, w, m int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		g := make([][]nb, n+1)
		for i := 0; i < n-1; i++ {
			Fscan(in, &v, &w)
			g[v] = append(g[v], nb{w, i})
			g[w] = append(g[w], nb{v, i})
		}
		Fscan(in, &m)
		p := make([]int, m)
		for i := range p {
			Fscan(in, &p[i])
		}
		for len(p) < n-1 {
			p = append(p, 1)
		}
		sort.Ints(p)
		pp := int64(p[n-2])
		for _, v := range p[n-1:] {
			pp = pp * int64(v) % mod
		}
		p[n-2] = int(pp)
		cnt := make([]int64, n-1)
		var f func(v, fa int) int
		f = func(v, fa int) int {
			sz := 1
			for _, p := range g[v] {
				if p.to != fa {
					s := f(p.to, v)
					cnt[p.i] = int64(s) * int64(n-s)
					sz += s
				}
			}
			return sz
		}
		f(1, 0)
		sort.Slice(cnt, func(i, j int) bool { return cnt[i] < cnt[j] })
		ans := int64(0)
		for i, c := range cnt {
			ans += c * int64(p[i]) % mod
		}
		Fprintln(out, ans%mod)
	}
}

func main() { CF1401D(os.Stdin, os.Stdout) }

package main

import (
	"strings"
	"strconv"
	"os"
	"sort"

	"bufio"
	"fmt"
)

const MOD = int64(1e9 + 7)

func main() {

	//f, _ := os.OpenFile("D:\\GO\\src\\codeforces\\div665\\tree", os.O_RDWR, 0)
	//defer f.Close()

	/*f.WriteString("1\n")
	f.WriteString("100000\n")

	for i := 1 ; i < int(1e5); i++ {
		f.WriteString(strconv.Itoa(i))
		f.WriteString(" ");
		f.WriteString(strconv.Itoa(i + 1))
		f.WriteString("\n")
	}

	f.WriteString("60000\n")
	for i := 0; i < 60000; i++ {
		f.WriteString("3")
		f.WriteString(" ")
	}*/

	scanner := bufio.NewScanner(os.Stdin)

	buff := make([]byte, 2 * 1024 * 1024)

	scanner.Buffer(buff, 1024 * 1024)

	scanner.Scan()

	t := scanner.Text()

	testCases, err := strconv.Atoi(t)

	if err != nil {
		panic(err)
	}

	for j := 0; j < int(testCases); j ++ {

		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		tree := make([][]int64, n + 1)

		for i := 0; i < n - 1; i++ {
			scanner.Scan()
			a := MustParseFieldsD(scanner.Text())

			tree[a[0]] = append(tree[a[0]], a[1])
			tree[a[1]] = append(tree[a[1]], a[0])
		}
		scanner.Scan()
		scanner.Text()

		scanner.Scan()
		factors := MustParseFieldsD(scanner.Text())

		ans := solveD(factors, tree, n)
		fmt.Println(ans)
	}
}

//265715384
//

func solveD(factors []int64, tree [][]int64, n int) int64 {
	sort.Slice(factors, func(i, j int) bool {
		return factors[i] < factors[j]
	})

	if len(factors) > n - 1 {
		var p int64 = 1
		for i := n - 2 ; i < len(factors); i++ {
			p = (p * int64(factors[i])) % MOD
		}

		factors = factors[ : n - 2]
		factors = append(factors, p)

	} else {
		l := len(factors)
		var f []int64

		for i := 0; i < n - 1 - l; i ++ {
			f = append(f, 1)
		}
		factors = append(f, factors...)
	}

	childCount := make([]int64, n + 1)
	dfs(1, -1, tree, &childCount)

	var weigths []int64

	for i := 2; i <= n; i ++ {
		left := childCount[1] - childCount[i]
		right := childCount[i]

		a := (left * right)

		weigths = append(weigths, a)
	}

	sort.Slice(weigths, func(i, j int) bool {
		return weigths[i] > weigths[j]
	})

	var sum int64 = 0

	for i := 0; i < len(weigths); i++ {
		e := weigths[i]

		var product int64

		product = (e * factors[len(factors) - 1 - i]) % MOD
		sum = (sum + product) % MOD
	}

	return sum
}

func dfs(node, parent int64, tree [][]int64, count *[]int64) int64 {
	children := tree[node]
	var localCount int64 = 0

	for i:= 0; i < len(children); i++ {
		child := children[i]
		if child == parent {
			continue
		}

		localCount = localCount + dfs(child, node, tree, count) + 1
	}

	(*count)[node] = localCount + 1
	return localCount
}

func MustParseFieldsD(str string) []int64 {
	parts := strings.Fields(str)

	var ints []int64

	for i := 0; i < len(parts); i++ {
		v, err := strconv.ParseInt(parts[i], 10, 64)

		if err != nil {
			panic(err)
		}
		ints = append(ints, int64(v))
	}
	return ints
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"sort"
)

var rw = NewFastIO(os.Stdin, os.Stdout)

func main() {
	defer rw.Flush()
	nTest := rw.NextInt()
	for i := 0; i < nTest; i++ {
		solveCase()
	}
}

const mod = int(1e9 + 7)

func solveCase() {
	n := rw.NextInt()
	g := make([][]int, n)
	for i := 1; i < n; i++ {
		u, v := rw.NextInt()-1, rw.NextInt()-1
		g[u] = append(g[u], v)
		g[v] = append(g[v], u)
	}
	ans := 0
	vals := make([]int64, 0)
	var dfs func(int, int) int
	dfs = func(u, p int) int {
		sz := 1
		for _, v := range g[u] {
			if v != p {
				sz += dfs(v, u)
			}
		}
		if p != -1 {
			vals = append(vals, int64(sz)*int64(n-sz))
		}
		return sz
	}
	dfs(0, -1)
	sort.Slice(vals, func(x, y int) bool {
		return vals[x] < vals[y]
	})
	m := rw.NextInt()
	p := rw.NextInts(m)
	for m < n-1 {
		p = append(p, 1)
		m++
	}
	sort.Ints(p)
	for m > n-1 {
		m--
		p[m-1] = int(int64(p[m]) * int64(p[m-1]) % int64(mod))
		p = p[:m]
	}
	for i, e := range p {
		ans += int(((vals[i] % int64(mod)) * int64(e)) % int64(mod))
		if ans >= mod {
			ans -= mod
		}
	}
	rw.Println(ans)
}

// @meooow template uwu
type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, c)
	}
	return string(s)
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}
func (f *FastIO) NextInt64() int64 {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := int64(0)
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int64(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}
func (f *FastIO) NextInt64s(n int) []int64 {
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt64()
	}
	return a
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f, a...)
}
func (f *FastIO) PrintInts(a ...int) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}
func (f *FastIO) PrintInt64s(a ...int64) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInt64s(a ...int64) {
	f.PrintInt64s(a...)
	f.Println()
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

// func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
// func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }
var MOD int64 = 1000000007

var n int
var edges [][]int
var contributions int64s

func dfs(node int, parent int) int64 {
	bs := make([]int64, 0)
	var below int64 = 0
	for _, u := range edges[node] {
		if u == parent {
			continue
		}
		b := dfs(u, node)
		below += b
		bs = append(bs, b)
	}
	for _, b := range bs {
		above := int64(n) - b
		c := (b * above)
		contributions = append(contributions, c)
	}
	return below + 1
}

type int64s []int64

func (a int64s) Len() int           { return len(a) }
func (a int64s) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a int64s) Less(i, j int) bool { return a[i] < a[j] }

func main() {
	var reader *bufio.Reader = bufio.NewReader(os.Stdin)
	var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var T int
	fmt.Scan(&T)

	for t := 0; t < T; t++ {
		fmt.Fscan(reader, &n)
		edges = make([][]int, n)
		contributions = make([]int64, 0)
		var u, v int
		for i := 0; i < n-1; i++ {
			fmt.Fscan(reader, &u)
			fmt.Fscan(reader, &v)
			edges[u-1] = append(edges[u-1], v-1)
			edges[v-1] = append(edges[v-1], u-1)
		}
		var m int
		fmt.Fscan(reader, &m)
		ps := make(int64s, m)
		for i := 0; i < m; i++ {
			fmt.Fscan(reader, &ps[i])
		}
		dfs(0, 0)
		sort.Sort(sort.Reverse(contributions))
		sort.Sort(ps)
		for len(ps) > len(contributions) {
			a := ps[len(ps)-1]
			b := ps[len(ps)-2]
			ps = ps[:len(ps)-2]
			ps = append(ps, (a*b)%MOD)
		}
		revps := make([]int64, len(ps))
		for i := len(ps) - 1; i >= 0; i-- {
			revps[len(ps)-i-1] = ps[i]
		}
		ps = revps
		for i := len(ps); i < len(contributions); i++ {
			ps = append(ps, 1)
		}
		var ans int64 = 0
		for i := 0; i < len(contributions); i++ {
			g := (contributions[i] * ps[i]) % MOD
			ans += g
			ans %= MOD
		}
		fmt.Fprintln(writer, ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"sort"
)

const mod = 1_000_000_007

func main() {
	io := NewFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	for t := io.NextInt(); t > 0; t-- {
		n := io.NextInt()
		g := make([][]int, n+1)
		for i := 0; i < n-1; i++ {
			u, v := io.NextInt(), io.NextInt()
			g[u] = append(g[u], v)
			g[v] = append(g[v], u)
		}
		m := io.NextInt()
		p := make([]int64, m)
		for i := range p {
			p[i] = int64(io.NextInt())
		}

		for len(p) < n-1 {
			p = append(p, 1)
		}
		sort.Slice(p, func(i, j int) bool { return p[i] < p[j] })
		for len(p) > n-1 {
			i := len(p) - 1
			p[i-1] = p[i-1] * p[i] % mod
			p = p[:i]
		}

		var ecnt []int64
		var dfs func(u, p int) int
		dfs = func(u, p int) int {
			s := 1
			for _, v := range g[u] {
				if v != p {
					c := dfs(v, u)
					s += c
					ecnt = append(ecnt, (int64(n)-int64(c))*int64(c))
				}
			}
			return s
		}
		dfs(1, 1)
		sort.Slice(ecnt, func(i, j int) bool { return ecnt[i] < ecnt[j] })

		var ans int64
		for i, c := range ecnt {
			ans = (ans + c*p[i]) % mod
		}
		io.Println(ans)
	}
}

type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, c)
	}
	return string(s)
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f, a...)
}

func (f *FastIO) PrintInts(a ...int) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}

func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

// func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
// func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }
var MOD int64 = 1000000007

var n int
var edges [][]int
var contributions int64s

func dfs(node int, parent int) int64 {
	bs := make([]int64, 0)
	var below int64 = 0
	for _, u := range edges[node] {
		if u == parent {
			continue
		}
		b := dfs(u, node)
		below += b
		bs = append(bs, b)
	}
	for _, b := range bs {
		above := int64(n) - b
		c := (b * above) % MOD
		contributions = append(contributions, c)
	}
	return below + 1
}

type int64s []int64

func (a int64s) Len() int           { return len(a) }
func (a int64s) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a int64s) Less(i, j int) bool { return a[i] < a[j] }

func main() {
	var reader *bufio.Reader = bufio.NewReader(os.Stdin)
	var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var T int
	fmt.Scan(&T)

	for t := 0; t < T; t++ {
		fmt.Fscan(reader, &n)
		edges = make([][]int, n)
		contributions = make([]int64, 0)
		var u, v int
		for i := 0; i < n-1; i++ {
			fmt.Fscan(reader, &u)
			fmt.Fscan(reader, &v)
			edges[u-1] = append(edges[u-1], v-1)
			edges[v-1] = append(edges[v-1], u-1)
		}
		var m int
		fmt.Fscan(reader, &m)
		ps := make(int64s, m)
		for i := 0; i < m; i++ {
			fmt.Fscan(reader, &ps[i])
		}
		dfs(0, 0)
		sort.Sort(sort.Reverse(contributions))
		sort.Sort(ps)
		for len(ps) > len(contributions) {
			a := ps[len(ps)-1]
			b := ps[len(ps)-2]
			ps = ps[:len(ps)-2]
			ps = append(ps, (a * b))
		}
		sort.Sort(sort.Reverse(ps))
		for i := len(ps); i < len(contributions); i++ {
			ps = append(ps, 1)
		}
		var ans int64 = 0
		for i := 0; i < len(contributions); i++ {
			g := (contributions[i] * ps[i]) % MOD
			ans += g
			ans %= MOD
		}
		fmt.Fprintln(writer, ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

// func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
// func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }
var MOD int64 = 1000000007

var n int
var edges [][]int
var contributions []int

func dfs(node int, parent int) int64 {
	bs := make([]int64, 0)
	var below int64 = 0
	for _, u := range edges[node] {
		if u == parent {
			continue
		}
		b := dfs(u, node)
		below += b
		bs = append(bs, b)
	}
	// above := int64(n) - below - 1
	// var contribution int64 = 0
	for _, b := range bs {
		above := int64(n) - b
		c := (b * above) % MOD
		// contribution += c
		// contribution %= MOD
		contributions = append(contributions, int(c))
	}
	return below + 1
}

func main() {
	var reader *bufio.Reader = bufio.NewReader(os.Stdin)
	var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var T int
	fmt.Scan(&T)

	for t := 0; t < T; t++ {
		fmt.Fscan(reader, &n)
		edges = make([][]int, n)
		contributions = make([]int, 0)
		var u, v int
		for i := 0; i < n-1; i++ {
			fmt.Fscan(reader, &u)
			fmt.Fscan(reader, &v)
			edges[u-1] = append(edges[u-1], v-1)
			edges[v-1] = append(edges[v-1], u-1)
		}
		var m int
		fmt.Fscan(reader, &m)
		ps := make([]int, m)
		for i := 0; i < m; i++ {
			fmt.Fscan(reader, &ps[i])
		}
		dfs(0, 0)
		sort.Sort(sort.Reverse(sort.IntSlice(contributions)))
		sort.Ints(ps)
		for len(ps) > len(contributions) {
			a := ps[len(ps)-1]
			b := ps[len(ps)-2]
			ps = ps[:len(ps)-2]
			ps = append(ps, int((int64(a)*int64(b))%MOD))
		}
		sort.Sort(sort.Reverse(sort.IntSlice(ps)))
		for i := len(ps); i < len(contributions); i++ {
			ps = append(ps, 1)
		}
		var ans int64 = 0
		for i := 0; i < len(contributions); i++ {
			g := (int64(contributions[i]) * int64(ps[i])) % MOD
			ans += g
			ans %= MOD
		}
		// fmt.Fprintln(writer, contributions)
		fmt.Fprintln(writer, ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

// func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
// func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }
var MOD int64 = 1000000007

var n int
var edges [][]int
var contributions int64s

func dfs(node int, parent int) int64 {
	bs := make([]int64, 0)
	var below int64 = 0
	for _, u := range edges[node] {
		if u == parent {
			continue
		}
		b := dfs(u, node)
		below += b
		bs = append(bs, b)
	}
	for _, b := range bs {
		above := int64(n) - b
		c := (b * above) % MOD
		contributions = append(contributions, c)
	}
	return below + 1
}

type int64s []int64

func (a int64s) Len() int           { return len(a) }
func (a int64s) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a int64s) Less(i, j int) bool { return a[i] < a[j] }

func main() {
	var reader *bufio.Reader = bufio.NewReader(os.Stdin)
	var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var T int
	fmt.Scan(&T)

	for t := 0; t < T; t++ {
		fmt.Fscan(reader, &n)
		edges = make([][]int, n)
		contributions = make([]int64, 0)
		var u, v int
		for i := 0; i < n-1; i++ {
			fmt.Fscan(reader, &u)
			fmt.Fscan(reader, &v)
			edges[u-1] = append(edges[u-1], v-1)
			edges[v-1] = append(edges[v-1], u-1)
		}
		var m int
		fmt.Fscan(reader, &m)
		ps := make(int64s, m)
		for i := 0; i < m; i++ {
			fmt.Fscan(reader, &ps[i])
		}
		dfs(0, 0)
		sort.Sort(sort.Reverse(contributions))
		sort.Sort(ps)
		for len(ps) > len(contributions) {
			a := ps[len(ps)-1]
			b := ps[len(ps)-2]
			ps = ps[:len(ps)-2]
			ps = append(ps, (a*b)%MOD)
		}
		sort.Sort(sort.Reverse(ps))
		for i := len(ps); i < len(contributions); i++ {
			ps = append(ps, 1)
		}
		var ans int64 = 0
		for i := 0; i < len(contributions); i++ {
			g := (contributions[i] * ps[i]) % MOD
			ans += g
			ans %= MOD
		}
		fmt.Fprintln(writer, ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

// func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
// func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }
var MOD int64 = 1000000007

var n int
var edges [][]int
var contributions int64s

func dfs(node int, parent int) int64 {
	bs := make([]int64, 0)
	var below int64 = 0
	for _, u := range edges[node] {
		if u == parent {
			continue
		}
		b := dfs(u, node)
		below += b
		bs = append(bs, b)
	}
	for _, b := range bs {
		above := int64(n) - b
		c := (b * above) % MOD
		contributions = append(contributions, c)
	}
	return below + 1
}

type int64s []int64

func (a int64s) Len() int           { return len(a) }
func (a int64s) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a int64s) Less(i, j int) bool { return a[i] < a[j] }

func main() {
	var reader *bufio.Reader = bufio.NewReader(os.Stdin)
	var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var T int
	fmt.Scan(&T)

	for t := 0; t < T; t++ {
		fmt.Fscan(reader, &n)
		edges = make([][]int, n)
		contributions = make([]int64, 0)
		var u, v int
		for i := 0; i < n-1; i++ {
			fmt.Fscan(reader, &u)
			fmt.Fscan(reader, &v)
			edges[u-1] = append(edges[u-1], v-1)
			edges[v-1] = append(edges[v-1], u-1)
		}
		var m int
		fmt.Fscan(reader, &m)
		ps := make(int64s, m)
		for i := 0; i < m; i++ {
			fmt.Fscan(reader, &ps[i])
		}
		dfs(0, 0)
		sort.Sort(sort.Reverse(contributions))
		sort.Sort(ps)
		for len(ps) > len(contributions) {
			a := ps[len(ps)-1]
			b := ps[len(ps)-2]
			ps = ps[:len(ps)-2]
			ps = append(ps, (a*b)%MOD)
		}
		revps := make([]int64, len(ps))
		for i := len(ps) - 1; i >= 0; i-- {
			revps[len(ps)-i-1] = ps[i]
		}
		ps = revps
		for i := len(ps); i < len(contributions); i++ {
			ps = append(ps, 1)
		}
		var ans int64 = 0
		for i := 0; i < len(contributions); i++ {
			g := (contributions[i] * ps[i]) % MOD
			ans += g
			ans %= MOD
		}
		fmt.Fprintln(writer, ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

// func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
// func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }
var MOD int64 = 1000000007

var n int
var edges [][]int
var contributions int64s

func dfs(node int, parent int) int64 {
	bs := make([]int64, 0)
	var below int64 = 0
	for _, u := range edges[node] {
		if u == parent {
			continue
		}
		b := dfs(u, node)
		below += b
		bs = append(bs, b)
	}
	for _, b := range bs {
		above := int64(n) - b
		c := (b * above) % MOD
		contributions = append(contributions, c)
	}
	return below + 1
}

type int64s []int64

func (a int64s) Len() int           { return len(a) }
func (a int64s) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a int64s) Less(i, j int) bool { return a[i] < a[j] }

func main() {
	var reader *bufio.Reader = bufio.NewReader(os.Stdin)
	var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var T int
	fmt.Scan(&T)

	for t := 0; t < T; t++ {
		fmt.Fscan(reader, &n)
		edges = make([][]int, n)
		contributions = make([]int64, 0)
		var u, v int
		for i := 0; i < n-1; i++ {
			fmt.Fscan(reader, &u)
			fmt.Fscan(reader, &v)
			edges[u-1] = append(edges[u-1], v-1)
			edges[v-1] = append(edges[v-1], u-1)
		}
		var m int
		fmt.Fscan(reader, &m)
		ps := make(int64s, m)
		for i := 0; i < m; i++ {
			fmt.Fscan(reader, &ps[i])
		}
		dfs(0, 0)
		sort.Sort(sort.Reverse(contributions))
		sort.Sort(ps)
		for len(ps) > len(contributions) {
			a := ps[len(ps)-1]
			b := ps[len(ps)-2]
			ps = ps[:len(ps)-2]
			ps = append(ps, (int64(a)*int64(b))%MOD)
		}
		sort.Sort(sort.Reverse(ps))
		for i := len(ps); i < len(contributions); i++ {
			ps = append(ps, 1)
		}
		var ans int64 = 0
		for i := 0; i < len(contributions); i++ {
			g := (int64(contributions[i]) * int64(ps[i])) % MOD
			ans += g
			ans %= MOD
		}
		fmt.Fprintln(writer, ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

// func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
// func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }
var MOD int64 = 1000000007

var n int
var edges [][]int
var contributions int64s

func dfs(node int, parent int) int64 {
	bs := make([]int64, 0)
	var below int64 = 0
	for _, u := range edges[node] {
		if u == parent {
			continue
		}
		b := dfs(u, node)
		below += b
		bs = append(bs, b)
	}
	for _, b := range bs {
		above := int64(n) - b
		c := (b * above) % MOD
		contributions = append(contributions, c)
	}
	return below + 1
}

type int64s []int64

func (a int64s) Len() int           { return len(a) }
func (a int64s) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a int64s) Less(i, j int) bool { return a[i] < a[j] }

func main() {
	var reader *bufio.Reader = bufio.NewReader(os.Stdin)
	var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var T int
	fmt.Scan(&T)

	for t := 0; t < T; t++ {
		fmt.Fscan(reader, &n)
		edges = make([][]int, n)
		contributions = make([]int64, 0)
		var u, v int
		for i := 0; i < n-1; i++ {
			fmt.Fscan(reader, &u)
			fmt.Fscan(reader, &v)
			edges[u-1] = append(edges[u-1], v-1)
			edges[v-1] = append(edges[v-1], u-1)
		}
		var m int
		fmt.Fscan(reader, &m)
		ps := make([]int, m)
		for i := 0; i < m; i++ {
			fmt.Fscan(reader, &ps[i])
		}
		dfs(0, 0)
		sort.Sort(sort.Reverse(contributions))
		sort.Ints(ps)
		for len(ps) > len(contributions) {
			a := ps[len(ps)-1]
			b := ps[len(ps)-2]
			ps = ps[:len(ps)-2]
			ps = append(ps, int((int64(a)*int64(b))%MOD))
		}
		sort.Sort(sort.Reverse(sort.IntSlice(ps)))
		for i := len(ps); i < len(contributions); i++ {
			ps = append(ps, 1)
		}
		var ans int64 = 0
		for i := 0; i < len(contributions); i++ {
			g := (int64(contributions[i]) * int64(ps[i])) % MOD
			ans += g
			ans %= MOD
		}
		fmt.Fprintln(writer, ans)
	}
}

package main

import (
	"strings"
	"strconv"
	"os"
	"sort"
	"bufio"
	"fmt"
)

const MOD = int64(1e9 + 7)

func main() {

	scanner := bufio.NewScanner(os.Stdin)

	buff := make([]byte, 2 * 1024 * 1024)

	scanner.Buffer(buff, 1024 * 1024)

	scanner.Scan()

	t := scanner.Text()

	testCases, err := strconv.Atoi(t)

	if err != nil {
		panic(err)
	}

	for j := 0; j < int(testCases); j ++ {

		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		tree := make([][]int64, n + 1)

		for i := 0; i < n - 1; i++ {
			scanner.Scan()
			a := MustParseFieldsD(scanner.Text())

			tree[a[0]] = append(tree[a[0]], a[1])
			tree[a[1]] = append(tree[a[1]], a[0])
		}
		scanner.Scan()
		scanner.Text()

		scanner.Scan()
		factors := MustParseFieldsD(scanner.Text())

		ans := solveD(factors, tree, n)
		fmt.Println(ans)
	}
}

func solveD(factors []int64, tree [][]int64, n int) int64 {
	sort.Slice(factors, func(i, j int) bool {
		return factors[i] < factors[j]
	})

	if len(factors) > n - 1 {
		var p int64 = 1
		for i := n - 2 ; i < len(factors); i++ {
			p = (p * int64(factors[i])) % MOD
		}

		factors = factors[ : n - 2]
		factors = append(factors, p)

	} else {
		l := len(factors)
		var f []int64

		for i := 0; i < n - 1 - l; i ++ {
			f = append(f, 1)
		}
		factors = append(f, factors...)
	}

	childCount := make([]int64, n + 1)
	dfs(1, -1, tree, &childCount)

	var weigths []int64

	for i := 2; i <= n; i ++ {
		left := childCount[1] - childCount[i]
		right := childCount[i]

		a := (left * right) % MOD

		weigths = append(weigths, a)
	}

	sort.Slice(weigths, func(i, j int) bool {
		return weigths[i] > weigths[j]
	})

	var sum int64 = 0

	for i := 0; i < len(weigths); i++ {
		e := weigths[i]

		product := (e * factors[len(factors) - 1 - i]) % MOD
		sum = (sum + product) % MOD
	}

	return sum
}
//487759617
func dfs(node, parent int64, tree [][]int64, count *[]int64) int64 {
	children := tree[node]
	var localCount int64 = 0

	for i:= 0; i < len(children); i++ {
		child := children[i]
		if child == parent {
			continue
		}

		localCount = localCount + dfs(child, node, tree, count) + 1
	}

	(*count)[node] = localCount + 1
	return localCount
}

func MustParseFieldsD(str string) []int64 {
	parts := strings.Fields(str)

	var ints []int64

	for i := 0; i < len(parts); i++ {
		v, err := strconv.Atoi(parts[i])

		if err != nil {
			panic(err)
		}
		ints = append(ints, int64(v))
	}
	return ints
}

package main

import (
	"strings"
	"strconv"
	"bufio"

	"os"
	"fmt"
	"sort"
)

const MOD = 1e9 + 7

type edge struct {
	l, r int
	value int
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	buff := make([]byte, 2 * 1024 * 1024)

	scanner.Buffer(buff, 1024 * 1024)

	scanner.Scan()

	t := scanner.Text()

	testCases, err := strconv.Atoi(t)

	if err != nil {
		panic(err)
	}

	for j := 0; j < int(testCases); j ++ {

		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		tree := make([]*edge, n - 1)

		for i := 0; i < n - 1; i++ {
			scanner.Scan()
			a := MustParseFieldsD(scanner.Text())
			tree[i] = &edge{l:a[0], r: a[1]}
		}
		scanner.Scan()
		scanner.Text()

		scanner.Scan()
		factors := MustParseFieldsD(scanner.Text())

		ans := solveD(factors, tree, n)
		fmt.Println(ans)
	}
}

func solveD(factors []int, edges []*edge, n int) int {
	sort.Slice(factors, func(i, j int) bool {
		return factors[i] < factors[j]
	})

	if len(factors) > n - 1 {
		p  := 1
		for i := n - 2 ; i < len(factors); i++ {
			p = (p * factors[i]) % MOD
		}

		factors = factors[ : n - 2]
		factors = append(factors, p)

	} else {
		l := len(factors)
		for i := 0; i < n - 1 - l; i ++ {
			factors = append([]int{1}, factors...)
		}
	}

	tree := make([][]int, n + 1)
	childCount := make([]int, n + 1)

	for i := 0; i < len(edges); i++ {
		e := edges[i]
		l, r := e.l, e.r
		tree[l] = append(tree[l], r)
		tree[r] = append(tree[r], l)
	}

	dfs(1, -1, tree, &childCount)

	var weigths []int

	for i := 2; i <= n; i ++ {
		left := childCount[1] - childCount[i]
		right := childCount[i]

		a := (left * right) % MOD

		weigths = append(weigths, a)
	}

	sort.Slice(weigths, func(i, j int) bool {
		return weigths[i] > weigths[j]
	})

	sum := 0

	for i := 0; i < len(weigths); i++ {
		e := weigths[i]

		sum = (sum + ((e * factors[len(factors) - 1 - i]) % MOD)) % MOD
	}

	return sum
}

func dfs(node, parent int, tree [][]int, count *[]int) int {
	children := tree[node]
	localCount := 0

	for i:= 0; i < len(children); i++ {
		child := children[i]
		if child == parent {
			continue
		}

		localCount = localCount + dfs(child, node, tree, count) + 1
	}

	(*count)[node] = localCount + 1
	return localCount
}

func MustParseFieldsD(str string) []int {
	parts := strings.Fields(str)

	var ints []int

	for i := 0; i < len(parts); i++ {
		v, err := strconv.Atoi(parts[i])

		if err != nil {
			panic(err)
		}
		ints = append(ints, v)
	}
	return ints
}

package main

import (
	"strings"
	"strconv"
	"bufio"

	"os"
	"fmt"
	"sort"
)

const MOD = 1e9 + 7

type edge struct {
	l, r int
	value int
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	buff := make([]byte, 2 * 1024 * 1024)

	scanner.Buffer(buff, 1024 * 1024)

	scanner.Scan()

	t := scanner.Text()

	testCases, err := strconv.Atoi(t)

	if err != nil {
		panic(err)
	}

	for j := 0; j < int(testCases); j ++ {

		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		tree := make([]*edge, n - 1)

		for i := 0; i < n - 1; i++ {
			scanner.Scan()
			a := MustParseFieldsD(scanner.Text())
			tree[i] = &edge{l:a[0], r: a[1]}
		}
		scanner.Scan()
		scanner.Text()

		scanner.Scan()
		factors := MustParseFieldsD(scanner.Text())

		ans := solveD(factors, tree, n)
		fmt.Println(ans)
	}
}

func solveD(factors []int, edges []*edge, n int) int {
	sort.Slice(factors, func(i, j int) bool {
		return factors[i] < factors[j]
	})

	if len(factors) > n - 1 {
		p  := 1
		for i := n - 2 ; i < len(factors); i++ {
			p = (p * factors[i]) % MOD
		}

		factors = factors[ : n - 2]
		factors = append(factors, p)

	} else {
		for i := 0; i <= n - 1 - len(factors); i ++ {
			factors = append(factors, 1)
		}
	}

	tree := make([][]int, n + 1)
	childCount := make([]int, n + 1)

	for i := 0; i < len(edges); i++ {
		e := edges[i]
		l, r := e.l, e.r
		tree[l] = append(tree[l], r)
		tree[r] = append(tree[r], l)
	}

	dfs(1, -1, tree, &childCount)

	var weigths []int

	for i := 2; i <= n; i ++ {
		left := childCount[1] - childCount[i]
		right := childCount[i]

		a := (left * right) % MOD

		weigths = append(weigths, a)
	}

	sort.Slice(weigths, func(i, j int) bool {
		return weigths[i] > weigths[j]
	})

	sum := 0

	for i := 0; i < len(weigths); i++ {
		e := weigths[i]

		sum = (sum + ((e * factors[len(factors) - 1 - i]) % MOD)) % MOD
	}

	return sum
}

func dfs(node, parent int, tree [][]int, count *[]int) int {
	children := tree[node]
	localCount := 0

	for i:= 0; i < len(children); i++ {
		child := children[i]
		if child == parent {
			continue
		}

		localCount = localCount + dfs(child, node, tree, count) + 1
	}

	(*count)[node] = localCount + 1
	return localCount
}

func MustParseFieldsD(str string) []int {
	parts := strings.Fields(str)

	var ints []int

	for i := 0; i < len(parts); i++ {
		v, err := strconv.Atoi(parts[i])

		if err != nil {
			panic(err)
		}
		ints = append(ints, v)
	}
	return ints
}

package main

import (
	"strings"
	"strconv"
	"os"
	"sort"
	"bufio"
	"fmt"
)

const MOD = int64(1e9 + 7)

func main() {

	scanner := bufio.NewScanner(os.Stdin)

	buff := make([]byte, 2 * 1024 * 1024)

	scanner.Buffer(buff, 1024 * 1024)

	scanner.Scan()

	t := scanner.Text()

	testCases, err := strconv.Atoi(t)

	if err != nil {
		panic(err)
	}

	for j := 0; j < int(testCases); j ++ {

		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		tree := make([][]int64, n + 1)

		for i := 0; i < n - 1; i++ {
			scanner.Scan()
			a := MustParseFieldsD(scanner.Text())

			tree[a[0]] = append(tree[a[0]], a[1])
			tree[a[1]] = append(tree[a[1]], a[0])
		}
		scanner.Scan()
		scanner.Text()

		scanner.Scan()
		factors := MustParseFieldsD(scanner.Text())

		ans := solveD(factors, tree, n)
		fmt.Println(ans)
	}
}

func solveD(factors []int64, tree [][]int64, n int) int64 {
	sort.Slice(factors, func(i, j int) bool {
		return factors[i] < factors[j]
	})

	if len(factors) > n - 1 {
		var p int64 = 1
		for i := n - 2 ; i < len(factors); i++ {
			p = (p * int64(factors[i])) % MOD
		}

		factors = factors[ : n - 2]
		factors = append(factors, p)

	} else {
		l := len(factors)
		var f []int64

		for i := 0; i < n - 1 - l; i ++ {
			f = append(f, 1)
		}
		factors = append(f, factors...)
	}

	childCount := make([]int64, n + 1)
	dfs(1, -1, tree, &childCount)

	var weigths []int64

	for i := 2; i <= n; i ++ {
		left := childCount[1] - childCount[i]
		right := childCount[i]

		a := (left * right) % MOD

		weigths = append(weigths, a)
	}

	sort.Slice(weigths, func(i, j int) bool {
		return weigths[i] > weigths[j]
	})

	var sum int64 = 0

	for i := 0; i < len(weigths); i++ {
		e := weigths[i]

		product := (e * factors[len(factors) - 1 - i]) % MOD
		sum = (sum + product) % MOD
	}

	return sum
}

func dfs(node, parent int64, tree [][]int64, count *[]int64) int64 {
	children := tree[node]
	var localCount int64 = 0

	for i:= 0; i < len(children); i++ {
		child := children[i]
		if child == parent {
			continue
		}

		localCount = localCount + dfs(child, node, tree, count) + 1
	}

	(*count)[node] = localCount + 1
	return localCount
}

func MustParseFieldsD(str string) []int64 {
	parts := strings.Fields(str)

	var ints []int64

	for i := 0; i < len(parts); i++ {
		v, err := strconv.ParseInt(parts[i], 10, 64)

		if err != nil {
			panic(err)
		}
		ints = append(ints, v)
	}
	return ints
}

package main

import (
	"strings"
	"strconv"

	"os"

	"sort"
	"bufio"
	"fmt"
)

const MOD = 1e9 + 7

func main() {

	scanner := bufio.NewScanner(os.Stdin)

	buff := make([]byte, 2 * 1024 * 1024)

	scanner.Buffer(buff, 1024 * 1024)

	scanner.Scan()

	t := scanner.Text()

	testCases, err := strconv.Atoi(t)

	if err != nil {
		panic(err)
	}

	for j := 0; j < int(testCases); j ++ {

		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		tree := make([][]int64, n + 1)

		for i := 0; i < n - 1; i++ {
			scanner.Scan()
			a := MustParseFieldsD(scanner.Text())

			tree[a[0]] = append(tree[a[0]], a[1])
			tree[a[1]] = append(tree[a[1]], a[0])
		}
		scanner.Scan()
		scanner.Text()

		scanner.Scan()
		factors := MustParseFieldsD(scanner.Text())

		ans := solveD(factors, tree, n)
		fmt.Println(ans)
	}
}

func solveD(factors []int64, tree [][]int64, n int) int64 {
	sort.Slice(factors, func(i, j int) bool {
		return factors[i] < factors[j]
	})

	if len(factors) > n - 1 {
		var p int64 = 1
		for i := n - 2 ; i < len(factors); i++ {
			p = (p * int64(factors[i])) % MOD
		}

		factors = factors[ : n - 2]
		factors = append(factors, p)

	} else {
		l := len(factors)
		var f []int64

		for i := 0; i < n - 1 - l; i ++ {
			f = append(f, 1)
		}
		factors = append(f, factors...)
	}

	childCount := make([]int64, n + 1)
	dfs(1, -1, tree, &childCount)

	var weigths []int64

	for i := 2; i <= n; i ++ {
		left := childCount[1] - childCount[i]
		right := childCount[i]

		a := (left * right) % MOD

		weigths = append(weigths, a)
	}

	sort.Slice(weigths, func(i, j int) bool {
		return weigths[i] > weigths[j]
	})

	var sum int64 = 0

	for i := 0; i < len(weigths); i++ {
		e := weigths[i]

		sum = (sum + ((e * factors[len(factors) - 1 - i]) % MOD)) % MOD
	}

	return sum
}

func dfs(node, parent int64, tree [][]int64, count *[]int64) int64 {
	children := tree[node]
	var localCount int64 = 0

	for i:= 0; i < len(children); i++ {
		child := children[i]
		if child == parent {
			continue
		}

		localCount = localCount + dfs(child, node, tree, count) + 1
	}

	(*count)[node] = localCount + 1
	return localCount
}

func MustParseFieldsD(str string) []int64 {
	parts := strings.Fields(str)

	var ints []int64

	for i := 0; i < len(parts); i++ {
		v, err := strconv.Atoi(parts[i])

		if err != nil {
			panic(err)
		}
		ints = append(ints, int64(v))
	}
	return ints
}

package main

import (
	"strings"
	"strconv"
	"os"
	"sort"

	"bufio"
	"fmt"
)

const MOD = uint64(1e9 + 7)

func main() {

	//f, _ := os.OpenFile("D:\\GO\\src\\codeforces\\div665\\tree", os.O_RDWR, 0)
	//defer f.Close()

	/*f.WriteString("1\n")
	f.WriteString("100000\n")

	for i := 1 ; i < int(1e5); i++ {
		f.WriteString(strconv.Itoa(i))
		f.WriteString(" ");
		f.WriteString(strconv.Itoa(i + 1))
		f.WriteString("\n")
	}

	f.WriteString("60000\n")
	for i := 0; i < 60000; i++ {
		f.WriteString("59993")
		f.WriteString(" ")
	}*/

	scanner := bufio.NewScanner(os.Stdin)

	buff := make([]byte, 2 * 1024 * 1024)

	scanner.Buffer(buff, 1024 * 1024)

	scanner.Scan()

	t := scanner.Text()

	testCases, err := strconv.Atoi(t)

	if err != nil {
		panic(err)
	}

	for j := 0; j < int(testCases); j ++ {

		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		tree := make([][]uint64, n + 1)

		for i := 0; i < n - 1; i++ {
			scanner.Scan()
			a := MustParseFieldsD(scanner.Text())

			tree[a[0]] = append(tree[a[0]], a[1])
			tree[a[1]] = append(tree[a[1]], a[0])
		}
		scanner.Scan()
		scanner.Text()

		scanner.Scan()
		factors := MustParseFieldsD(scanner.Text())

		ans := solveD(factors, tree, n)
		fmt.Println(ans)
	}
}

//226019091
//

func solveD(factors []uint64, tree [][]uint64, n int) uint64 {
	sort.Slice(factors, func(i, j int) bool {
		return factors[i] < factors[j]
	})

	if len(factors) > n - 1 {
		var p uint64 = 1
		for i := n - 2 ; i < len(factors); i++ {
			p = (p * uint64(factors[i])) % MOD
		}

		factors = factors[ : n - 2]
		factors = append(factors, p)

	} else {
		l := len(factors)
		var f []uint64

		for i := 0; i < n - 1 - l; i ++ {
			f = append(f, 1)
		}
		factors = append(f, factors...)
	}

	childCount := make([]uint64, n + 1)
	dfs(1, 1, tree, &childCount)

	var weigths []uint64

	for i := 2; i <= n; i ++ {
		left := childCount[1] - childCount[i]
		right := childCount[i]

		a := (left * right) % MOD

		weigths = append(weigths, a)
	}

	sort.Slice(weigths, func(i, j int) bool {
		return weigths[i] > weigths[j]
	})

	var sum uint64 = 0

	for i := 0; i < len(weigths); i++ {
		e := weigths[i]

		var product uint64

		product = (e * factors[len(factors) - 1 - i]) % MOD
		sum = (sum + product) % MOD
	}

	return sum
}

func dfs(node, parent uint64, tree [][]uint64, count *[]uint64) uint64 {
	children := tree[node]
	var localCount uint64 = 0

	for i:= 0; i < len(children); i++ {
		child := children[i]
		if child == parent {
			continue
		}

		localCount = localCount + dfs(child, node, tree, count) + 1
	}

	(*count)[node] = localCount + 1
	return localCount
}

func MustParseFieldsD(str string) []uint64 {
	parts := strings.Fields(str)

	var ints []uint64

	for i := 0; i < len(parts); i++ {
		v, err := strconv.ParseInt(parts[i], 10, 64)

		if err != nil {
			panic(err)
		}
		ints = append(ints, uint64(v))
	}
	return ints
}

package main

import (
	"strings"
	"strconv"

	"os"

	"sort"
	"bufio"
	"fmt"
)

const MOD = int64(1e9 + 7)

func main() {

	scanner := bufio.NewScanner(os.Stdin)

	buff := make([]byte, 2 * 1024 * 1024)

	scanner.Buffer(buff, 1024 * 1024)

	scanner.Scan()

	t := scanner.Text()

	testCases, err := strconv.Atoi(t)

	if err != nil {
		panic(err)
	}

	for j := 0; j < int(testCases); j ++ {

		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		tree := make([][]int64, n + 1)

		for i := 0; i < n - 1; i++ {
			scanner.Scan()
			a := MustParseFieldsD(scanner.Text())

			tree[a[0]] = append(tree[a[0]], a[1])
			tree[a[1]] = append(tree[a[1]], a[0])
		}
		scanner.Scan()
		scanner.Text()

		scanner.Scan()
		factors := MustParseFieldsD(scanner.Text())

		ans := solveD(factors, tree, n)
		fmt.Println(ans)
	}
}

func solveD(factors []int64, tree [][]int64, n int) int64 {
	sort.Slice(factors, func(i, j int) bool {
		return factors[i] < factors[j]
	})

	if len(factors) > n - 1 {
		var p int64 = 1
		for i := n - 2 ; i < len(factors); i++ {
			p = (p * int64(factors[i])) % MOD
		}

		factors = factors[ : n - 2]
		factors = append(factors, p)

	} else {
		l := len(factors)
		var f []int64

		for i := 0; i < n - 1 - l; i ++ {
			f = append(f, 1)
		}
		factors = append(f, factors...)
	}

	childCount := make([]int64, n + 1)
	dfs(1, -1, tree, &childCount)

	var weigths []int64

	for i := 2; i <= n; i ++ {
		left := childCount[1] - childCount[i]
		right := childCount[i]

		a := (left * right) % MOD

		weigths = append(weigths, a)
	}

	sort.Slice(weigths, func(i, j int) bool {
		return weigths[i] > weigths[j]
	})

	var sum int64 = 0

	for i := 0; i < len(weigths); i++ {
		e := weigths[i]

		sum = (sum + ((e * factors[len(factors) - 1 - i]) % MOD)) % MOD
	}

	return sum
}

func dfs(node, parent int64, tree [][]int64, count *[]int64) int64 {
	children := tree[node]
	var localCount int64 = 0

	for i:= 0; i < len(children); i++ {
		child := children[i]
		if child == parent {
			continue
		}

		localCount = localCount + dfs(child, node, tree, count) + 1
	}

	(*count)[node] = localCount + 1
	return localCount
}

func MustParseFieldsD(str string) []int64 {
	parts := strings.Fields(str)

	var ints []int64

	for i := 0; i < len(parts); i++ {
		v, err := strconv.Atoi(parts[i])

		if err != nil {
			panic(err)
		}
		ints = append(ints, int64(v))
	}
	return ints
}

package main

import (
	"strings"
	"strconv"
	"os"
	"sort"
	"bufio"
	"fmt"
)

const MOD = int64(1e9 + 7)

func main() {

	scanner := bufio.NewScanner(os.Stdin)

	buff := make([]byte, 2 * 1024 * 1024)

	scanner.Buffer(buff, 1024 * 1024)

	scanner.Scan()

	t := scanner.Text()

	testCases, err := strconv.Atoi(t)

	if err != nil {
		panic(err)
	}

	for j := 0; j < int(testCases); j ++ {

		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		tree := make([][]int64, n + 1)

		for i := 0; i < n - 1; i++ {
			scanner.Scan()
			a := MustParseFieldsD(scanner.Text())

			tree[a[0]] = append(tree[a[0]], a[1])
			tree[a[1]] = append(tree[a[1]], a[0])
		}
		scanner.Scan()
		scanner.Text()

		scanner.Scan()
		factors := MustParseFieldsD(scanner.Text())

		ans := solveD(factors, tree, n)
		fmt.Println(ans)
	}
}

func solveD(factors []int64, tree [][]int64, n int) int64 {
	sort.Slice(factors, func(i, j int) bool {
		return factors[i] < factors[j]
	})

	if len(factors) > n - 1 {
		var p int64 = 1
		for i := n - 2 ; i < len(factors); i++ {
			p = (p * int64(factors[i])) % MOD
		}

		factors = factors[ : n - 2]
		factors = append(factors, p)

	} else {
		l := len(factors)
		var f []int64

		for i := 0; i < n - 1 - l; i ++ {
			f = append(f, 1)
		}
		factors = append(f, factors...)
	}

	childCount := make([]int64, n + 1)
	dfs(1, -1, tree, &childCount)

	var weigths []int64

	for i := 2; i <= n; i ++ {
		left := childCount[1] - childCount[i]
		right := childCount[i]

		a := (left * right) % MOD

		weigths = append(weigths, a)
	}

	sort.Slice(weigths, func(i, j int) bool {
		return weigths[i] > weigths[j]
	})

	var sum int64 = 0

	for i := 0; i < len(weigths); i++ {
		e := weigths[i]

		product := (e * factors[len(factors) - 1 - i])
		sum = (sum + product) % MOD
	}

	return sum
}

func dfs(node, parent int64, tree [][]int64, count *[]int64) int64 {
	children := tree[node]
	var localCount int64 = 0

	for i:= 0; i < len(children); i++ {
		child := children[i]
		if child == parent {
			continue
		}

		localCount = localCount + dfs(child, node, tree, count) + 1
	}

	(*count)[node] = localCount + 1
	return localCount
}

func MustParseFieldsD(str string) []int64 {
	parts := strings.Fields(str)

	var ints []int64

	for i := 0; i < len(parts); i++ {
		v, err := strconv.Atoi(parts[i])

		if err != nil {
			panic(err)
		}
		ints = append(ints, int64(v))
	}
	return ints
}

package main

import (
	"strings"
	"strconv"
	"os"
	"sort"

	"bufio"
	"fmt"
)

const MOD = int64(1e9 + 7)

func main() {

	//f, _ := os.OpenFile("D:\\GO\\src\\codeforces\\div665\\tree", os.O_RDWR, 0)
	//defer f.Close()

	/*f.WriteString("1\n")
	f.WriteString("100000\n")

	for i := 1 ; i < int(1e5); i++ {
		f.WriteString(strconv.Itoa(i))
		f.WriteString(" ");
		f.WriteString(strconv.Itoa(i + 1))
		f.WriteString("\n")
	}

	f.WriteString("60000\n")
	for i := 0; i < 60000; i++ {
		f.WriteString("59993")
		f.WriteString(" ")
	}*/

	scanner := bufio.NewScanner(os.Stdin)

	buff := make([]byte, 2 * 1024 * 1024)

	scanner.Buffer(buff, 1024 * 1024)

	scanner.Scan()

	t := scanner.Text()

	testCases, err := strconv.Atoi(t)

	if err != nil {
		panic(err)
	}

	for j := 0; j < int(testCases); j ++ {

		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		tree := make([][]int64, n + 1)

		for i := 0; i < n - 1; i++ {
			scanner.Scan()
			a := MustParseFieldsD(scanner.Text())

			tree[a[0]] = append(tree[a[0]], a[1])
			tree[a[1]] = append(tree[a[1]], a[0])
		}
		scanner.Scan()
		scanner.Text()

		scanner.Scan()
		factors := MustParseFieldsD(scanner.Text())

		ans := solveD(factors, tree, n)
		fmt.Println(ans)
	}
}

//226019091
//

func solveD(factors []int64, tree [][]int64, n int) int64 {
	sort.Slice(factors, func(i, j int) bool {
		return factors[i] < factors[j]
	})

	if len(factors) > n - 1 {
		var p int64 = 1
		for i := n - 2 ; i < len(factors); i++ {
			p = (p * int64(factors[i])) % MOD
		}

		factors = factors[ : n - 2]
		factors = append(factors, p)

	} else {
		l := len(factors)
		var f []int64

		for i := 0; i < n - 1 - l; i ++ {
			f = append(f, 1)
		}
		factors = append(f, factors...)
	}

	childCount := make([]int64, n + 1)
	dfs(1, -1, tree, &childCount)

	var weigths []int64

	for i := 2; i <= n; i ++ {
		left := childCount[1] - childCount[i]
		right := childCount[i]

		a := (left * right) % MOD

		weigths = append(weigths, a)
	}

	sort.Slice(weigths, func(i, j int) bool {
		return weigths[i] > weigths[j]
	})

	var sum int64 = 0

	for i := 0; i < len(weigths); i++ {
		e := weigths[i]

		var product int64

		product = (e * factors[len(factors) - 1 - i]) % MOD
		sum = (sum + product) % MOD
	}

	return sum
}

func dfs(node, parent int64, tree [][]int64, count *[]int64) int64 {
	children := tree[node]
	var localCount int64 = 0

	for i:= 0; i < len(children); i++ {
		child := children[i]
		if child == parent {
			continue
		}

		localCount = localCount + dfs(child, node, tree, count) + 1
	}

	(*count)[node] = localCount + 1
	return localCount
}

func MustParseFieldsD(str string) []int64 {
	parts := strings.Fields(str)

	var ints []int64

	for i := 0; i < len(parts); i++ {
		v, err := strconv.ParseInt(parts[i], 10, 64)

		if err != nil {
			panic(err)
		}
		ints = append(ints, int64(v))
	}
	return ints
}

package main

import (
	"strings"
	"strconv"
	"bufio"

	"os"
	"fmt"
	"sort"
)

const MOD = 1e9 + 7

type edge struct {
	l, r int
	value int
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	buff := make([]byte, 2 * 1024 * 1024)

	scanner.Buffer(buff, 1024 * 1024)

	scanner.Scan()

	t := scanner.Text()

	testCases, err := strconv.Atoi(t)

	if err != nil {
		panic(err)
	}

	for j := 0; j < int(testCases); j ++ {

		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		tree := make([]*edge, n - 1)

		for i := 0; i < n - 1; i++ {
			scanner.Scan()
			a := MustParseFieldsD(scanner.Text())
			tree[i] = &edge{l:a[0], r: a[1]}
		}
		scanner.Scan()
		scanner.Text()

		scanner.Scan()
		factors := MustParseFieldsD(scanner.Text())

		ans := solveD(factors, tree, n)
		fmt.Println(ans)
	}
}

func solveD(factors []int, edges []*edge, n int) int {
	sort.Slice(factors, func(i, j int) bool {
		return factors[i] < factors[j]
	})

	if len(factors) > n - 1 {
		p  := 1
		for i := n - 2 ; i < len(factors); i++ {
			p = (p * factors[i]) % MOD
		}

		factors = factors[ : n - 2]
		factors = append(factors, p)

	} else {
		var f []int
		for i := 0; i <= n - 1 - len(factors); i ++ {
			f = append(f, 1)
			f = append(f, factors...)
			factors = f
		}
	}

	tree := make([][]int, n + 1)
	childCount := make([]int, n + 1)

	for i := 0; i < len(edges); i++ {
		e := edges[i]
		l, r := e.l, e.r
		tree[l] = append(tree[l], r)
		tree[r] = append(tree[r], l)
	}

	dfs(1, -1, tree, &childCount)

	var weigths []int

	for i := 2; i <= n; i ++ {
		left := childCount[1] - childCount[i]
		right := childCount[i]

		a := (left * right) % MOD

		weigths = append(weigths, a)
	}

	sort.Slice(weigths, func(i, j int) bool {
		return weigths[i] > weigths[j]
	})

	sum := 0

	for i := 0; i < len(weigths); i++ {
		e := weigths[i]

		sum = (sum + ((e * factors[len(factors) - 1 - i]) % MOD)) % MOD
	}

	return sum
}

func dfs(node, parent int, tree [][]int, count *[]int) int {
	children := tree[node]
	localCount := 0

	for i:= 0; i < len(children); i++ {
		child := children[i]
		if child == parent {
			continue
		}

		localCount = localCount + dfs(child, node, tree, count) + 1
	}

	(*count)[node] = localCount + 1
	return localCount
}

func MustParseFieldsD(str string) []int {
	parts := strings.Fields(str)

	var ints []int

	for i := 0; i < len(parts); i++ {
		v, err := strconv.Atoi(parts[i])

		if err != nil {
			panic(err)
		}
		ints = append(ints, v)
	}
	return ints
}

package main

import (
	"strings"
	"strconv"
	"os"
	"sort"
	"bufio"
	"fmt"
)

const MOD = int64(1e9 + 7)

func main() {

	scanner := bufio.NewScanner(os.Stdin)

	buff := make([]byte, 2 * 1024 * 1024)

	scanner.Buffer(buff, 1024 * 1024)

	scanner.Scan()

	t := scanner.Text()

	testCases, err := strconv.Atoi(t)

	if err != nil {
		panic(err)
	}

	for j := 0; j < int(testCases); j ++ {

		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		tree := make([][]int64, n + 1)

		for i := 0; i < n - 1; i++ {
			scanner.Scan()
			a := MustParseFieldsD(scanner.Text())

			tree[a[0]] = append(tree[a[0]], a[1])
			tree[a[1]] = append(tree[a[1]], a[0])
		}
		scanner.Scan()
		scanner.Text()

		scanner.Scan()
		factors := MustParseFieldsD(scanner.Text())

		ans := solveD(factors, tree, n)
		fmt.Println(ans)
	}
}

func solveD(factors []int64, tree [][]int64, n int) int64 {
	sort.Slice(factors, func(i, j int) bool {
		return factors[i] < factors[j]
	})

	if len(factors) > n - 1 {
		var p int64 = 1
		for i := n - 2 ; i < len(factors); i++ {
			p = (p * int64(factors[i])) % MOD
		}

		factors = factors[ : n - 2]
		factors = append(factors, p)

	} else {
		l := len(factors)
		var f []int64

		for i := 0; i < n - 1 - l; i ++ {
			f = append(f, 1)
		}
		factors = append(f, factors...)
	}

	childCount := make([]int64, n + 1)
	dfs(1, -1, tree, &childCount)

	var weigths []int64

	for i := 2; i <= n; i ++ {
		left := childCount[1] - childCount[i]
		right := childCount[i]

		a := (left * right) % MOD

		weigths = append(weigths, a)
	}

	sort.Slice(weigths, func(i, j int) bool {
		return weigths[i] > weigths[j]
	})

	var sum int64 = 0

	for i := 0; i < len(weigths); i++ {
		e := weigths[i]

		product := (e * factors[len(factors) - 1 - i]) % MOD
		sum = (sum + product) % MOD
	}

	return sum
}

func dfs(node, parent int64, tree [][]int64, count *[]int64) int64 {
	children := tree[node]
	var localCount int64 = 0

	for i:= 0; i < len(children); i++ {
		child := children[i]
		if child == parent {
			continue
		}

		localCount = localCount + dfs(child, node, tree, count) + 1
	}

	(*count)[node] = localCount + 1
	return localCount
}

func MustParseFieldsD(str string) []int64 {
	parts := strings.Fields(str)

	var ints []int64

	for i := 0; i < len(parts); i++ {
		v, err := strconv.Atoi(parts[i])

		if err != nil {
			panic(err)
		}
		ints = append(ints, int64(v))
	}
	return ints
}

package main

import (
	"strings"
	"strconv"
	"os"
	"sort"
	"bufio"
	"fmt"
)

const MOD = int64(1e9 + 7)

func main() {

	scanner := bufio.NewScanner(os.Stdin)

	buff := make([]byte, 2 * 1024 * 1024)

	scanner.Buffer(buff, 1024 * 1024)

	scanner.Scan()

	t := scanner.Text()

	testCases, err := strconv.Atoi(t)

	if err != nil {
		panic(err)
	}

	for j := 0; j < int(testCases); j ++ {

		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		tree := make([][]int64, n + 1)

		for i := 0; i < n - 1; i++ {
			scanner.Scan()
			a := MustParseFieldsD(scanner.Text())

			tree[a[0]] = append(tree[a[0]], a[1])
			tree[a[1]] = append(tree[a[1]], a[0])
		}
		scanner.Scan()
		scanner.Text()

		scanner.Scan()
		factors := MustParseFieldsD(scanner.Text())

		ans := solveD(factors, tree, n)
		fmt.Println(ans)
	}
}

func solveD(factors []int64, tree [][]int64, n int) int64 {
	sort.Slice(factors, func(i, j int) bool {
		return factors[i] < factors[j]
	})

	if len(factors) > n - 1 {
		var p int64 = 1
		for i := n - 2 ; i < len(factors); i++ {
			p = (p * int64(factors[i])) % MOD
		}

		factors = factors[ : n - 2]
		factors = append(factors, p)

	} else {
		l := len(factors)
		var f []int64

		for i := 0; i < n - 1 - l; i ++ {
			f = append(f, 1)
		}
		factors = append(f, factors...)
	}

	childCount := make([]int64, n + 1)
	dfs(1, -1, tree, &childCount)

	var weigths []int64

	for i := 2; i <= n; i ++ {
		left := childCount[1] - childCount[i]
		right := childCount[i]

		a := (left * right) % MOD

		weigths = append(weigths, a)
	}

	sort.Slice(weigths, func(i, j int) bool {
		return weigths[i] > weigths[j]
	})

	var sum int64 = 0

	for i := 0; i < len(weigths); i++ {
		e := weigths[i]

		var product int64

		product = (e * factors[len(factors) - 1 - i]) % MOD
		sum = (sum + product) % MOD
	}

	return sum
}

func dfs(node, parent int64, tree [][]int64, count *[]int64) int64 {
	children := tree[node]
	var localCount int64 = 0

	for i:= 0; i < len(children); i++ {
		child := children[i]
		if child == parent {
			continue
		}

		localCount = localCount + dfs(child, node, tree, count) + 1
	}

	(*count)[node] = localCount + 1
	return localCount
}

func MustParseFieldsD(str string) []int64 {
	parts := strings.Fields(str)

	var ints []int64

	for i := 0; i < len(parts); i++ {
		v, err := strconv.ParseInt(parts[i], 10, 64)

		if err != nil {
			panic(err)
		}
		ints = append(ints, v)
	}
	return ints
}

package main

import (
	"strings"
	"strconv"
	"os"
	"sort"
	"bufio"
	"fmt"
)

const MOD = int64(1e9 + 7)

func main() {

	scanner := bufio.NewScanner(os.Stdin)

	buff := make([]byte, 16 * 1024 * 1024)

	scanner.Buffer(buff, 1024 * 1024)

	scanner.Scan()

	t := scanner.Text()

	testCases, err := strconv.Atoi(t)

	if err != nil {
		panic(err)
	}

	for j := 0; j < int(testCases); j ++ {

		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		tree := make([][]int64, n + 1)

		for i := 0; i < n - 1; i++ {
			scanner.Scan()
			a := MustParseFieldsD(scanner.Text())

			tree[a[0]] = append(tree[a[0]], a[1])
			tree[a[1]] = append(tree[a[1]], a[0])
		}
		scanner.Scan()
		scanner.Text()

		scanner.Scan()
		factors := MustParseFieldsD(scanner.Text())

		ans := solveD(factors, tree, n)
		fmt.Println(ans)
	}
}

func solveD(factors []int64, tree [][]int64, n int) int64 {
	sort.Slice(factors, func(i, j int) bool {
		return factors[i] < factors[j]
	})

	if len(factors) > n - 1 {
		var p int64 = 1
		for i := n - 2 ; i < len(factors); i++ {
			p = (p * int64(factors[i])) % MOD
		}

		factors = factors[ : n - 2]
		factors = append(factors, p)

	} else {
		l := len(factors)
		var f []int64

		for i := 0; i < n - 1 - l; i ++ {
			f = append(f, 1)
		}
		factors = append(f, factors...)
	}

	childCount := make([]int64, n + 1)
	dfs(1, -1, tree, &childCount)

	var weigths []int64

	for i := 2; i <= n; i ++ {
		left := childCount[1] - childCount[i]
		right := childCount[i]

		a := (left * right) % MOD

		weigths = append(weigths, a)
	}

	sort.Slice(weigths, func(i, j int) bool {
		return weigths[i] > weigths[j]
	})

	var sum int64 = 0

	for i := 0; i < len(weigths); i++ {
		e := weigths[i]

		product := (e * factors[len(factors) - 1 - i]) % MOD
		sum = (sum + product) % MOD
	}

	return sum
}

func dfs(node, parent int64, tree [][]int64, count *[]int64) int64 {
	children := tree[node]
	var localCount int64 = 0

	for i:= 0; i < len(children); i++ {
		child := children[i]
		if child == parent {
			continue
		}

		localCount = localCount + dfs(child, node, tree, count) + 1
	}

	(*count)[node] = localCount + 1
	return localCount
}

func MustParseFieldsD(str string) []int64 {
	parts := strings.Fields(str)

	var ints []int64

	for i := 0; i < len(parts); i++ {
		v, err := strconv.Atoi(parts[i])

		if err != nil {
			panic(err)
		}
		ints = append(ints, int64(v))
	}
	return ints
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
	"sort"
)

// github.com/EndlessCheng/codeforces-go
func CF1401D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	const mod int64 = 1e9 + 7
	type nb struct{ to, i int }

	var T, n, v, w, m int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		g := make([][]nb, n+1)
		for i := 0; i < n-1; i++ {
			Fscan(in, &v, &w)
			g[v] = append(g[v], nb{w, i})
			g[w] = append(g[w], nb{v, i})
		}
		Fscan(in, &m)
		p := make(sort.IntSlice, m)
		for i := range p {
			Fscan(in, &p[i])
		}
		sort.Sort(sort.Reverse(p))
		for len(p) < n {
			p = append(p, 1)
		}
		p0 := int64(p[0])
		for _, v := range p[n:] {
			p0 = p0 * int64(v) % mod
		}
		p[0] = int(p0)
		cnt := make([]int64, n-1)
		var f func(v, fa int) int
		f = func(v, fa int) int {
			sz := 1
			for _, p := range g[v] {
				if p.to != fa {
					s := f(p.to, v)
					cnt[p.i] = int64(s) * int64(n-s)
					sz += s
				}
			}
			return sz
		}
		f(1, 0)
		sort.Slice(cnt, func(i, j int) bool { return cnt[i] > cnt[j] })
		ans := int64(0)
		for i, c := range cnt {
			ans += c * int64(p[i]) % mod
		}
		Fprintln(out, ans%mod)
	}
}

func main() { CF1401D(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"sort"
)

var rw = NewFastIO(os.Stdin, os.Stdout)

func main() {
	defer rw.Flush()
	nTest := rw.NextInt()
	for i := 0; i < nTest; i++ {
		solveCase()
	}
}

const mod = int(1e9 + 7)

func solveCase() {
	n := rw.NextInt()
	g := make([][]int, n)
	for i := 1; i < n; i++ {
		u, v := rw.NextInt()-1, rw.NextInt()-1
		g[u] = append(g[u], v)
		g[v] = append(g[v], u)
	}
	ans := 0
	vals := make([]int64, 0)
	var dfs func(int, int) int
	dfs = func(u, p int) int {
		sz := 1
		for _, v := range g[u] {
			if v != p {
				sz += dfs(v, u)
			}
		}
		if p != -1 {
			vals = append(vals, int64(sz)*int64(n-sz))
		}
		return sz
	}
	dfs(0, -1)
	sort.Slice(vals, func(x, y int) bool {
		return vals[x] < vals[y]
	})
	m := rw.NextInt()
	p := rw.NextInts(m)
	for m < n-1 {
		p = append(p, 1)
		m++
	}
	sort.Ints(p)
	for m > n-1 {
		m--
		p[m-1] = p[m] * p[m-1] % mod
		p = p[:m]
	}
	for i, e := range p {
		ans += int(((vals[i] % int64(mod)) * int64(e)) % int64(mod))
		if ans >= mod {
			ans -= mod
		}
	}
	rw.Println(ans)
}

// @meooow template uwu
type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, c)
	}
	return string(s)
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}
func (f *FastIO) NextInt64() int64 {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := int64(0)
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int64(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}
func (f *FastIO) NextInt64s(n int) []int64 {
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt64()
	}
	return a
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f, a...)
}
func (f *FastIO) PrintInts(a ...int) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}
func (f *FastIO) PrintInt64s(a ...int64) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInt64s(a ...int64) {
	f.PrintInt64s(a...)
	f.Println()
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"sort"
)

var rw = NewFastIO(os.Stdin, os.Stdout)

func main() {
	defer rw.Flush()
	nTest := rw.NextInt()
	for i := 0; i < nTest; i++ {
		solveCase()
	}
}

const mod = int(1e9 + 7)

func solveCase() {
	n := rw.NextInt()
	g := make([][]int, n)
	for i := 1; i < n; i++ {
		u, v := rw.NextInt()-1, rw.NextInt()-1
		g[u] = append(g[u], v)
		g[v] = append(g[v], u)
	}
	ans := 0
	vals := make([]int64, 0)
	var dfs func(int, int) int
	dfs = func(u, p int) int {
		sz := 1
		for _, v := range g[u] {
			if v != p {
				sz += dfs(v, u)
			}
		}
		if p != -1 {
			vals = append(vals, int64(sz)*int64(n-sz))
		}
		return sz
	}
	dfs(0, -1)
	sort.Slice(vals, func(x, y int) bool {
		return vals[x] < vals[y]
	})
	m := rw.NextInt()
	p := rw.NextInts(m)
	for m < n-1 {
		p = append(p, 1)
		m++
	}
	sort.Ints(p)
	for i, e := range p {
		if i >= n-2 {
			i = n - 2
		}
		ans += int(vals[i] % int64(mod) * int64(e) % int64(mod))
		if ans >= mod {
			ans -= mod
		}
	}
	rw.Println(ans)
}

// @meooow template uwu
type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, c)
	}
	return string(s)
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}
func (f *FastIO) NextInt64() int64 {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := int64(0)
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int64(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}
func (f *FastIO) NextInt64s(n int) []int64 {
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt64()
	}
	return a
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f, a...)
}
func (f *FastIO) PrintInts(a ...int) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}
func (f *FastIO) PrintInt64s(a ...int64) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInt64s(a ...int64) {
	f.PrintInt64s(a...)
	f.Println()
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"sort"
)

var rw = NewFastIO(os.Stdin, os.Stdout)

func main() {
	defer rw.Flush()
	nTest := rw.NextInt()
	for i := 0; i < nTest; i++ {
		solveCase()
	}
}

const mod = int(1e9 + 7)

func solveCase() {
	n := rw.NextInt()
	g := make([][]int, n)
	for i := 1; i < n; i++ {
		u, v := rw.NextInt()-1, rw.NextInt()-1
		g[u] = append(g[u], v)
		g[v] = append(g[v], u)
	}
	ans := 0
	vals := make([]int64, 0)
	var dfs func(int, int) int
	dfs = func(u, p int) int {
		sz := 1
		for _, v := range g[u] {
			if v != p {
				sz += dfs(v, u)
			}
		}
		if p != -1 {
			vals = append(vals, int64(sz)*int64(n-sz))
		}
		return sz
	}
	dfs(0, -1)
	sort.Slice(vals, func(x, y int) bool {
		return vals[x] < vals[y]
	})
	m := rw.NextInt()
	p := rw.NextInts(m)
	for m < n-1 {
		p = append(p, 1)
		m++
	}
	sort.Ints(p)
	for i, e := range p {
		if i >= n-2 {
			i = n - 2
		}
		ans += int(((vals[i] % int64(mod)) * int64(e)) % int64(mod))
		if ans >= mod {
			ans -= mod
		}
	}
	rw.Println(ans)
}

// @meooow template uwu
type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, c)
	}
	return string(s)
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}
func (f *FastIO) NextInt64() int64 {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := int64(0)
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int64(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}
func (f *FastIO) NextInt64s(n int) []int64 {
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt64()
	}
	return a
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f, a...)
}
func (f *FastIO) PrintInts(a ...int) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}
func (f *FastIO) PrintInt64s(a ...int64) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInt64s(a ...int64) {
	f.PrintInt64s(a...)
	f.Println()
}