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<queue>
#include<vector>

using namespace std;

class Load{
	public:
	int n,m,o,p;
};

int n,m;
Load load[100];

int prim(){
	vector<int> visit;
	visit.push_back(0);
	int cont=0;
	
	while(1){
		if(visit.size()==n){
			break;
		}
		int maxi=1000000000;
		int maxo=0;
		int maxa=0;
		for(int i=0;i<m;i++){
			for(int j=0;j<visit.size();j++){
				if(load[i].n==visit[j]&&load[i].p==0){
					if(maxi>load[i].o){
						load[i].p=1;
						maxi=load[i].o;
						maxo=load[i].m;
						maxa=i;
					}
				}
				if(load[i].m==visit[j]&&load[i].p==0){
					if(maxi>load[i].o){
						load[i].p=1;
						maxi=load[i].o;
						maxo=load[i].n;
						maxa=i;
					}
				}
			}
		}
		//printf("%d %d %d\n",maxa,maxo,maxi);
		cont+=maxi;
		visit.push_back(maxo);
	}
	return cont;
}

int main(){
	while(1){
	cin>>n>>m;
		if(!n){
			break;
		}
		for(int i=0;i<m;i++){
			int a,b,c;
			cin>>a>>b>>c;
			load[i].n=a;
			load[i].m=b;
			load[i].o=c;
			load[i].p=0;
		}
		printf("%d\n",prim());
	}
	
}

C++: #include <iostream>
#include <algorithm>

using namespace std;

#define MAX 100

int par[MAX],rank[MAX];
int V,E;

struct edge{
  int u,v,cost;
};

bool comp(const edge &e1,const edge &e2){
  return e1.cost < e2.cost;
}

void init(){
  for(int i = 0 ; i < V ; i++){
    par[i] = i;
    rank[i] = 0;
  }
}

int find(int x){
  if(par[x] == x){
    return x;
  }else{
    return par[x] = find(par[x]);
  }
}

void unite(int x,int y){
  x = find(x);
  y = find(y);

  if(x == y) return;

  if(rank[x] < rank[y]){
    par[x] = y;
  }else{
    par[y] = x;
    if(rank[x] == rank[y]){
      rank[x]++;
    }
  }
}

bool same(int x,int y){
  return find(x) == find(y);
}

edge es[MAX*MAX];

int kruskal(){
  init();
  sort(es,es+E,comp);

  int res = 0;
  for(int i = 0 ; i < E ; i++){
    edge e = es[i];
    if(!same(e.u,e.v)){
      res += e.cost;
      unite(e.u,e.v);
    }
  }
  return res;
}

int main(){
  while(cin >> V >> E , (V | E)){
    int a,b,c;
    for(int i = 0 ; i < E ; i++){
      cin >> a >> b >> c;
      es[i] = (edge){a,b,c};
    }
    cout << kruskal() << endl;
  }

  return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;

//#define int long long
#define DEBUG 0
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()
#define dump(o) if(DEBUG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DEBUG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
using pii = pair<int, int>; using vi = vector<int>;
static const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
static const int MOD = (int)(1e9 + 7);

using Weight = int;
class Edge {
public:
	int s; //source
	int d; //destination
	Weight w; //weight
	Edge(int s = 0, int d = 0, Weight w = INF) : s(s), d(d), w(w) {};
	bool operator<(const Edge &e)const { return w < e.w; }
};
using Edges = vector<Edge>;
//???????????\: g[u].push_back(Edge(u, v, c));
//????????¨???????????????????????°??????????????\?????????????????????
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;

//?´?????????????????§????
//Union-Find??¨
struct UnionFind {
	vector<int> p; //parent
	int s; //size ???????????°
	UnionFind(int n) :p(n, -1), s(n) {}
	bool unite(int x, int y) {
		x = root(x); y = root(y);
		if (x == y) return false;
		if (size(x) < size(y)) swap(x, y);
		p[x] += p[y]; p[y] = x; s--;
		return true;
	}
	bool same(int x, int y) { return root(x) == root(y); }
	int root(int x) { return p[x] < 0 ? x : p[x] = root(p[x]); }
	//?????????????´???°
	int size(int x) { return -p[root(x)]; }
};

//????°???¨?????¨
//Kruskal O(|E|log|E|)
pair<Weight, Edges> kruskal(const Graph &g) {
	UnionFind UF(g.size());
	Edges es;
	for (int i = 0; i < g.size(); i++) for (auto &e : g[i]) es.emplace_back(e);
	sort(es.begin(), es.end());
	Weight total = 0;
	Edges T;
	for (auto &e : es) if (!UF.same(e.s, e.d)) {
		T.push_back(e); total += e.w; UF.unite(e.s, e.d);
	}
	return make_pair(total, T);
}

signed main() {
	for (int n, m; cin >> n >> m&&n;) {
		Graph g(n);
		rep(i, 0, m) {
			int a, b, cost; cin >> a >> b >> cost;
			g[a].emplace_back(a, b, cost);
			g[b].emplace_back(b, a, cost);
		}
		auto k = kruskal(g);
		cout << k.first << endl;
	}
	return 0;
}