#include <cstdio>
#include <cstdlib>
#include <functional>
#include <utility>
#include <queue>
#include <list>

using namespace std;

#define N 100

typedef pair<int,int> arw;

int
main (
  int   argc,
  char *argv[ ]
  )
{
  list<arw>::iterator it;
  int i;

  for ( ; ; )
  {
    priority_queue<arw, vector<arw>, greater<arw> > lis;
    list<arw> d[ N ];
    int mk[ N ] = { };
    int res = 0;
    int n, m;

    scanf ( "%d%d", &n, &m );
    if ( n == 0 && m == 0 ) break ;
    for ( i = 0; i < m; ++i )
    {
      int a, b, cost;

      scanf ( "%d%d%d", &a, &b, &cost );
      d[ a ].push_back ( make_pair ( cost, b ) );
      d[ b ].push_back ( make_pair ( cost, a ) );
    }

    /* Prim's Algorithm */
    lis.push ( make_pair ( 0, 0 ) );
    while ( !lis.empty ( ) )
    {
      const arw c = lis.top ( );
      lis.pop ( );

      if ( mk[ c.second ] )
        continue ;

      res += c.first;
      for ( it = d[ c.second ].begin ( ); it != d[ c.second ].end ( ); ++it )
      {
        lis.push ( *it );
      }

      mk[ c.second ] = 1;
    }

    printf ( "%d\n", res );
  }

  return ( EXIT_SUCCESS );
}