#include <iostream>
#include <algorithm>

using namespace std;

const int inf = 999999;

int prim(int a[101][101], int n)
{
	int r = 0, mcost[101];
	bool vis[101];
	
	fill(mcost, mcost+101, inf);
	fill(vis, vis+101, false);
	mcost[0] = 0;
	
	while(1){
		int v = -1;
		
		for(int i = 0; i < n; i++){
			if(!vis[i] && (v == -1 || mcost[v] > mcost[i])){
				v = i;
			}
		}
		
		if(v == -1){
			break;
		}
		vis[v] = true;
		r += mcost[v];
		
		for(int i = 0; i < n; i++){
			mcost[i] = min(mcost[i], a[v][i]);
		}
	}
	
	return r;
}

int main()
{
	int n, m, a[101][101], s, t, u;
	
	while(1){
		cin >> n >> m;
		if(n == 0 && m == 0){
			break;
		}
		
		fill(&a[0][0], &a[101][101], inf);
		for(int i = 0; i <= n; i++){
			a[i][i] = 0;
		}
		
		while(m--){
			cin >> s >> t >> u;
			a[s][t] = a[t][s] = u;
		}
		
		cout << prim(a, n) << endl;
	}
	
	return 0;
}