Under construction

Still being built. Things may change

Weeping Fig

Time limit3sMemory limit512 MB

Summary
Split the vertices of a connected weighted undirected graph into two nonempty parts so that the total weight of edges crossing the cut is minimized.
Level

Hard8 of 10

Topics
Graph, Minimum spanning tree, Greedy, Divide and conquer
Solved
No attempts yet

Problem

A weeping fig grows in your backyard. Its roots never push under the house, they only spread into the empty space around it, so your friends call it the nice weeping fig. A friend who is building a new house wants the same tree, and yours grows fast enough that you agreed to cut off part of it.

The tree has NN roots, numbered 11 to NN. There are MM connections, each one joining two different roots, and cutting the connection between roots aa and bb takes effort ww.

You want to split the roots into two groups, neither of them empty. No connection may be left between the two groups, and the effort you spend is the sum of the efforts of the connections you cut. Find the smallest total effort.

Input

The first line has the number of test cases TT (1≤T≤201 \le T \le 20).

The first line of each test case has two integers NN and MM, the number of roots and the number of connections (2≤N≤5002 \le N \le 500, N−1≤M≤N(N−1)/2N-1 \le M \le N(N-1)/2).

Each of the next MM lines has three integers aia_i, bib_i, wiw_i (1≤ai,bi≤N1 \le a_i, b_i \le N, ai≠bia_i \ne b_i, 1≤wi≤10001 \le w_i \le 1000), meaning roots aia_i and bib_i are connected and cutting that connection takes effort wiw_i.

At most one connection joins any pair of roots, and every root reaches every other root along the connections.

Output

For each test case, print the smallest total effort on its own line.

Hint

In the first example, pulling root 11 away from the rest takes 2+3+5=102+3+5=10. Splitting {1,2}\{1,2\} from {3,4}\{3,4\} instead takes 3+5+5+5=183+5+5+5=18.

Examples7

  1. Example 1

    Input
    1
    4 6
    1 2 2
    1 3 3
    1 4 5
    2 3 5
    2 4 5
    3 4 8
    
    Expected output
    10
    
  2. Example 2

    Input
    1
    2 1
    1 2 1000
    
    Expected output
    1000
    
  3. Example 3

    Input
    1
    5 4
    1 2 4
    2 3 1
    3 4 7
    4 5 3
    
    Expected output
    1
    
  4. Example 4

    Input
    1
    6 6
    1 2 3
    2 3 1
    3 4 4
    4 5 1
    5 6 5
    6 1 9
    
    Expected output
    2
    
  5. Example 5

    Input
    1
    10 22
    1 2 100
    1 3 100
    1 4 100
    1 5 100
    2 3 100
    2 4 100
    2 5 100
    3 4 100
    3 5 100
    4 5 100
    6 7 100
    6 8 100
    6 9 100
    6 10 100
    7 8 100
    7 9 100
    7 10 100
    8 9 100
    8 10 100
    9 10 100
    3 6 2
    5 10 3
    
    Expected output
    5
    
  6. Example 6

    Input
    1
    6 15
    1 2 1
    1 3 1
    1 4 1
    1 5 1
    1 6 1
    2 3 1
    2 4 1
    2 5 1
    2 6 1
    3 4 1
    3 5 1
    3 6 1
    4 5 1
    4 6 1
    5 6 1
    
    Expected output
    5
    
  7. Example 7

    Input
    4
    2 1
    1 2 7
    3 3
    1 2 5
    2 3 5
    1 3 5
    4 4
    1 2 1
    2 3 1
    3 4 1
    4 1 1
    5 6
    1 2 10
    1 3 10
    2 3 10
    3 4 1
    4 5 6
    3 5 6
    
    Expected output
    7
    10
    2
    7