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 <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
using namespace std;
int par[104];
int ran[105];
void init(int n) {
	for (int i = 0; i<n; i++) {
		par[i] = i;
		ran[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 (ran[x]<ran[y]) {
		par[x] = y;
	}
	else {
		par[y] = x;
		if (ran[x] == ran[y]) ran[x]++;
	}
}
bool same(int x, int y) {
	return find(x) == find(y);
}
struct edge { int s, t, cost; };
bool comp(edge& e1, edge& e2) {
	return e1.cost < e2.cost;
}
int main()
{
	int n, m;
	while (cin >> n >> m, n != 0) {
		init(n);
		edge b[10008];
		int x, y, z;
		for (int i = 0; i < m; i++) {
			cin >> x >> y >> z;
			b[i].s = x;
			b[i].t = y;
			b[i].cost = z;
		}
		int ans = 0;
		sort(b, b + m, comp);
		for (int i = 0; i < m; i++) {
			edge now = b[i];
			if (!same(now.s, now.t)) {
				unite(now.s, now.t);
				ans += now.cost;
			}
		}cout << ans<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <utility>
#include <queue>
#include <cmath>
#include <algorithm>
#include <random>
#include <string.h>

using namespace std;
typedef long long ll;
const double pi = acos(-1);
#define FOR(i,a,b) for (ll i=(a);i<(b);i++)
#define RFOR(i,a,b) for (ll i=(b)-1;i>=(a);i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
	REP(i, v.size()) { if (i)os << " "; os << v[i]; }return os;
}
template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v) {
	REP(i, v.size()) { if (i)os << endl; os << v[i]; }return os;
}

class union_find {
public:
    union_find(int n)
        : par_(n, -1)
    {}
    void init(int n) {
        par_.assign(n, -1);
    }

    int root(int x) {
        return par_[x] < 0 ? x : par_[x] = root(par_[x]);
    }

    bool unite(int x, int y) {
        x = root(x); y = root(y);
        if(x == y) {
            return false;
        } else {
            if(par_[x] < par_[y]) {
                par_[x] += par_[y];
                par_[y] = x;
            } else {
                par_[y] += par_[x];
                par_[x] = y;
            }
            return true;
        }
    }

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

    int size(int x) {
        return -par_[root(x)];
    }

private:
    std::vector<int> par_;
};

//s
using weight = int;

struct edge {
    int from, to;
    weight cost;
    bool operator<(edge const& rhs) const {
        return cost > rhs.cost;
    }
};

using edges = std::vector<edge>;
using graph = std::vector<edges>;

edges kruskal (int V, edges& es) {
    std::sort(es.rbegin(), es.rend());
    union_find uf(V);
    edges res;
    for(int i=0; i<es.size(); ++i) {
        edge e = es[i];
        if(!uf.same(e.from, e.to)) {
            uf.unite(e.from, e.to);
            res.push_back(e);
        }
    }
    return res;
}

edges prim(graph& g) {
    edges res;
    std::vector<bool> used(g.size(), 0);
    priority_queue<edge> que;
    que.push((edge{-1, 0, 0}));
    while(!que.empty()) {
        edge e = que.top(); que.pop();
        if(used[e.to]) continue;
        if(e.from >= 0) {
            res.push_back(e);
        }
        used[e.to] = true;
        for(auto ee : g[e.to]) {
            if(!used[ee.to]) {
                que.push(ee);
            }
        }
    }
    sort(res.rbegin(), res.rend());
    return res;
}


int main() {
	int n,m;
    cin >> n >> m;
    edges es,answer;
    while(n!=0 && m!=0){
        int sum = 0;
        edge e;
        es.erase(es.begin(),es.begin()+es.size());
        for(int i=0;i<m;++i){
            int v1,v2,c;
            cin >> v1 >> v2 >> c;
            e = edge{v1,v2,c};
            es.push_back(e);
        }
        answer = kruskal(n,es);
        for(auto a: answer){
            sum += a.cost;
        }
        cout << sum << endl;
        cin >> n >> m;
    }
}

C++: //最小全域木したよ!!
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
#define INF ( 1 << 30 )
#define fr first
#define sc second
#define MAX_V 100
typedef pair<int,int> Pi;
typedef pair<int,Pi> Pii;
vector<vector<Pi> > info;
bool used[MAX_V];
int prim(){
  int total = 0;
  fill_n(used,MAX_V,false);
  priority_queue< Pi , vector<Pi> , greater<Pi> > que;
  que.push(Pi(0,0));
  while(!que.empty()){
    Pi p = que.top();
    que.pop();
    if(used[p.sc]++) continue;
    total += p.fr;
    for(int i = 0 ; i < info[p.sc].size() ; i++ ) que.push(info[p.sc][i]);
  }
  return total;
}
int main(){
  int n, m;
  while(cin >> n >> m, n){
    info.resize(n);
    for(int i = 0 ; i < m ; i++ ){
      int a,b,cost;
      cin >> a >> b >> cost;
      info[a].push_back(Pi(cost,b));
      info[b].push_back(Pi(cost,a));
    }
    cout << prim() << endl;
    info.clear();
  }
}