#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define MAX 101
#define INF (1<<29)

int n, m;
int cost[MAX][MAX];
int minCost[MAX];
bool used[MAX];

int prim(){
  fill(minCost, minCost+MAX, INF);
  memset(used, false, sizeof(used));
  
  minCost[0] = 0;
  int ans = 0;
  
  while(true){
    int v = -1;
    for(int i = 0 ; i < n ; i++){
      if(!used[i] && (v == -1 || minCost[i] < minCost[v])) v = i;
    }
    
    if(v == -1) break;
    used[v] = true;
    ans += minCost[v];
    
    for(int i = 0 ; i < n ; i++){
      minCost[i] = min(minCost[i], cost[v][i]);
    }
  }
  return ans;
}
    
int main(){
  while(cin >> n >> m){
    if(n == 0 && m == 0) break;
    fill(cost[0], cost[0]+MAX*MAX, INF);
    int a, b, C;
    for(int i = 0 ; i < m ; i++){
      cin >> a >> b >> C;
      cost[a][b] = C;
      cost[b][a] = C;
    }
    cout << prim() << endl;
  }
  return 0;
}