#include <iostream>
#include <cmath>

using namespace std;

#define INF 10000000
int cost[110][110];
int mincost[110];
bool used[110];

int prim(int V) {
	for (int i = 0; i < V; i++) {
		mincost[i] = INF;
		used[i] = false;
	}
	mincost[0] = 0;
	int res = 0;

	while (true) {
		int v = -1;
		for (int u = 0; u < V; u++) {
			if (!used[u] && (v == -1 || mincost[u] < mincost[v])) {
				v = u;
			}
		}

		if (v == -1) {
			break;
		}
		used[v] = true;
		res += mincost[v];

		for (int u = 0; u < V; u++) {
			mincost[u] = min(mincost[u], cost[v][u]);
		}
	}
	return res;
}
int main() {
	int n, m;
	while (cin >> n >> m) {
		if (n == 0 &&  m == 0) {
			break;
		}
		for (int i = 0; i < 110; i++) {
			for (int j = 0; j < 110; j++) {
				cost[i][j] = INF;
			}
		}
		int a,b,c;
		for (int i = 0; i < m; i++) {
			cin >> a >> b >> c;
			cost[a][b] = c;
			cost[b][a] = c;
		}
		cout << prim(n) << endl;
	}
	return 0;
}