#include <iostream>
#include <vector>
#include <queue>
using namespace std;

class Node
{
	public:
	vector<int> to,c;
};

class Trl
{
	public:
	int p,c,dc;

	Trl(int p, int c, int dc):p(p),c(c),dc(dc){}
	bool operator<(const Trl& t) const
	{
		return c>t.c;
	}
};


int main()
{
	int n,m;
	int a,b,c;
	while(cin >> n >> m , (n||m))
	{
		Node node[100];
		while(m--)
		{
			cin >> a >> b >> c;
			node[a].to.push_back(b);
			node[b].to.push_back(a);
			node[a].c.push_back(c);
			node[b].c.push_back(c);
		}
		
		int total = 0;
		bool visit[100] = {false};
		priority_queue<Trl> q;
		q.push(Trl(0,0,0));

		while(!q.empty())
		{
			Trl t=q.top(); q.pop();
			if(visit[t.p]) continue;

			visit[t.p] = true;
			total += t.dc;

			for(int i=0; i<node[t.p].to.size(); i++)
			{
				int next = node[t.p].to[i];
				int tc   = node[t.p].c[i];
				if(visit[next]) continue;
				
				q.push(Trl(next,tc,tc));
			}
		}

		cout << total << endl;
	}
	return 0;
}