import java.util.Scanner;
public class Main{
static int n;
static int e;
static float c[][];
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
n = in.nextInt();
e = in.nextInt();
c = new float[n+1][n+1];
for(int i = 0;i<=n;i++)
for(int j = 0;j<=n;j++)
c[i][j] = Float.MAX_VALUE;
for(int i = 0;i<e;i++)
{
int j = in.nextInt();
int k = in.nextInt();
c[j][k] = in.nextFloat();
c[k][j] = c[j][k];
}
prim(n,c);
}
public static void prim(int n,float c[][])
{
float lowcost[] = new float[n+1];
int[] closest = new int[n+1];//
boolean[] s = new boolean[n+1];
s[1] = true;
for(int i = 2;i<=n;i++)
{
lowcost[i] = c[1][i];
closest[i] = 1;
s[i] = false;
}
for(int i = 1;i<n;i++)
{
float min = Float.MAX_VALUE;
int j = 1;
for(int k = 2;k<=n;k++)
{
if((lowcost[k] < min)&&(!s[k]))
{
min = lowcost[k];
j = k;
}
}
System.out.println(closest[j]+" "+j);
s[j] = true;
for(int k = 2;k<=n;k++)
{
if((c[j][k] < lowcost[k])&&(!s[k]))
{
lowcost[k] = c[j][k];
closest[k] = j;
}
}
}
}
}