#include <bits/stdc++.h>
using namespace std;

typedef int Weight;
struct Edge {  
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) :
    src(src), dst(dst), weight(weight) { }
};
bool operator < (const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : 
    (e.src != f.src ? e.src < f.src : e.dst < f.dst);
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

pair<Weight, Edges> prim(const Graph &g, int r = 0) {
  int n = g.size();
  Edges T;
  Weight total = 0;
  vector<bool> visited(n);
  priority_queue<Edge> Q;
  Q.push( Edge(-1, r, 0) );    
  while (!Q.empty()) {
    Edge e = Q.top(); Q.pop();
    if (visited[e.dst]) continue;
    T.push_back(e);
    total += e.weight;
    visited[e.dst] = true;
    for(auto f : g[e.dst]){
        if(!visited[f.dst]) Q.push(f);
    }
  }
  T.erase(T.begin());
  return pair<Weight, Edges>(total, T);
}

int main(){
    while(1){
        int n, m; cin >> n >> m;
        if(n==0) break;
        Graph g(n);
        int a, b, cost;
        for(int i=0; i<m; i++){
            cin >> a >> b >> cost;
            g[a].push_back(Edge(a, b, cost));
            g[b].push_back(Edge(b, a, cost));
        }

        cout << prim(g).first << endl;
    }
}
