Alice and Bob are hosting a programming contest in Abiland. All the judges live in different cities (or same). On the contest day, all judges are going to be present at contest venue. Alice and Bob will pay the travel cost of all the judges. They want to minimize total traveling cost. Cities are connected by roads like a tree. Judges can move alone or can form a group with other judges. Cost of the traveling between cities depends on the size of the group. Two groups can merge. After merging, all members of previous two group will be in the new group. If two judges are in the same group at some point then they will be in same group forever.
The government of Abiland offers Alice and Bob and all contestants x free tickets per person. Using those tickets Alice and Bob can relocate contest venue. However, these tickets can’t be used for the transportation of the judges.
Now, you are given the number of cities (cities are numbered from 1 to ), the number of judges and their current cities, all the roads that connect two cities. You have to tell the minimum cost of traveling for all judges to the contest venue. All contestants, Alice and Bob and Contest Venue are now in city 1. By moving all contestants, Alice and Bob can relocate the contest venue. To pass a road contestants, Alice and Bob use 1 ticket each and they cannot pass a road if they have no tickets left.
Input starts with an integer (), denoting the number of test cases. Each case starts with an integer (), followed by () and ().
The next line contains space separated integers between 0 and , the -th integer in this line denotes the cost of passing a road if a group of size travels.
The next line contains space separated integers between 1 and , denoting the current city of the judges respectively.
Then there are lines each containing two integers, and (, ) meaning that there is a road between city and .
The sum of n in one file is .
For each case, print the minimum traveling cost that Alice and Bob must pay so that all the judges can reach the Contest Venue.
Input | Output |
---|---|
2 5 1 5 2 4 1 2 1 3 2 4 2 5 5 3 0 2 1 1 4 5 3 1 2 1 3 2 4 2 5 | 0 7 |