import java.util.*;
public class Main {
	private static Scanner in;

	public static void main(String[] args) {
		in = new Scanner(System.in);
		int i,t,n,e,j,f,o,h,g,temp1,temp2,current;
		int a,b,m,mm,k,min,mincost,minIndex=0;
		int[][] nextArray;
		ArrayList<Integer> U = new ArrayList<Integer>();
		ArrayList<Integer> V = new ArrayList<Integer>();
		t = in.nextInt();
		for(i = 1;i <= t;i++){
			mincost = 0;
			U.clear();
			V.clear();
			n = in.nextInt();
			e = in.nextInt();
			nextArray = new int[n][n];
			for( m = 0;m < n;m++)
				for( mm = 0;mm < n;mm++)
					nextArray[m][mm]=-1;
			for( j = 0;j < e;j++) {
				a = in.nextInt();
				b = in.nextInt();
				k = in.nextInt();
				nextArray[a][b]=k;
				nextArray[b][a]=k;
			}
			U.add(0);
			for (f = 1;f < n;f++)
				V.add(f);
			for( o = 1;o < n;o++) {
				min = 1000;
				for( h = 0;h < U.size();h++) {
					    temp1=U.get(h);
					for(g=0;g < V.size();g++) {
					    temp2 = V.get(g);
						current = nextArray[temp1][temp2];
						if(current != -1 && current < min) {
							min = current;
							minIndex = g;
						}
					}
				}
				mincost += min;
				U.add(V.get(minIndex));
				V.remove(minIndex);
			}
			System.out.println(mincost);
		}

	}
}

/**************************************************************
	Problem: 2069
	User: admin
	Language: Java
	Result: Memory Limit Exceed
****************************************************************/