#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
typedef long long int ll;
#define FOR(i,a,n) for(int i=a;i<n;++i)
#define TFOR(i,n)FOR(i,0,n)
#define ALL(x) x.begin(),x.end();
const int INF = 1LL << 30;
const ll LLINF = 1LL << 60;
int V;//??????
int E;//???
struct edge { int from, to, cost; };
typedef std::pair<int, int> PP;
std::vector<edge> e;
int Par[100000];
int Rank[100000];
void init(int n) {
	for (int i = 0; i < n; i++) {
		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);
}
// ??????????????????????????????????????¢??° 
bool comp(const edge& e1, const edge& e2) {
	return e1.cost < e2.cost;
}
int kruskal() {
	init(V);
	sort(e.begin(), e.end(), comp);
	int ans = 0;
	for (int i = 0; i<E; i++) {
		if (!same(e[i].from, e[i].to)) {
			unite(e[i].from, e[i].to);
			ans += e[i].cost;
		}
	}
	return ans;
}
int main() {
	while (std::cin >> V >> E&&V) {
		int a, b, c;
		for (int i = 0; i < E; i++) {
			std::cin >> a >> b >> c;
			e.push_back(edge{ a,b,c });
		}
		std::cout << kruskal() << std::endl;
	}
	return 0;
}