#include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 114514810
#define MOD 1000000007
#define ALL(a) (a).begin(),(a).end()
#define pb(x) push_back(x)
#define ms(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(10) 
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge{ int from, to, cost; };

int par[10000]; int hukasa[10000];

int find(int x)
{
	if (par[x] == x) return x;
	else par[x] = find(par[x]);
}

void unite(int x, int y)
{
	x = find(x); y = find(y);
	if (x == y) return;
	if (hukasa[x] < hukasa[y]) par[x] = y;
	else
	{
		par[y] = x;
		if (hukasa[x] == hukasa[y]) hukasa[x]++;
	}
}

bool same(int x, int y)
{
	return find(x) == find(y);
}

void init_UFT(int n)
{
	REP(i, n)
	{
		par[i] = i;
		hukasa[i] = 0;
	}
}

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

vector<edge> es;
int main()
{
	int n, m;
	while (cin >> n >> m, n)
	{
		init_UFT(n);
		es.clear();
		int a, b, c;
		REP(i, m)
		{
			cin >> a >> b >> c;
			edge e = { a, b, c };
			es.push_back(e);
		}
		sort(ALL(es), comp);
		int sum = 0;
		REP(i, es.size())
		{
			if (!same(es[i].from, es[i].to))
			{
				unite(es[i].from, es[i].to);
				sum += es[i].cost;
			}
		}
		cout << sum << endl;
	}

	return 0;
}