#include <algorithm>
#include <iostream>
#include <vector>

struct arista {
   int v1, v2;
   int costo;
};

bool operator<(const arista& a, const arista& b) {
   return a.costo < b.costo;
}

int representante(int v, std::vector<int>& repr) {
   if (repr[v] != v) {
      repr[v] = representante(repr[v], repr);
   }
   return repr[v];
}

void une(int v1, int v2, std::vector<int>& repr) {
   repr[representante(v1, repr)] = representante(v2, repr);
}

int main( ) {
   int n, m;
   std::cin >> n >> m;

   std::vector<arista> aristas(m);
   for (int i = 0; i < m; ++i) {
      std::cin >> aristas[i].v1 >> aristas[i].v2 >> aristas[i].costo;
   }

   std::vector<int> repr(n);
   for (int i = 0; i < n; ++i) {
      repr[i] = i;
   }

   std::sort(aristas.begin( ), aristas.end( ));
   for (arista a : aristas) {
      if (representante(a.v1, repr) != representante(a.v2, repr)) {
         std::cout << a.v1 << "---" << a.v2 << " (" << a.costo << ")\n";
         une(a.v1, a.v2, repr);
      }
   }
}

/* ejemplo de entrada
4
6
0 1 1
0 2 5
0 3 2
1 2 2
1 3 4
2 3 1
*/
