You are given a tree that consists of $$$n$$$ nodes. You should label each of its $$$n-1$$$ edges with an integer in such way that satisfies the following conditions:   each integer must be greater than $$$0$$$;  the product of all $$$n-1$$$ numbers should be equal to $$$k$$$;  the number of $$$1$$$-s among all $$$n-1$$$ integers must be minimum possible. Let's define $$$f(u,v)$$$ as the sum of the numbers on the simple path from node $$$u$$$ to node $$$v$$$. Also, let $$$\sum\limits_{i=1}^{n-1} \sum\limits_{j=i+1}^n f(i,j)$$$ be a distribution index of the tree.Find the maximum possible distribution index you can get. Since answer can be too large, print it modulo $$$10^9 + 7$$$.In this problem, since the number $$$k$$$ can be large, the result of the prime factorization of $$$k$$$ is given instead.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of nodes in the tree. Each of the next $$$n-1$$$ lines describes an edge: the $$$i$$$-th line contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$; $$$u_i \ne v_i$$$) — indices of vertices connected by the $$$i$$$-th edge. Next line contains a single integer $$$m$$$ ($$$1 \le m \le 6 \cdot 10^4$$$) — the number of prime factors of $$$k$$$. Next line contains $$$m$$$ prime numbers $$$p_1, p_2, \ldots, p_m$$$ ($$$2 \le p_i < 6 \cdot 10^4$$$) such that $$$k = p_1 \cdot p_2 \cdot \ldots \cdot p_m$$$. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$, the sum of $$$m$$$ over all test cases doesn't exceed $$$6 \cdot 10^4$$$, and the given edges for each test cases form a tree.

Print the maximum distribution index you can get. Since answer can be too large, print it modulo $$$10^9+7$$$.

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 (
	"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"
)

// 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 (
	"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) % 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()
}


On a number axis directed from the left rightwards, n marbles with coordinates x1, x2, ..., xn are situated. Let's assume that the sizes of the marbles are infinitely small, that is in this task each of them is assumed to be a material point. You can stick pins in some of them and the cost of sticking in the marble number i is equal to ci, number ci may be negative. After you choose and stick the pins you need, the marbles will start to roll left according to the rule: if a marble has a pin stuck in it, then the marble doesn't move, otherwise the marble rolls all the way up to the next marble which has a pin stuck in it and stops moving there. If there is no pinned marble on the left to the given unpinned one, it is concluded that the marble rolls to the left to infinity and you will pay an infinitely large fine for it. If no marble rolled infinitely to the left, then the fine will consist of two summands:   the sum of the costs of stuck pins;  the sum of the lengths of the paths of each of the marbles, that is the sum of absolute values of differences between their initial and final positions. Your task is to choose and pin some marbles in the way that will make the fine for you to pay as little as possible.

The first input line contains an integer n (1 ≤ n ≤ 3000) which is the number of marbles. The next n lines contain the descriptions of the marbles in pairs of integers xi, ci ( - 109 ≤ xi, ci ≤ 109). The numbers are space-separated. Each description is given on a separate line. No two marbles have identical initial positions.

Output the single number — the least fine you will have to pay.

package main

import (
	"fmt"
	"io"
	"io/ioutil"
	"bufio"
	"strconv"
	"os"
)

const MAXN int = 3010

var x [MAXN]int
func part(a *[MAXN]int, lo, hi int)(int){
	var pivot int = x[(*a)[hi]]
	i := lo
	for j := lo; j <= hi; j++ {
		if(x[(*a)[j]] <= pivot){
			if i != j {
				(*a)[i] ^= (*a)[j]
				(*a)[j] ^= (*a)[i]
				(*a)[i] ^= (*a)[j]
			}
			i++
		}
	}
	return i - 1
}

func quicksort(a *[MAXN]int, lo, hi int){
	if lo < hi {
		p := part(a, lo, hi)
		quicksort(a, lo, p-1)
		quicksort(a, p+1, hi)
	}
}

