Go: // Find the minimum spanning tree by the Kruskal algorithm
// -------------------------------------------------------
// Kruskal(G):
//     A = ∅
//     foreach v ∈ G.V:
//        MAKE-SET(v)
//     foreach (u, v) in G.E ordered by weight(u, v), increasing:
//         if FIND-SET(u) ≠ FIND-SET(v):
//             A = A ∪ {(u, v)}
//             UNION(u, v)
//     return A
//
// Ref: https://en.wikipedia.org/wiki/Kruskal%27s_algorithm

package main

import (
	"bufio"
	"os"
	"sort"
	"strconv"
	"strings"
	"fmt"
	)

var scanner = bufio.NewScanner(os.Stdin)
var (
	matrix [][]int
)

type Edge struct {
	a, b, cost, added int
}
type ByCost []Edge

var edges ByCost

func (a ByCost) Len() int           { return len(a) }
func (a ByCost) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ByCost) Less(i, j int) bool { return a[i].cost < a[j].cost }

func main() {
	for scanner.Scan() {
		buf := strings.Split(scanner.Text(), " ")
		n, _ := strconv.Atoi(buf[0])
		m, _ := strconv.Atoi(buf[1])
		if n == 0 && m == 0 {
			break
		}
		matrix = make([][]int, n)
		for i := range matrix {
			matrix[i] = make([]int, n)
		}
		for i := 0; i < m; i++ {
			scanner.Scan()
			buf_ := strings.Split(scanner.Text(), " ")
			a, _ := strconv.Atoi(buf_[0])
			b, _ := strconv.Atoi(buf_[1])
			cost, _ := strconv.Atoi(buf_[2])
			matrix[a][b] = cost
			matrix[b][a] = cost
		}
		sum := Kruskal(n)
		fmt.Println(sum)
		if len(edges) > 0 {
			edges = []Edge{}
		}
	}
}

func Kruskal_func(size, edge_num int) int {
	for i := 0; i < size; i++ {
		for j := 0; j < size; j++ {
			if matrix[i][j] > 0 {
				tmp := Edge{i, j, matrix[i][j], 0}
				edges = append(edges, tmp)
			}
		}
	}
	sort.Sort(ByCost(edges))
	UF_Init(edge_num)
	for i := 0; i < edge_num; i++ {
		a := edges[i].a
		b := edges[i].b
		if UF_Find(a) != UF_Find(b) {
			edges[i].added = 1
			UF_Union(UF_Find(a), UF_Find(b))
		}
	}
	sum := 0
	for i := 0; i < edge_num; i++ {
		if edges[i].added != 0 {
			// log.Printf("edge(%d,%d) is added\n", edges[i].a, edges[i].b)
			sum += edges[i].cost
		}
	}
	return sum
}

func Kruskal(size int) int {
	edge_num := 0
	for i := 0; i < size; i++ {
		for j := 0; j < size; j++ {
			if matrix[i][j] > 0 {
				edge_num++
			}
		}
	}
	return Kruskal_func(size, edge_num)
}

/*** Union-Find functions ***/
var UF_Parent []int
var UF_Rank []int

func UF_Init(n int) {
	UF_Parent = make([]int, n)
	UF_Rank = make([]int, n)
	for i := 0; i < n; i++ {
		UF_Parent[i] = i
		UF_Rank[i] = 0
	}
}
func UF_Union(a, b int) {
	if UF_Rank[a] < UF_Rank[b] {
		UF_Parent[a] = b
	} else {
		UF_Parent[b] = a
	}
	if UF_Rank[a] == UF_Rank[b] {
		UF_Rank[a]++
	}
}
func UF_Find(a int) int {
	if UF_Parent[a] == a {
		return a
	}
	return UF_Find(UF_Parent[a])
}
/*** Union-Find functions ***/

C++: #include <iostream>
#include <vector>
#include <queue>
#include <utility>
#include <algorithm>
using namespace std;

// UnionFind??¨ ??????????????????
class UnionFindTree{
public:
  UnionFindTree();
  UnionFindTree(int size);
  void init(int size);
  void unite(int a, int b);
  bool same(int a, int b);
  bool isConnected(void);
private:
  int m_size, unite_count;
  vector<int> parent;
  vector<int> rank;
  int find(int a);
};

// ?????????????????????
UnionFindTree::UnionFindTree(){};
UnionFindTree::UnionFindTree(int size){ init(size); };

