public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder out = new StringBuilder();
		int testCases = Integer.parseInt(in.readLine().trim());
		for (int t = 0; t < testCases; t++) {
			int[] nm = readInts(in.readLine());
			int n = nm[0], m = nm[1];
			for (int i = 0; i < m; i++) {
				int[] uvw = readInts(in.readLine());
				int u = uvw[0] - 1, v = uvw[1] - 1, w = uvw[2];
				if (u > v) {
					int aux = u;
					u = v;
					v = aux;
				}
				arista[i] = new Edge(u, v, w);
			}
			E = m;
			V = n;
			Arrays.sort(arista, 0, E);
			long a = KruskalMST(), b = Long.MAX_VALUE;
			nodes = n;
			nl = (int) (Math.log(nodes) / Math.log(2)) + 1;
			for (int i = 0; i < n; i++)
				G[i] = new ArrayList<>();
			for (int i = 0; i < V - 1; i++) {
				G[MST[i].origen].add(new Edge(MST[i].origen, MST[i].destino, MST[i].peso));
				G[MST[i].destino].add(new Edge(MST[i].destino, MST[i].origen, MST[i].peso));
			}
			bfs(0);
			process();
			for (int i = 0; i < E; i++)
				if (!used[i])
					b = Math.min(b, a + arista[i].peso - LCA(arista[i].origen, arista[i].destino));
			if (b == Long.MAX_VALUE)
				b = a;
			out.append(a + " " + b + "\n");
		}
		System.out.print(out);

	}