# include <iostream>
# include <algorithm>

const int MAX_N = 100;
const int INF = 100000;

int n, m;
int cost[MAX_N][MAX_N], mincost[MAX_N];
bool used[MAX_N];

int prim(){
	for (int i = 0; i < n; ++i)
		mincost[i] = INF;
	mincost[0] = 0;
	int res = 0;
	while (1){
		int v = -1;
		for (int i = 0; i < n; ++i){
			if (!used[i] && (v == -1 || mincost[i] < mincost[v]))
				v = i;
		}
		if (v == -1)
			break;
		used[v] = true;
		res += mincost[v];

		for (int i = 0;  i < n; ++i)
			mincost[i] = std::min(mincost[i], cost[v][i]);
	
	}
	return res;
}

int main(){
	while (1){
		std::cin >> n >> m;
		if (n == 0 && m == 0)
			break;
		std::memset(used, 0, n);
		for (int i = 0; i < n; ++i){
			for (int j = 0; j < n; ++j)
				cost[i][j] = INF;
		}
		int a, b, c;

		for (int i = 0; i < m; ++i){
			std::cin >> a >> b >> c;
			cost[a][b] = c;
			cost[b][a] = c;
		}
		std::cout << prim() << std::endl;
	}
	return 0;
}