#include <iostream>
#include <vector>
#include <queue>
using namespace std;

class Edge{
public:
	int edge_dist;
	int cost;
	Edge() : edge_dist(0), cost(0) {}
	Edge(int _dist, int _cost) : edge_dist(_dist), cost(_cost) {}
};

bool operator==(const Edge &a, const Edge &b){
	return (a.cost == b.cost);
}
bool operator!=(const Edge &a, const Edge &b){
	return !(a == b);
}
bool operator<(const Edge &a, const Edge &b){
	return (a.cost < b.cost);
}
bool operator>=(const Edge &a, const Edge &b){
	return !(a < b);
}
bool operator>(const Edge &a, const Edge &b){
	return (a.cost > b.cost);
}
bool operator<=(const Edge &a, const Edge &b){
	return !(a > b);
}
class Prim{
	vector< int > vlist; //??¢?´¢?????? -> 1 / else -> 0
	vector< vector< Edge > > edge;
	vector< Edge > tree;
	priority_queue< Edge, vector< Edge >, greater< Edge > > priq;
	
public:
	
	void Init(int size){
		vlist.clear();
		edge.clear();
		
		vlist.resize(size);
		edge.resize(size);
	}
	
	void Put(int s, int t, int cost){
		edge[s].push_back(Edge(t, cost));
	}
	
	int Run(){
		int sum_cost = 0;
		
		vlist[0] = 1;
		for(vector< Edge >::iterator it = edge[0].begin(); it != edge[0].end(); ++it){
			priq.push(*it);
		}
		
		while(!priq.empty()){
			
			Edge ed = priq.top();
			priq.pop();
			
			if(vlist[ed.edge_dist])continue;
			
			tree.push_back(ed);
			vlist[ed.edge_dist] = 1;
			sum_cost += ed.cost;
			
			for(vector< Edge >::iterator it = edge[ed.edge_dist].begin(); it != edge[ed.edge_dist].end(); ++it){
				if(vlist[(*it).edge_dist] == 0){
					priq.push(*it);
				}
			}
		}
		return sum_cost;
	}
};

int main(){
	
	int v, e, s, t, w;
	Prim prim;
	
	while(cin >> v >> e, v || e){
		
		prim.Init(v);
		
		for(int i = 0; i < e; ++i){
			cin >> s >> t >> w;
			
			prim.Put(s, t, w);
			prim.Put(t, s, w);
		}
		
		cout << prim.Run() << endl;
	}
}