// Arup Guha
// 3/17/2022
// Solution to Kattis Problem: Minimum Spanning Tree (minspantree), using Kruskal's

import java.util.*;

public class minspantree_kruskal {
	
	public static void main(String[] args) {
		
		Scanner stdin = new Scanner(System.in);
		int numV = stdin.nextInt();
		int numE = stdin.nextInt();
		
		// Process multiple cases.
		while (numV != 0) {
			
			// Create the empty graph.
			ArrayList<edge> list = new ArrayList<edge>();
			
			// Add edges.
			for (int i=0; i<numE; i++) {
				int u = stdin.nextInt();
				int v = stdin.nextInt();
				int w = stdin.nextInt();
				list.add(new edge(u, v, w));
			}
			
			// Get the mst.
			ArrayList<edge> res = kruskals(list, numV);
			
			// Handle this case first.
			if (res == null)
				System.out.println("Impossible");
			
			// Otherwise we have to output it.
			else 
				outputMST(res);
			
			// Get next case.
			numV = stdin.nextInt();
			numE = stdin.nextInt();
		}
		
	}
	
	public static void outputMST(ArrayList<edge> mst) {
		
		// Calculate cost of MST.
		int cost = 0;
		for (edge e: mst)
			cost += e.w;
		
		// Sort in lexicographical edge order.
		Collections.sort(mst, new Comparator<edge>() {
			public int compare(edge a, edge b) {
				if (a.u != b.u) return a.u - b.u;
				return a.v - b.v;
			}
		});
		
		// Output cost and mst.
		System.out.println(cost);
		for (int i=0; i<mst.size(); i++)
			System.out.println(mst.get(i));
	}

	// Returns all the edges in an MST of graph OR null if no MST exists, via Kruskal's Algorithm.
	public static ArrayList<edge> kruskals(ArrayList<edge> list, int n) {
	
		// Create list of edges to sort.
		Collections.sort(list);
			
		// Will store MST here.
		ArrayList<edge> mst = new ArrayList<edge>();
		
		// Create my disjoint set.
		djset dj = new djset(n);
		
		// Keep going if mst is not complete or we've run out of edges to consider.
		int idx = 0;
		while (idx < list.size() && mst.size() < n-1) {
		
			// Get the next edge.
			edge cur = list.get(idx++);
			
			// Union these two sets.
			boolean newC = dj.union(cur.u, cur.v);
			
			// Not a new edge to add.
			if (!newC) continue;
			
			// Just add to the MST.
			mst.add(cur);
		}
		
		// No MST.
		if (mst.size() < n-1) return null;
	
		// Here it is.
		return mst;
	}
}

// Disjoint Set Class with Path Compression (previously shown in class).
class djset {
	
	public int n;
	public int[] par;
	
	public djset(int numV) {
		n = numV;
		par = new int[n];
		for (int i=0; i<n; i++)
			par[i] = i;
	}
	
	public int find(int u) {
		if (par[u] == u) return u;
		return par[u] = find(par[u]);
	}
	
	public boolean union(int u, int v) {
		u = find(u);
		v = find(v);
		if (u == v) return false;
		par[v] = u;
		return true;
	}
}

// Edge class
class edge implements Comparable<edge> {

	public int u;
	public int v;
	public int w;
	
	public edge(int start, int end, int weight) {
		u = Math.min(start,end);
		v = Math.max(start,end);
		w = weight;
	}
	
	public int compareTo(edge other) {
		if (w != other.w) return w - other.w;
		if (u != other.u) return u - other.u;
		return v - other.v;
	}
	
	public String toString() {
		return u+" "+v;
	}
}