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 <bits/stdc++.h>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;

typedef long long LL;
typedef pair<int, int> PII;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<bool> VB;
typedef vector<VB> VVB;

typedef vector<PII> VPII;

#define fst first
#define snd second
#define MP make_pair
#define PB push_back
#define ALL(x) (x).begin(),(x).end()
#define RANGE(x,y,maxX,maxY) (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define DUMP( x ) cerr << #x << " = " << ( x ) << endl

template < typename T > inline T fromString(const string &s) { T res; ISS iss(s); iss >> res; return res; };
template < typename T > inline string toString(const T &a) { OSS oss; oss << a; return oss.str(); };

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const int DX[]={1,0,-1,0},DY[]={0,-1,0,1};

/***** Union-Find *****/

const int MAX_N = 10000;
int Par[MAX_N];
int Rank[MAX_N];

void union_find_init(int n){
	for(int i=0; i<n; ++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);
}

/***** /Union-Find *****/

/***** Kruskal *****/

struct edge {
	int from,
		to,
		cost;
};

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

const int MAX_E = 10000;
edge es[MAX_E];
int V, E;

int kruskal(void){
	sort(es, es + E, comp);
	union_find_init(V);

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

	return res;
}

/***** /Kruskal *****/

int main(void) {
	while (cin >> V >> E, V) {
		E *= 2;
		for (int i = 0; i < E / 2; i++) {
			int a, b, cost;
			cin >> a >> b >> cost;
			es[i * 2].from = a;
			es[i * 2].to = b;
			es[i * 2].cost = cost;
			es[i * 2 + 1].from = b;
			es[i * 2 + 1].to = a;
			es[i * 2 + 1].cost = cost;
		}

		int res = kruskal();

		cout << res << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cfloat>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <algorithm>
#include <functional>
#include <utility>
#include <numeric>

using namespace std;

typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); ++i )
#define EACH( v, c ) for ( auto &v : c )

#define ITER( c ) __typeof( (c).begin() )
#define IREP( c, it ) for ( ITER(c) it = c.begin(); it != c.end(); ++it )
#define ALL( c ) (c).begin(), (c).end()

#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define DEBUG( x ) cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << ( x ) << endl

class UnionFind
{
private:
	vector<int> ary;
	vector<int> rank;

public:
	UnionFind( int n ) : ary( n ), rank( n, 0 )
	{
		REP( i, 0, n )
		{
			ary[i] = i;
		}

		return;
	}

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

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

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

		if ( x == y )
		{
			return;
		}

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

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

		return;
	}
};

struct Edge
{
	int from, to, cost;

	Edge()
	{
		return;
	}

	Edge( int f, int t, int c ) : from( f ), to( t ), cost( c )
	{
		return;
	}

	bool operator < ( const Edge &a ) const
	{
		return cost < a.cost;
	}
};

int main()
{
	cin.tie( 0 );
	ios::sync_with_stdio( false );

	while ( true )
	{
		int n, m;
		cin >> n >> m;

		if ( !( n | m ) )
		{
			break;
		}

		vector< Edge > E( m );

		EACH( e, E )
		{
			cin >> e.from >> e.to >> e.cost;
		}

		sort( ALL( E ) );

		int res = 0;
		UnionFind uf( n );

		REP( i, 0, m )
		{
			auto &e = E[i];

			if ( uf.same( e.from, e.to ) )
			{
				continue;
			}

			uf.unite( e.from, e.to );

			res += e.cost;
		}

		cout << res << endl;
	}

	return 0;
}