#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

/*Union Find*/
int par[100];

void init(int n){
  for(int i = 0; i < n; i++){
    par[i] = i;
  }
}

int find(int x){
  if(par[x] == x) return x;
  return par[x] = find(par[x]);
}

void unite(int x, int y){
  x = find(x);
  y = find(y);
  if(x == y) return;

  par[x] = y;
}

bool same(int x, int y){
  return find(x) == find(y);
}

/*Kruskal*/
struct edge {int u, v, cost;};

bool comp(const edge& e1, const edge& e2){
  return e1.cost < e2.cost;
}

vector<edge> es;
int N, M;

int kruskal(){
  vector<edge>::iterator it = es.begin();
  sort(it, it + M, comp);
  init(N);
  int res = 0;
  for(int i = 0; i < M; i++){
    edge e = es[i];
    if(!same(e.u, e.v)){
      unite(e.u, e.v);
      res += e.cost;
    }
  }
  return res;
}

int main(void){
  while(true){
    int a, b, cost;
    cin >> N >> M;
    if(N == 0 && M == 0) break;
    es.clear();
    for(int i = 0; i < M; i++){
      cin >> a >> b >> cost;
      edge tmp;
      tmp.u = a, tmp.v = b; tmp.cost = cost;
      es.push_back(tmp);
    }
    cout << kruskal() << endl;
  }
  return 0;
}