#include<iostream>
#include<queue>
#include<vector>
#include<functional>
using namespace std;
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define MAX_N 1000
#define INF 1000000000
int dist[MAX_N], color[MAX_N]; vector<pair<int, int> >X[MAX_N];
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > >Q;
long long prim(int V) {
	for (int i = 0; i < V; i++) { dist[i] = INF; color[i] = WHITE; }
	dist[0] = 0; color[0] = GRAY; Q.push(make_pair(0, 0));
	while (!Q.empty()) {
		pair<int, int>pa = Q.top(); Q.pop();
		int a1 = pa.first, a2 = pa.second; color[a2] = BLACK;
		for (int i = 0; i < X[a2].size(); i++) {
			int to = X[a2][i].first, leng = X[a2][i].second;
			if (dist[to] > leng && color[to] < BLACK) {
				dist[to] = leng; Q.push(make_pair(dist[to], to));
			}
		}
	}
	long long sum = 0; for (int i = 0; i < V; i++) { sum += dist[i]; }//0-indexed
	return sum;
}
int main() {
	int N, M, a, b, c;
	while (true) {
		for (int i = 0; i < MAX_N; i++) { X[i].clear(); }
		cin >> N >> M; if (N == 0 && M == 0) { break; }
		for (int i = 0; i < M; i++) {
			cin >> a >> b >> c; X[a].push_back(make_pair(b, c));
			X[b].push_back(make_pair(a, c));
		}
		cout << prim(N) << endl;
	}
	return 0;
}