// Arup Guha
// 10/8/2015
// Prim's Algorithm - written as an example for the programming team.

/*** This file implements a solution to the 9/12/2015 Practice Problem: Guitars ***/

import java.util.*;

public class guitar_prims {

    public static void main(String[] args) {

        Scanner stdin = new Scanner(System.in);
        int numCases = stdin.nextInt();

        for (int loop=0; loop<numCases; loop++) {
            int v = stdin.nextInt();
            int e = stdin.nextInt();
            ArrayList[] graph = new ArrayList[v];
            for (int i=0; i<v; i++)
                graph[i] = new ArrayList<edge>();

            // Read and stor graph edges.
            for (int i=0; i<e; i++) {
                int v1 = stdin.nextInt()-1;
                int v2 = stdin.nextInt()-1;
                int w = stdin.nextInt();
                graph[v1].add(new edge(v1, v2, w));
                graph[v2].add(new edge(v1, v2, w));
            }

            // Output result.
            System.out.println(prims.mst(graph, 0));
        }
    }
}

class edge implements Comparable<edge> {

	public int v1;
	public int v2;
	public int w;

	public edge(int a, int b, int weight) {
		v1 = a;
		v2 = b;
		w = weight;
	}

	public int compareTo(edge other) {
		return this.w - other.w;
	}
}

class prims {

	public static int mst(ArrayList[] graph, int v) {

		// Mark vertex v as being in mst.
		int n = graph.length;
		boolean[] used = new boolean[n];
		used[v] = true;

		// Add all of v's edges into the priority queue.
		PriorityQueue<edge> pq = new PriorityQueue<edge>();
		for (int i=0; i<graph[v].size(); i++)
			pq.offer( ((ArrayList<edge>)graph[v]).get(i));

		int numEdges = 0, res = 0;

		while (pq.size() > 0) {

			// Get next edge.
			edge next = pq.poll();
			if (used[next.v1] && used[next.v2]) continue;

            // Add new items to priority queue - need to check which vertex is new.
			if (!used[next.v1]) {
                for (int i=0; i<graph[next.v1].size(); i++)
                    pq.offer( ((ArrayList<edge>)graph[next.v1]).get(i));
                used[next.v1] = true;
			}
			else {
                for (int i=0; i<graph[next.v2].size(); i++)
                    pq.offer( ((ArrayList<edge>)graph[next.v2]).get(i));
                used[next.v2] = true;
			}

			// Bookkeeping
			numEdges++;
			res += next.w;
			if (numEdges == n-1) break;
		}

		// -1 indicates no MST, so not connected.
		return numEdges == n-1 ? res : -1;
	}
}
