// 2011/02/15 Tazoe

#include <iostream>
using namespace std;

struct bridge{
	int a;
	int b;
	int c;
	bool chk;
};

int main()
{
	while(true){
		int n, m;
		cin >> n >> m;
		if(n==0&&m==0)
			break;

		struct bridge brd[4950];
		for(int i=0; i<m; i++){
			cin >> brd[i].a >> brd[i].b >> brd[i].c;
			brd[i].chk = false;
		}

		int ttl = 0;
		int num = m;
		while(num>0){
			int i_min = -1;
			int c_min = 200;
			for(int i=0; i<m; i++){
				if(brd[i].chk)
					continue;
				if(brd[i].c<c_min){
					i_min = i;
					c_min = brd[i].c;
				}
			}

			ttl += brd[i_min].c;
			int n_fr = brd[i_min].a;
			int n_to = brd[i_min].b;
			brd[i_min].chk = true;
			num--;

			for(int i=0; i<m; i++){
				if(brd[i].chk)
					continue;
				if(brd[i].a==n_fr)
					brd[i].a = n_to;
				else if(brd[i].b==n_fr)
					brd[i].b = n_to;
				else
					continue;
				if(brd[i].a==brd[i].b){
					brd[i].chk = true;
					num--;
				}
			}
		}

		cout << ttl << endl;
	}

	return 0;
}