#include <iostream>
using namespace std;
int n,m;
int mp[101][101];

int check() {
  int flg[101] ={};
  int dis[101];
  int sum = 0;
  for(int i=0;i<n;i++) dis[i] = 1000000000;
  dis[0]  = 0;

  for(int k=0;k<n;k++) {
    int mincost = 1000000000;
    int ima;
    for(int i=0;i<n;i++) {
      if(mincost > dis[i] && flg[i] == 0) {
	mincost = dis[i];
	ima = i;
      }
    }
    sum += mincost;
    flg[ima] = 1;
    // cout << mincost<<endl;;
    
    for(int i=0;i<n;i++) 
      if(mp[ima][i] < dis[i] && flg[i] == 0 )
	dis[i] = mp[ima][i];
  }


  return sum;

}

int main() {
  
  while(1) {
    cin >> n >> m;

    if(n == 0 && m ==0) break;

    for(int i=0;i<n;i++) 
      for(int j=0;j<n;j++)
	mp[i][j] = 1000000000;

    int a,b,cost;
    for(int i=0;i<m;i++){
      cin >> a >> b >> cost;
      mp[a][b] = cost;
      mp[b][a] = cost;
    }
    
    
    int ans = check();
    
    cout << ans << endl;
    
  }

  return 0;
}