


For ‘N’ = 3, ‘WELLS[]’ = ‘[1,2,2]’, ‘PIPES[]’ = [ [1, 2, 1], [2 , 3, 1]]. The image shows the costs of connecting houses using pipes. The best strategy is to build a well in the first house with cost 1 and connect the other houses to it with cost 2 so the total cost is 3.

The first line of input contains an integer 'T' representing the number of test cases. Then the test cases follow.
The first line of each test case contains two integers ‘N’ and ‘K’ representing the number of Houses in the village and the size of ‘PIPES’ respectively.
The next line contains ‘N’ single space-separated integers denoting ‘WELLS[i]’.
The next ‘K’ line contains 3 single space-separated integers denoting ‘PIPES[i][0]’, ‘PIPES[i][1]’ and ‘PIPES[i][2]’.
For each test case, print the minimum cost to supply water to all the houses in the village.
The output for each test case is printed in a separate line.
1 <= T <= 100
1 <= N <= 10 ^ 2
0 <= WELLS[i] <= 10^5
1 <= K <= 10000
1 <= PIPES[i][0], PIPES[i][1] <= N
0 <= PIPES[i][2] <= 10^5
PIPES[i][0] != PIPES[i][1]
Where ‘T’ is the number of test cases, ‘N’ is the number of houses in the village, WELL[i]’ denotes the cost of inserting a well at house ‘i’ and ‘PIPES[i][0]’, ‘PIPES[i][1]’ and ‘PIPES[2]’ represents the cost to connect house ‘PIPES[i][0]’ to ‘PIPES[i][1]’.
Time Limit: 1 sec
We can assume this problem as a Shortest path problem/Minimum spanning tree problem. In this problem, in a graph, consider cities as nodes, pipe connects two cities as edges with cost. Now wells cost, they are self-connected edges, we can add an extra node as root node 0, and connect all 0 and every node with costs ‘WELLS[i]’. So that we can have one graph/tree, and can get minimum spanning trees / shortest path in a graph.
In this approach, we will find the minimum spanning tree of the given graph as a minimum spanning tree of a graph is a subset of the graph that connects all the vertices(houses in this case) with the minimum total cost.
To deal with the cost of Well, we will create an extra node having index 0 and connect all the houses to this node and the edge weight will be WELL[i].
It can be considered that Node 0 is the main supply to all the wells of the kingdom.
After updating the graph and storing it in an adjacency list manner.We will use Prims Algorithm using priority queue to find the cost of minimum spanning tree.