var b []byte
var pb, npb int = 0, 0
var err error
func readi(x *int){
	for b[pb] < '0' && b[pb] != '-' || b[pb] > '9' { pb++ }
	var flag bool = false
	if b[pb] == '-' {
        pb++
        flag = true
    }
	npb = pb
	for '0' <=  b[npb] && b[npb] <= '9' { npb++ }
	*x, err = strconv.Atoi( string(b[pb:npb]) )
	pb = npb
	if flag {
        *x = - *x
    }
}

var c, ind [MAXN]int
var dp, sum [MAXN]int64
func main(){
	//var f *os.File
	//f, err = os.Open("dat")
	var r io.Reader
	r = bufio.NewReader(os.Stdin)
	b, err = ioutil.ReadAll(r)
	var n, i, j int
	readi(&n)
	for i = 1; i <= n; i++ {
		readi(&x[i])
		readi(&c[i])
		ind[i]=i
	}
	quicksort(&ind, 1, n)
	for i = 1; i <= n; i++ { sum[i] = sum[i-1] + int64(x[ind[i]])}
	dp[1] = int64(c[ind[1]])
	var tmp int64
	for i = 2; i <= n; i++ {
		dp[i] = dp[i-1] + int64(c[ind[i]])
		for j = 1; j < i; j++ {
			tmp = dp[j-1] + int64(c[ind[j]])+sum[i]-sum[j]-int64(i - j)*					int64(x[ind[j]])
			if tmp < dp[i] { dp[i] = tmp }
		}
	}
	fmt.Printf("%d",dp[n]);
}

package main

import (
	"fmt"
	"io"
	"io/ioutil"
	"bufio"
	"strconv"
	"os"
)

const MAXN int = 3010

var x [MAXN]int
func part(a *[MAXN]int, lo, hi int)(int){
	var pivot int = x[(*a)[hi]]
	i := lo
	for j := lo; j <= hi; j++ {
		if(x[(*a)[j]] <= pivot){
			if i != j {
				(*a)[i] ^= (*a)[j]
				(*a)[j] ^= (*a)[i]
				(*a)[i] ^= (*a)[j]
			}
			i++
		}
	}
	return i - 1
}

func quicksort(a *[MAXN]int, lo, hi int){
	if lo < hi {
		p := part(a, lo, hi)
		quicksort(a, lo, p-1)
		quicksort(a, p+1, hi)
	}
}

var b []byte
var pb, npb int = 0, 0
var err error
func readi(x *int){
	for b[pb] < '0' || b[pb] > '9' { pb++ }
	npb = pb
	for '0' <=  b[npb] && b[npb] <= '9' { npb++ }
	*x, err = strconv.Atoi( string(b[pb:npb]) )
	pb = npb
}

var c, ind [MAXN]int
var dp, sum [MAXN]int64
func main(){
	//var f *os.File
	//f, err = os.Open("dat")
	var r io.Reader
	r = bufio.NewReader(os.Stdin)
	b, err = ioutil.ReadAll(r)
	var n, i, j int
	readi(&n)
	for i = 1; i <= n; i++ {
		readi(&x[i])
		readi(&c[i])
		ind[i]=i
	}
	quicksort(&ind, 1, n)
	for i = 1; i <= n; i++ { sum[i] = sum[i-1] + int64(x[ind[i]])}
	dp[1] = int64(c[ind[1]])
	var tmp int64
	for i = 2; i <= n; i++ {
		dp[i] = dp[i-1] + int64(c[ind[i]])
		for j = 1; j < i; j++ {
			tmp = dp[j-1] + int64(c[ind[j]])+sum[i]-sum[j]-int64(i - j)*					int64(x[ind[j]])
			if tmp < dp[i] { dp[i] = tmp }
		}
	}
	fmt.Printf("%d",dp[n]);
}