// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0180

#include <stdio.h>
#include <algorithm>

int A[10010], B[10010];
std::pair<int, int> bridge[10010];

int parent[110]; //代表元

int root(int a) {
	if(parent[a] == a) { return a; }
	parent[a] = root(parent[a]);
	return parent[a];
}

int main(void) {
	int n, m;
	
	while(true) {
		scanf("%d %d", &n, &m);
		if ( n == 0 ) { break; }
		
		for(int i=0; i<n; i++) {
			parent[i] = i;
		}
		
		for(int i=0; i<m; i++) {
			bridge[i].second = i;
			scanf("%d %d %d", &A[i], &B[i], &(bridge[i].first));
		}
		
		sort(bridge, bridge+m);
		
		int sum = 0;
		for(int i=0; i<m; i++) {
			int bid = bridge[i].second;
			if ( root(A[bid]) == root(B[bid]) ) { continue; } //既に連結
			
			sum += bridge[i].first;
			parent[root(A[bid])] = root(B[bid]);
		}
		
		printf("%d\n", sum);
	}
}