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 <algorithm>

using namespace std;

const int MAX_V = 1000;

int cost[MAX_V][MAX_V];
int mincost[MAX_V];
bool used[MAX_V];

int V;
const int INF = 10000000;

int prim(){
  for(int i = 0; i < V; i++){
    mincost[i] = INF;
    used[i] = false;
  }
  mincost[0] = 0;
  int res = 0;
  while(1){
    int v = -1;
    // Xツづ可堕ョツつウツづ按つ「ツ陳クツ点ツづ個つ、ツつソXツつゥツづァツづ個陛督づ個コツスツトツつェツ催渉ャツづ可づ按づゥツ点ツづーツ探ツつキ
    for(int u = 0; u < V; u++){
      if(!used[u] && (v == -1 || mincost[u] < mincost[v]))
		  v = u;
	}
	if(v == -1)
		break;
	used[v] = true;
	res += mincost[v];
	
	for(int u = 0; u < V; u++){
		mincost[u] = min(mincost[u],cost[v][u]);
	}

  }
  return res;
}

int main(){

	int n,m;
	while(cin>>n>>m&&!(n==0&&m==0)){
		for(int i = 0; i < MAX_V; i++){
			fill(cost[i],cost[i]+MAX_V,INF);
		}
		V=n;
		int a,b,c;
		for(int i = 0; i < m; i++){
			cin>>a>>b>>c;
			cost[a][b]=c;
			cost[b][a]=c;
		}
		cout<<prim()<<endl;
	}

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(void)
{
	int n,m,a[500],b[500],cost[500],node[100],size[100],sum;
	int i,j,k;
	while(1) {
		cin>>n>>m;
		sum=0;
		if(n==0&&m==0) break;
		for(i=0;i<m;i++) cin>>a[i]>>b[i]>>cost[i];
		for(i=0;i<m-1;i++) {
			for(j=i+1;j<m;j++) {
				if(cost[i]>cost[j]) {
					swap(cost[i],cost[j]);
					swap(a[i],a[j]);
					swap(b[i],b[j]);
				}
			}
		}
		for(i=0;i<n;i++) size[i]=1,node[i]=i;
		for(i=0;i<m;i++) {
			int x,y,va;
			x=a[i],y=b[i],va=cost[i];
			while(x!=node[x]) x=node[x];
			while(y!=node[y]) y=node[y];
			if(x!=y) {
				sum+=va;
				if(size[x]>=size[y]) node[y]=x;
				else node[x]=y;
			}
		}
		cout<<sum<<endl;
	}
}

C++: #include<cstdio>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
struct edge{
    int to,cost;
    edge(int a,int b):to(a),cost(b){}
    edge(){}
    bool operator>(const edge &d)const{
        return cost>d.cost;
    }
};
int main(){
    int N,M;
    while(scanf("%d%d",&N,&M),N||M){
        vector<vector<edge> >V(N);
        for(int i=0;i<M;i++){
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            V[a].push_back(edge(b,c));
            V[b].push_back(edge(a,c));
        }
        priority_queue<edge,vector<edge>,greater<edge> >Q;
        Q.push(edge(0,0));
        bool used[100];fill(used,used+100,false);
        int res=0;
        while(Q.size()){
            int to=Q.top().to,cost=Q.top().cost;
            Q.pop();
            if(used[to])continue;
            used[to]=true;
            res+=cost;
            for(int i=0;i<V[to].size();i++)Q.push(V[to][i]);
        }
        printf("%d\n",res);
    }
}

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

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

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

void init_union_find(int);
int find(int);
void unite(int,int);
bool same(int,int);
bool comp(const edge& e1,const edge& e2);
int kruskal(void);

using namespace std;

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

  while(cin >> V >> E && V){
    for(i=0;i<E;i++){
      cin >> a >> b >> c;
      es[i].u=a;
      es[i].v=b;
      es[i].cost=c;
    }
    printf("%d\n",kruskal());
  }
  return 0;
}

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

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;
}

C++: #include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(ll (i)=0;(i)<(n);(i)++)
using namespace std;

struct uft{
    vector<ll> par;
    uft(ll n) {
        par.resize(n);
        rep(i,n)par[i]=i;
    }

    ll find(ll a){
        if(par[a]==a){
            return a;
        }else {
            return find(par[a]);
        }
    }

    void unite(ll a,ll b){
        par[find(a)]=find(b);
    }

    bool same(ll a,ll b){
        return find(a) == find(b);
    }
};

struct edge{
    ll s,t,w;
};

bool sortw(edge a,edge b) {
    return a.w < b.w;
}

void q(ll v, ll e) {
    vector<edge> edges;
    rep(i,e){
        ll s,t,w;
        cin>>s>>t>>w;
        edges.push_back(edge{s,t,w});
    }
    sort(edges.begin(),edges.end(),sortw);
    ll sum=0;
    uft uni(v);
    rep(i,e){
        if(!uni.same(edges[i].s,edges[i].t)) {
            uni.unite(edges[i].s,edges[i].t);
            sum+=edges[i].w;
        }
    }
    cout<<sum<<endl;
}

int main() {
    while(true){
        ll v,e;
        cin>>v>>e;
        if(v==0&&e==0)break;
        q(v,e);
    }
}