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 <algorithm>
#include <iostream>
using namespace std;


// int N, M, A[10010], B[10010], COST[10010];
// pair<int, int> bridge[10010];
int P[10010];//村のrootが保存

void init(int N){
  for(int i=0; i< N; ++i){
    P[i]= i;
  }
}

int root(int a){//aのroot(代表元)を決める
  if(P[a]==a) return a;//aはroot
  return (P[a] = root(P[a]));//aの親のrootを求め,aの親とする
}
bool is_same_set(int a, int b){
  return root(a) == root(b);
}
void unite(int a, int b){
   P[root(a)] = root(b);
}


int main(){
  int N, M, A[10010], B[10010], COST[10010];
  pair<int, int> bridge[10010];
  int allcost = 0;
  int bridgenum = 0;
  //root決める
  while(cin >> N >> M  && N>0){
    init(N);
    for (int i = 0; i < M; ++i)
    {
      cin >> A[i] >> B[i] >> COST[i];
      bridge[i].first = COST[i];
      bridge[i].second = i ;
    }
    sort(bridge,bridge+M);
    for (int i = 0; i < M; ++i)
    {
      int cost = bridge[i].first;
      int a = A[bridge[i].second];
      int b = B[bridge[i].second];

      if (bridgenum == M-1)
      {
        unite(a,b);
      }
      if(is_same_set(a,b)){

      }else{
        unite(a,b);
        allcost += cost;
        cout <<  allcost << endl;
        bridgenum += 1;
      }


      // 「a から b に cost の橋がかかっている」と表示
    }
    cout <<  allcost << endl;
  }
}

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

class UnionFind{
private:
	int n;
	std::unique_ptr<int[]> par, rank;
public:
	UnionFind(int n);
	int find(int x);
	void unite(int x, int y);
	bool same(int x, int y);
};

UnionFind::UnionFind(int _n)
	:n(_n)
{
	this->par.reset(new int[n]);
	this->rank.reset(new int[n]);
	for(int i=0;i<n;i++){
		this->par[i] = i;
		this->rank[i] = 0;
	}
}

int UnionFind::find(int x){
	if(x == this->par[x]){
		return x;
	}
	return this->par[x] = find(par[x]);
}

void UnionFind::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 UnionFind::same(int x, int y){
	return find(x) == find(y);
}

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

bool comp(const Edge& l_edge, const Edge& r_edge){
	return l_edge.cost < r_edge.cost;
}

const int MAX_N = 100, MAX_M = 100;
const int INF = 1 << 24;

int kruskal(int V, std::vector<Edge> &ev){
		std::sort(ev.begin(), ev.end(), comp);
		UnionFind uf(V);
		int res = 0, ev_size = ev.size();
		for(int i=0;i<ev_size;i++){
			Edge e = ev[i];
			if(!uf.same(e.from, e.to)){
				uf.unite(e.from, e.to);
				res += e.cost;
			}
		}
		return res;
}

int main(){
	int n, m;
	int cost[MAX_N][MAX_N];
	while(std::cin >> n >> m, n){
		std::vector<Edge> ev;

		for(int i=0;i<MAX_N;i++){
			std::fill(cost[i], cost[i]+MAX_N, INF);
		}

		for(int i=0;i<m;i++){
			int a, b, c;
			std::cin >> a >> b >> c;
			Edge e = {a, b, c};
			ev.push_back(e);
		}

		std::cout << kruskal(n, ev) << std::endl;
	}
}

C++: //include
//------------------------------------------
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
using namespace std;

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORR(i,a,b) for(int i=(a);i>(b);i--)
#define REP(i,n)  FOR(i,0,n)

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//debug
//-------------------------------------------
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define VISITED 1
#define NOT_VISITED 0
#define INF 1<<21

//typedef
//-------------------------------------------
typedef long long ll;
int g[101][101];
int visited[101];
int d[101];
int n, m;

int prim() {
    d[0] = 0;

    while(true) {
        int min = INF;
        int u = -1;
        REP(i, n) {
            if (visited[i] != VISITED && d[i] < min) {
                min = d[i];
                u = i;
            }
        }
        if (u < 0) break;

        visited[u] = VISITED;

        REP(v, n) {
            if (visited[v] != VISITED && g[u][v] != 0) {
                if (g[u][v] < d[v]) {
                    d[v] = g[u][v];
                    // visited[v] = VISITED;
                }
            }
        }
    }
    int sum = 0;
    REP(i, n) sum += d[i];
    return sum;
}

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

    while(true) {
        cin >> n >> m;
        if (!n && !m) break;
        REP(i, n) {
            visited[i] = NOT_VISITED;
            d[i] = INF;
        }
        REP(i, m) {
            int s, d, c;
            cin >> s >> d >> c;
            g[s][d] = c;
            g[d][s] = c;
        }
        cout << prim() << endl;
    }

    return 0;
}