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

int main(){
  int br[100][100];
  bool f[100];
  int tr[100];
  int n,m;

  while(1){
    cin>>n>>m;
    if(n==0&&m==0) break;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++)
	br[i][j] = -1;
      tr[i]=INT_MAX;
      f[i]=0;
    }    
    for(int k=0;k<m;k++){
      int a,b,cost;
      cin>>a>>b>>cost;
      br[a][b]=cost;
      br[b][a]=cost;
    }
    int comin=0,r,trmin;
    tr[0] = 0;

    for(int k=0;k<n;k++){
      trmin=INT_MAX;
      for(int i=0;i<n;i++)
	if(f[i]==0 && trmin>tr[i]) r=i,trmin=tr[i];
      //cout << trmin<<endl;
      comin+=trmin;
      f[r]=1;
      for(int i=0;i<n;i++){
	if(f[i]==0 && tr[i]>br[r][i] && br[r][i]!=-1) tr[i]=br[r][i];
      }
    }
    cout <<comin<<endl;
  }
  
  return 0;
}

C++: //14
#include<iostream>
#include<algorithm>
#include<numeric>

using namespace std;

int main(){
  for(int n,m;cin>>n>>m,n|m;){
    int d[100][100];
    fill_n(d[0],10000,1<<29);
    for(int i=0;i<m;i++){
      int a,b,c;
      cin>>a>>b>>c;
      d[a][b]=d[b][a]=c;
    }
    int ds[100];
    fill_n(ds,100,1<<29);
    ds[0]=0;
    bool u[100]={};
    int p[100]={};
    for(int i=0;i<n;i++){
      int m=1<<29;
      int v;
      for(int i=0;i<n;i++){
	if(!u[i]&&ds[i]<m){
	  m=ds[i];
	  v=i;
	}
      }
      u[v]=true;
      for(int i=0;i<n;i++){
	if(ds[i]>ds[v]+d[v][i]){
	  ds[i]=ds[v]+d[v][i];
	  p[i]=d[v][i];
	}
      }
    }
    cout<<accumulate(p,p+n,0)<<endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for(ll i = (a); i < (b); ++i)
#define FORR(i, a, b) for(ll i = (a); i > (b); --i)
#define REP(i, n) for(ll i = 0; i < (n); ++i)
#define REPR(i, n) for(ll i = n; i >= 0; i--)
#define FOREACH(x, a) for(auto &(x) : (a))
#define VECCIN(x)                                                              \
    for(auto &youso_ : (x)) cin >> youso_
#define mp make_pair
#define bitcnt __builtin_popcount
#define SZ(x) ((ll)(x).size())
#define All(a) (a).begin(), (a).end()
template <typename T = long long> inline T IN() {
    T x;
    cin >> x;
    return (x);
}
inline void CIN() {}
template <class Head, class... Tail>
inline void CIN(Head &&head, Tail &&... tail) {
    cin >> head;
    CIN(move(tail)...);
}
#define CINT(...)                                                              \
    int __VA_ARGS__;                                                           \
    CIN(__VA_ARGS__)
#define LCIN(...)                                                              \
    ll __VA_ARGS__;                                                            \
    CIN(__VA_ARGS__)
#define SCIN(...)                                                              \
    string __VA_ARGS__;                                                        \
    CIN(__VA_ARGS__)
#define Yes(a) cout << (a ? "Yes" : "No") << "\n"
#define YES(a) cout << (a ? "YES" : "NO") << "\n"

typedef long long ll;
typedef unsigned long long ul;
typedef vector<ll> VL;
typedef pair<ll, ll> PL;
typedef priority_queue<ll> PQ;
typedef priority_queue<ll, VL, greater<ll>> PQG;

const int INF = 1e9;
const int MOD = 1e9 + 7;
// const int MOD = 998244353;
const ll LINF = 1e18;

ll V, E;
struct Edge {
    ll from, to, cost;
};
vector<Edge> Edges;

//通常
struct UnionFind {
    VL par, rank;
    UnionFind(int size) { init(size); }
    void init(int size) {
        par.clear();
        rank.clear();
        REP(i, size) {
            par.push_back(i);
            rank.push_back(0);
        }
    }
    int root(int x) {
        if(par[x] == x)
            return x;
        else {
            int r = root(par[x]);
            return par[x] = r;
        }
    }
    void unite(int x, int y) {
        x = root(x);
        y = root(y);
        if(x == y) return;
        if(rank[x] < rank[y]) {
            swap(x, y);
        } else {
            if(rank[x] == rank[y]) rank[x]++;
        }
        par[y] = x;
    }
    bool same(int x, int y) { return root(x) == root(y); }
};

signed main() {
    while(1) {
        cin >> V >> E;
        if(V == 0 && E == 0) break;
        REP(i, E) {
            LCIN(s, t, w);
            Edges.push_back({s, t, w});
        }
        sort(All(Edges),
             [](const Edge &x, const Edge &y) { return x.cost < y.cost; });
        auto uf = UnionFind(V);
        ll ans = 0;
        REP(i, E) {
            Edge e = Edges[i];
            if(!uf.same(e.from, e.to)) {
                uf.unite(e.from, e.to);
                ans += e.cost;
            }
        }
        cout << ans << "\n";
        Edges.clear();
    }
}