// UF??¨????????????
void UnionFindTree::init(int size){
  m_size = size;
  unite_count = 0;
  parent.clear();
  rank.clear();
  for(int i = 0; i < size; i++){
    parent.push_back(i);
    rank.push_back(0);
  }
  return;
}

// UF??¨??§???????????£?????????
void UnionFindTree::unite(int a, int b){
  int x = find(a);
  int y = find(b);
  // ?????§???????????£???????????????????????????
  if(x == y){ return; }

  unite_count++;
  if(rank[x] < rank[y]){ parent[x] = y; }
  else{
    parent[y] = x;
    if(rank[x] == rank[y]){ rank[x]++; }
  }
  return;
}

// UF??¨??§???????????????????????????????????????
bool UnionFindTree::same(int a, int b){
  return find(a) == find(b);
}

// UF??¨??§?????????????????????????????¨???????????????????????????
bool UnionFindTree::isConnected(void){
  return unite_count == m_size - 1;
}

// UF??¨?????????????????????
int UnionFindTree::find(int a){
  if(parent[a] != a){ parent[a] = find(parent[a]); }
  return parent[a];
}

// ?????????????????? ?????????
// v: ????????°, adjlist: ??£??\?????????(first: ???????????????, second: ?????????)
vector<pair<int, pair<int, int> > > kruskal_pre(int v, vector<vector<pair<int, int> > > adjlist){
  vector<pair<int, pair<int, int> > > edge; // first: ???????????????, second: ???????????????
  for(int i = 0; i < v; i++)
    for(int j = 0; j < adjlist[i].size(); j++)
      edge.push_back(make_pair(adjlist[i][j].first, make_pair(i, adjlist[i][j].second)));
  sort(edge.begin(), edge.end());

  return edge; // ?????????: ???????????????????????????
}

// ?????????????????¬??????
// v: ????????°, edge: ???????????????????????????(first: ???????????????, second: ???????????????)
int kruskal(int v, vector<pair<int, pair<int, int> > > edge){
  int length = 0; // ??¨????????????
  UnionFindTree uf(v); // UF??¨?????£?¨???¨?????????

  // ???????????????????????????????????????????????????????????£?????????????????§?????????
  for(int i = 0; i < edge.size() && !uf.isConnected(); i++){
    // i??????????°???????????????????
    int cost = edge[i].first;
    int a = edge[i].second.first;
    int b = edge[i].second.second;
    // ????????????????????????????????£????????????????£???°???
    if(uf.same(a, b)){ continue; }

    // UF??¨?????£??????????????????????????´??°
    uf.unite(a, b);
    length += cost;
  }

  return length; // ?????????: ??¨????????????
}


int main(void){
  while(true){
    int n, m;
    cin >> n >> m;
    if(n == 0 && m == 0){ break; }
    
    int a, b, cost;
    vector<pair<int, pair<int, int> > > edgelist(n);
    for(int i = 0; i < m; i++){
      cin >> a >> b >> cost;
      edgelist.push_back(make_pair(cost, make_pair(a, b)));
    }
    sort(edgelist.begin(), edgelist.end());
    
    cout << kruskal(n, edgelist) << endl;
  }

  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define fr first
#define sc second
#define Rep(i,n) for(int i=0;i<(n);i++)
#define Rrep(i, m, n) for(int i=(m);i<(n);i++)
#define All(v) v.begin(),v.end()
#define Uniq(v) v.erase(unique(All(v)),v.end())
typedef pair<int, int> Pii;
typedef pair<int, Pii> Pip;
typedef pair<string, int> Psi;
typedef vector<int> Vi;
const int INF = (1<<30);
const int dx[]={1,0,-1,0}, dy[]={0,-1,0,1};

int n, m;
int cost[101][101];
int mincost[101];
bool used[101];


int prim() {
  fill_n(mincost, 101, INF);
  fill_n(used, 101, false);

  mincost[0] = 0;
  int res = 0;

  while( 1 ) {
    int v = -1;
    Rep(u, n) {
      if( !used[u] && (v == -1 || mincost[u] < mincost[v]) ) v = u;
    }

    if( v == -1 ) break;
    used[v] = true;
    res += mincost[v];

    Rep(u, n) {
      mincost[u] = min(mincost[u], cost[v][u]);
    }
  }

  return res;
}

	  
int main()
{
  while( cin >> n >> m, n+m ) {
    fill_n(*cost, 101*101, INF);

    Rep(i, m) {
      int a, b, c;
      cin >> a >> b >> c;
      cost[a][b] = c;
      cost[b][a] = c;
    }

    cout << prim() << endl;
  }

}