spoj#RDRUG. Rage Drug

Rage Drug

Dr. Banner (a.k.a. The Hulk) is synthesizing a drug that will help him control his rage. There are a total of N atoms of various atomic masses.
Banner knows that to create a bond between two atoms the energy required is equal to the sum of the atomic masses of the two atoms.
Currently some of the atoms are connected to one another forming compounds.
Banner wants to join all separate atoms and compounds into a single compound by creating chemical bonds. Each atom can form at max one more bond. Help Dr. Banner to find out the minimum energy required to synthesize his drug.

INPUT

Input starts with an integer T, the number of test cases. Each test case starts with a line containing integers N, M denoting the number of atoms and the number of bonds already formed respectively. M lines contain 2 integers i and j denoting a chemical bond between the ith and the jth atom. The next N lines contains one integer each where the integer on ith line denotes the atomic mass of the ith atom.

OUTPUT

Print T lines, where the ith line is the answer to the ith test case. If it is not possible to link the atoms print -1;
PS: If you mislead Dr. Banner, he might lose his temper and SMASH you to bits.

CONSTRAINTS

1 <= T <= 12
1 <= N <= 100000
1 <= AtomicMass <= 10000
1 <= M <= 10^6 (1000000)
1 <= i, j <= N

Sample Input:

2
6 6
1 2
2 3
1 3
4 5
5 6
4 6
1
3
5
2
4
6
4 0
1
2
3   
4

Sample Output:

3
-1