#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> mp;
#define inf 1e9


int main(){
    while(1){
	int n,m;
	cin>>n>>m;
	if(n==0)break;
	vector<vector<mp> > g(n);
	for(int i=0;i<m;i++){
		int a,b,c;
		cin>>a>>b>>c;
		g[a].push_back(mp(c,b));
		g[b].push_back(mp(c,a));
	}
	priority_queue<mp,vector<mp>,greater<mp> > q;
	for(int i=0;i<g[0].size();i++)q.push(g[0][i]);
	int sum = 0;
	vector<bool> used(n,false);
	used[0] = true;
	//cout<<"ok"<<endl;
	while(!q.empty()){
		mp now = q.top();
		q.pop();
		int nn = now.second;
		int cost = now.first;
		if(used[nn])continue;
	//	cout<<nn<<' '<<cost<<endl;
		used[nn] = true;
		sum+=cost;
		for(int i=0;i<g[nn].size();i++){
		    	if(used[g[nn][i].second])continue;
			q.push(g[nn][i]);
		}

	}
	cout<<sum<<endl;
	}
	return 0;
}

