#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>

#define N 101

using namespace std;

const int infty = 1<<28;

struct Node{
  vector<int> cons;
  vector<int> costs;
};

struct Edge{
  int src,dst;
  Edge():src(0),dst(0){}
  Edge(int s,int d):src(s),dst(d){}
};

int BruteForceMST(const vector<Node> &G){
  int ret = 0;
  bool inMST[G.size()];
  vector< Node > MST;
  vector< Edge > MSTE;

  fill( inMST, inMST+G.size(), false );
  MST.push_back( G[0] );
  inMST[0]=true;
  while( MST.size() < G.size() ){
    int from_id = 0;
    int next_id = 0;
    int mincost = infty;

    for(int i = 0; i < MST.size(); ++i){
      for(int j = 0; j < MST[i].cons.size(); ++j){
	if( !inMST[ MST[i].cons[j] ] &&
	    mincost > MST[i].costs[j] ){
	  from_id = i;
	  next_id = MST[i].cons[j];
	  mincost = MST[i].costs[j];
	}
      }
    }

    inMST[ next_id ] = true;
    ret += mincost;
    MST.push_back( G[next_id] );
    MSTE.push_back( Edge(from_id, next_id) );
  }

  return ret;
}

int main(){
  while(true){
    int n,m;
    vector<Node> G;
    cin >> n >> m;
    if( n == 0 && m == 0 ) break;
    G.resize(n);
    for(int i = 0; i < m; ++i){
      int a,b,cost;
      cin>>a>>b>>cost;
      G[a].cons.push_back( b );
      G[a].costs.push_back( cost );
      G[b].cons.push_back( a );
      G[b].costs.push_back( cost );
    }
    cout << BruteForceMST(G) << endl;
  }
  return 0;
}