Given an undirected complete graph with vertices and triples where , it's guaranteed that , and for any two triples and with different indices we have .
For any two vertices and in the graph (), define the weight of the edge connecting them as follows:
If there exists a triple satisfying and , the weight of edge will be .
Otherwise, the weight of edge will be .
Calculate the total weight of edges in the minimum spanning tree of the graph.
输入格式
There are multiple test cases. The first line of the input contains an integer () indicating the number of test cases. For each test case:
The first line contains two integers and (, ) indicating the number of vertices in the graph and the number of triples.
For the following lines, the -th line contains three integers , and (, ) indicating the -th triple. It's guaranteed that for all we have .
It's guaranteed that the sum of of all test cases will not exceed .
输出格式
For each test case output one line containing one integer indicating the total weight of edges in the minimum spanning tree of the graph.