#P1401D. Maximum Distributed Tree
Maximum Distributed Tree
No submission language available for this problem.
Description
You are given a tree that consists of nodes. You should label each of its edges with an integer in such way that satisfies the following conditions:
- each integer must be greater than ;
- the product of all numbers should be equal to ;
- the number of -s among all integers must be minimum possible.
Let's define as the sum of the numbers on the simple path from node to node . Also, let 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 .
In this problem, since the number can be large, the result of the prime factorization of is given instead.
The first line contains one integer () — the number of test cases.
The first line of each test case contains a single integer () — the number of nodes in the tree.
Each of the next lines describes an edge: the -th line contains two integers and (; ) — indices of vertices connected by the -th edge.
Next line contains a single integer () — the number of prime factors of .
Next line contains prime numbers () such that .
It is guaranteed that the sum of over all test cases doesn't exceed , the sum of over all test cases doesn't exceed , 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 .
Input
The first line contains one integer () — the number of test cases.
The first line of each test case contains a single integer () — the number of nodes in the tree.
Each of the next lines describes an edge: the -th line contains two integers and (; ) — indices of vertices connected by the -th edge.
Next line contains a single integer () — the number of prime factors of .
Next line contains prime numbers () such that .
It is guaranteed that the sum of over all test cases doesn't exceed , the sum of over all test cases doesn't exceed , and the given edges for each test cases form a tree.
Output
Print the maximum distribution index you can get. Since answer can be too large, print it modulo .
Samples
Note
In the first test case, one of the optimal ways is on the following image:

In this case, , , , , , , so the sum of these numbers is .
In the second test case, one of the optimal ways is on the following image:

In this case, , , , , , , so the sum of these numbers is .