#include <bits/stdc++.h>

using namespace std;

class Bridge {
public:
	int u, v, cost;
};

Bridge MapData[500];
int Point[100];
int Height[100];


int find(int root) {

	while (root != Point[root]) {
		root = Point[root];
	}

	return root;
}

bool judge(int x, int y) {
	x = find(x);
	y = find(y);
	if (x == y) {
		return false;
	}

	if (Height[x] < Height[y]) {
		Point[x] = y;
	}
	else {
		Point[y] = x;
		if (Height[x] == Height[y]) {
			Height[x]++;
		}	
	}

	return true;
}

bool cmp(Bridge &b1, Bridge &b2) {
	return b1.cost < b2.cost;
}

int solve(int n, int m) {
	int cnt, sum;

	sort(MapData, MapData + m, cmp);
	for (int i = 0; i < n; i++) {
		Point[i] = i;
		Height[i] = 1;
	}

	cnt = sum = 0;
	for (int i = 0; i < m; i++) {
		if (cnt == n - 1) {
			return sum;
		}

		if (judge(MapData[i].u, MapData[i].v)) {
			cnt++;
			sum += MapData[i].cost;
		}
	}

	return sum;
}

int main() {
	int n, m;

	while (1) {
		cin >> n >> m;
		if (n == 0 && m == 0) {
			break;
		}

		for (int i = 0; i < m; i++) {
			cin >> MapData[i].u >> MapData[i].v >> MapData[i].cost;
		}

		cout << solve(n, m) << endl;
	}
	

	return 0;
}
