#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdlib>
#include<vector>
#define MAX_V 1000
#define INF 1<<29
#define cl(a,b) for(int i=0;i<a;i++)for(int j=0;j<b;j++)cost[i][j] = INF;
#define de(a)  for(int i=0;i<a;i++){cout << mincost[i] << " "; if(i == a-1){cout << endl;}}
#define de2(a,b) for(int I=0;I<a;I++){for(int j=0;j<b;j++){cout << cost[I][j] << " ";}cout << endl;}cout << endl;
using namespace std;

int cost[MAX_V][MAX_V];
int mincost[MAX_V];
bool used[MAX_V];
int V;
 bool connect[10000];

int prime()
{
  for(int i=0;i<V;++i)
    {
      mincost[i]=INF;
      used[i]=false;
    }

  mincost[0] = 0;
  int res = 0;

  //de(10);
  

  //de2(10,10);
  

  while(true)
    {
      int v = -1; 
      for(int u = 0; u<V ; u++ )
	{
	  if(connect[u] && !used[u] && (v == -1 || mincost[u] < mincost[v]))
	    {
	      v = u;
	      //cout << "v = " << v << endl;
	    }
	}

      if(v == -1)break;
      used[v] = true;
      res += mincost[v];

      for(int u = 0; u < V; u++)
	{
	  if(!connect[u]){}
	  else{
	  mincost[u] = min(mincost[u],cost[v][u]);
	  //cout << "mincost[u] = " << mincost[u] << ", cost[v][u] = " << cost[v][u] << endl;
	  }
	}
    }
  return res;
}



int main()
{

  int n,m,a,b;
 

  while(true)
    {
      cin >> n >> m;
      if(n == 0 && m == 0)break;
      cl(m,m);
      memset(connect,false,sizeof(connect));

      V = m;
      for(int i=0;i<m;i++)
	{
	  cin >> a >> b;
	  cin >> cost[a][b];
	  cost[b][a] = cost[a][b];
	  connect[a] = true;
	  connect[b] = true;
	}
      /*     
 for(int i=0;i<m;i++)
	{
	  if(!connect[i])
	    {


	    }
	}
      */


      cout << prime() << endl;




    }

}