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>
using namespace std;

constexpr int INF = 1e8;
constexpr int N_MAX = 100;
constexpr int M_MAX = 500;
bool visited[N_MAX] = {}; 
int n, m;

struct Edge {
    int to_;
    int cost_;
    Edge(int to, int cost) : to_(to), cost_(cost) {}
};

auto clear(vector<vector<Edge>> &g) -> void {
    for (int i = 0; i < N_MAX; ++i) {
        visited[i] = false;
    }

    for(auto& e : g) {
        g.clear();
    }
}

auto prim(vector<vector<Edge>> &g) -> int {
    auto comp = [](auto lhs, auto rhs){return lhs.first > rhs.first;};
    priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(comp)> que(comp);
    int ans = 0;
    que.push(make_pair(0, 0));
    while (!que.empty()) {
        auto p = que.top();
        que.pop();
        if (visited[p.second]) continue;
        visited[p.second] = true;
        auto edges = g[p.second];
        ans += p.first;
        for(const auto &e : edges) {
            if (!visited[e.to_]) {
                que.push(make_pair(e.cost_, e.to_));
            }
        }
    }
    return ans;
}

auto main(int argc, char const *argv[]) -> int {
    while (cin>>n>>m, n || m) {
        vector<vector<Edge>> g(n);
        clear(g);
        for (int i = 0; i < m; ++i) {
            int a, b, cost;
            cin>>a>>b>>cost;
            g[a].push_back(Edge(b, cost));
            g[b].push_back(Edge(a, cost));
        }
        cout<<prim(g)<<endl;
    }
    return 0;
}

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

using namespace std;

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

int par[101],rank[101];
edge es[200];
int V,E;

void init_union_find(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,rank[x]+=(rank[x]==rank[y]);
}

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

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

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

int main(void){
  int a,b,c;

  while(cin >> V >> E && V){
    for(int i=0;i<E;i++){
      cin >> a >> b >> c;
      es[i].u=a,es[i].v=b,es[i].cost=c;
    }
    cout << kruskal() << endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
 
#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)
#define SORT(c) sort((c).begin(),(c).end())
#define IINF INT_MAX
#define LLINF LLONG_MAX
 
typedef long long ll;
typedef pair<int, int> ii;

#define DEBUG false

struct UnionFind {
  vector<int> data;
  UnionFind(int v) : data(v, -1) {}
  int root(int x) {
    return data[x] < 0 ? x : data[x] = root(data[x]);
  }
  bool same(int x, int y) {
    return root(x) == root(y);
  }
  int size(int x) {
    return -data[root(x)];
  }
  void merge(int x, int y) {
    x = root(x), y = root(y);
    if (x != y) {
      if (size(y) > size(x))
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
  }
};

struct NODE{
	int x, y;
	int cost;
	
	NODE(){x = y = cost = 0;}
	NODE(int x, int y, int cost):x(x), y(y), cost(cost){}
	
	bool operator<(const NODE r){
		return cost < r.cost;
	}
};

int main(){
	
	int n, m;
	while(cin >> n >> m, m, n){
		vector<NODE> v(m);
		rep(i, m){
			int x, y, cost;
			cin >> x >> y >> cost;
			v[i] = NODE(x, y, cost);
		}
		
		SORT(v);
		
		UnionFind uni(n+1);
		
		ll ans = 0;
		rep(i, m){
			int xx = v[i].x;
			int yy = v[i].y;
			if(uni.root(xx) != uni.root(yy)){
				uni.merge(xx, yy);
				ans += v[i].cost;
			}
		}
		
		cout << ans << endl;
		
	}
	
	
	
	return 0;
}

/*
struct BIT {
  int N;
  vector<ll> data;
  BIT(int n) : N(n), data(n + 1, 0) { }
  void add(int a, ll w) {
    ++a;
    for(int x = a; x <= N; x += x & -x) {
      data[x] += w;
    }
  }
  ll sum(int a) {
    ll ret = 0;
    for(int x = a; x > 0; x -= x & -x) {
      ret += data[x];
    }
    return ret;
  }
  
  ll sum(int l, int r){//[l, r)
  	return sum(r) - sum(l);
  }
};

BIT bit = BIT(1);
int m, n;

ll dfs(int pos, int rest){
	ll ret = LLINF;
	
	if(rest == 1) return bit.sum(pos, n+1);
	for(int i = 0; i + pos <= n+1; i++){
		ll hoge1 = bit.sum(pos, pos+i);
		ll hoge2 = dfs(pos+i, rest-1);
		cout << "sum(" << pos << ", " << pos+i << "): " << hoge1 << endl;
		cout << "des(" << pos+i << ". " << rest-1 << "): " << hoge2 << endl;
		cout << endl;
		ret = min(ret, hoge1 + hoge2);
	}
	
	return ret;
}

int main(){

	while(cin >> m >> n, m, n){
		bit = BIT(n);
		rep(i, n){
			ll tmp;
			cin >> tmp;
			bit.add(i+1, tmp);
		}
		
		cout << dfs(1, m) << endl;
		
	}
	
	return 0;
}
*/