#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const int UF_SIZE = 128;
int UF[UF_SIZE];

void UF_init() {
	for(int i = 0; i < UF_SIZE; i++) {
		UF[i] = i;
	}
}

int UF_indx(int a) {
	if(a == UF[a])
		return a;
	else
		return UF_indx(UF[a]);
}

int UF_merge(int a, int b) {
	UF[UF_indx(a)] = UF_indx(b);
}

struct bridge {
	int a, b;
	int cost;
	bridge(int a_, int b_, int cost_) {
		a = a_; b = b_;
		cost = cost_;
	}

	bool operator<(const bridge obj) const {
		return cost < obj.cost;
	}
};

int main() {
	int n, m;
	while(1) {
		cin >> n >> m;
		if(n == 0 && m == 0)
			break;
		vector<bridge> v;
		UF_init();
		for(int i = 0; i < m; i++) {
			int a, b;
			int cost;
			cin >> a >> b >> cost;
			v.push_back(bridge(a, b, cost));
		}

		sort(v.begin(), v.end());
		int ans = 0;
		for(int i = 0; i < v.size(); i++) {
			if(UF_indx(v[i].a) != UF_indx(v[i].b)) {
				UF_merge(v[i].a, v[i].b);
				ans += v[i].cost;
			}
		}
		cout << ans << endl;
	}
}