#include<iostream>
#include<utility>
#include<queue>
#include<functional>
#include<algorithm>
#include<cstring>
#include<vector>
#include<climits>
#include<map>
using namespace std;
#define FOR(i,n) for(int i=0;i<(int)n;i++)
#define FORI(i,k,n) for(int i=k;i<(int)n;i++)
#define ALL(x) (x).begin(),(x).end()
#define INF (2<<25)
typedef pair<int,int> pii;
typedef vector<int> vi;

int main()
{
	while(true)
	{
		int n,m;
		int cost[200][200];
		bool used[200]={};
		
		cin>>n>>m;
		if(n==0&&m==0)break;
		FOR(i,200)FOR(j,200)cost[i][j]=INF;
		
		FOR(i,m)
		{
			int a,b,c;
			cin>>a>>b>>c;
			cost[a][b]=cost[b][a]=c;
		}
		
		priority_queue< pii,vector<pii>,greater<pii> > que;
		que.push(pii(0,0));
		int ans=0;
		while(!que.empty())
		{
			pii now=que.top();que.pop();
			if(used[now.second])continue;
			used[now.second]=true;
			ans+=now.first;
			FOR(i,200)
			{
				if(cost[now.second][i]==INF)continue;
				que.push(pii(cost[now.second][i],i));
			}
		}
		cout<<ans<<endl;
		
		
	}
}