#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cfloat>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <algorithm>
#include <functional>
#include <utility>
#include <numeric>

using namespace std;

typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); ++i )
#define EACH( v, c ) for ( auto &v : c )

#define ITER( c ) __typeof( (c).begin() )
#define IREP( c, it ) for ( ITER(c) it = c.begin(); it != c.end(); ++it )
#define ALL( c ) (c).begin(), (c).end()

#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define DEBUG( x ) cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << ( x ) << endl

class UnionFind
{
private:
	vector<int> ary;
	vector<int> rank;

public:
	UnionFind( int n ) : ary( n ), rank( n, 0 )
	{
		REP( i, 0, n )
		{
			ary[i] = i;
		}

		return;
	}

	int find( int x )
	{
		if ( ary[x] == x )
		{
			return x;
		}
		else
		{
			return ary[x] = find( ary[x] );
		}
	}

	bool same( int x, int y )
	{
		return find( x ) == find( y );
	}

	void unite( int x, int y )
	{
		x = find( x );
		y = find( y );

		if ( x == y )
		{
			return;
		}

		if ( rank[x] < rank[y] )
		{
			ary[x] = y;
		}
		else
		{
			ary[y] = x;

			if ( rank[x] == rank[y] )
			{
				rank[x]++;
			}
		}

		return;
	}
};

struct Edge
{
	int from, to, cost;

	Edge()
	{
		return;
	}

	Edge( int f, int t, int c ) : from( f ), to( t ), cost( c )
	{
		return;
	}

	bool operator < ( const Edge &a ) const
	{
		return cost < a.cost;
	}
};

int main()
{
	cin.tie( 0 );
	ios::sync_with_stdio( false );

	while ( true )
	{
		int n, m;
		cin >> n >> m;

		if ( !( n | m ) )
		{
			break;
		}

		vector< Edge > E( m );

		EACH( e, E )
		{
			cin >> e.from >> e.to >> e.cost;
		}

		sort( ALL( E ) );

		int res = 0;
		UnionFind uf( n );

		REP( i, 0, m )
		{
			auto &e = E[i];

			if ( uf.same( e.from, e.to ) )
			{
				continue;
			}

			uf.unite( e.from, e.to );

			res += e.cost;
		}

		cout << res << endl;
	}

	return 0;
}