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

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

struct edge
{
	int from;
	int to;
	int cost;

	edge (int from, int to, int cost) : from(from),to(to),cost(cost) {}
};

bool cmp(const edge& e1, const edge& e2)
{
	return e1.cost < e2.cost;
}

int par[101];
int rank[101];

void init(int n)
{
	rep(i,n)
	{
		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);
}

int main()
{
	int n,m;
	while(cin >> n >> m)
	{
		if(n == 0 && m == 0) break;
			
		
		vector<edge> v;

		rep(i,m)
		{
			int t1,t2,temp;
			cin >> t1 >> t2 >> temp;

			v.push_back(edge(t1,t2,temp));
		}

		sort(v.begin(),v.end(),cmp);

		init(n);
		int res = 0;
		

		rep(i,m)
		{
			edge e = v[i];

			if(!same(e.from,e.to))
			{
				unite(e.from,e.to);
				res += e.cost;
			}
		}

		cout  << res << endl;
	}
	return 0;
}