#include <iostream>
using namespace std;
#define MAX 101
#define INF (1<<21)

int n,bri[MAX][MAX];

void prim(){
  bool visit[MAX];
  int cost[MAX],d[MAX];

  for(int i = 0 ; i < n ; i++){
    visit[i] = false;
    cost[i] = INF;
  }

  cost[0] = 0;
  d[0] = -1;

  while(true){
    int u ,mincost = INF;
    for(int i = 0 ; i < n ; i++){
      if(!visit[i]&&cost[i] < mincost){
	mincost = cost[i];
	u = i;
      }
    }
    if(mincost == INF) break;
    visit[u] = true;

    for(int j = 0 ; j < n ; j++){
      if(visit[j] || bri[u][j] == INF) continue;
      if(bri[u][j] < cost[j]){
	cost[j] = bri[u][j];
	d[j] = u;
      }
    } 
  }

  int sum = 0;
  for(int i = 0 ; i < n ; i++){
    if(d[i] == -1) continue;
    sum += bri[i][d[i]];
  }

  cout << sum << endl;
}

int main(){
  int m,a,b,c;

  while(cin >> n >> m , n + m){
    for(int i = 0 ; i < MAX ; i++)
      for(int j = 0 ; j < MAX ; j++)
	bri[i][j] = INF;

    for(int i = 0 ; i < m ; i++){
	cin >> a >> b >> c;
	bri[a][b] = bri[b][a] = c;
    }

    prim();
  }

  return 0;
}