#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
struct Bri{
	int fn;
	int tn;
	int cost;
	bool operator <(const Bri &bBri)const{
		return cost>bBri.cost;
	}
};
int main(void){
	while (true){
		int citynum, brinum;
		cin >> citynum >> brinum;
		if (citynum == 0 && brinum == 0)break;
		int CostCol[100][100];
		for (int i = 0; i < 100; ++i){
			for (int j = 0; j < 100; ++j){
				CostCol[i][j] = 10000000;
			}
		}
		vector<int> IsSelected;
		for (int i = 0; i < citynum; ++i){
			IsSelected.push_back(0);
		}
		vector<Bri> BriQue;
		for (int i = 0; i < brinum; ++i){
			int a, b, cost;
			cin >> a >> b >> cost;
			BriQue.push_back({ a, b, cost });
		}
		sort(BriQue.begin(), BriQue.end());
		reverse(BriQue.begin(),BriQue.end());
		int CostSum=0;
		IsSelected[BriQue[0].fn] = 1; IsSelected[BriQue[0].tn] = 1;
		CostSum += BriQue[0].cost; 
		while (1){
			for (auto it = BriQue.begin(); it != BriQue.end(); ++it){
				if (IsSelected[(*it).fn] + IsSelected[(*it).tn] == 1){
					IsSelected[(*it).fn] = 1;
					IsSelected[(*it).tn] = 1;
					CostSum += (*it).cost;
					break;
				}
				else{

				}
			}
			if (all_of(IsSelected.begin(), IsSelected.end(), [](int x){return x == 1; })){
				cout << CostSum << endl;
				break;
			}
		}
	}
	return 0;
}