#include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<string>
#include<queue>

using namespace std;

#define reps(i,f,n) for(int i=f;i<int(n);i++)
#define rep(i,n) reps(i,0,n)

typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<int,int> pii;

class dii{
	public:
	int first;
	int second;
	dii(int f,int s):first(f),second(s){}
	bool operator<(const dii& d)const{
		if(d.first == first) return d.second<second;
		return d.first<first;
	}
};
int main(void){
	A:;
	
	int n,m;
	cin>>n>>m;
	if(n==0)return 0;
	
	vector<pii> edge[100];
	int st = -1;
	int stmin = 1000000000;
	rep(i,m){
		int a,b,c;
		cin>>a>>b>>c;
		edge[a].push_back(pii(b,c));
		edge[b].push_back(pii(a,c));
		
		if(c<stmin){
			stmin = c;
			st = a;
		}
	}
	
	priority_queue<dii> que;
	rep(i,edge[st].size()){
		que.push(dii(edge[st][i].second, edge[st][i].first));
	}
	
	int visit[101]={0};
	visit[st]=1;
	int ans = 0;
	while(1){
		if(que.empty())break;
		dii u = que.top();
		que.pop();
		
		if(visit[u.second]==1)continue;
		visit[u.second]=1;
		
		ans += u.first;
		
		rep(i,edge[u.second].size()){
			que.push(dii(edge[u.second][i].second, edge[u.second][i].first));
		}
	}
	
	printf("%d\n",ans);
	goto A;
}