#include <bits/stdc++.h>

#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;
typedef int Weight;
struct Edge{
  int src, dest; Weight weight;
  bool operator < (const Edge &rhs) const {return weight > rhs.weight;}
};

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

Edges prim(Graph &g){
  Edges res;
  vector<int> visited(g.size(), 0);
  priority_queue<Edge> q;
  q.push((Edge){-1, 0, 0});
  while (!q.empty()) {
    Edge e = q.top();
    q.pop();
    if (visited[e.dest]) continue;
    if (e.src >= 0) res.push_back(e);
    visited[e.dest] = 1;
    for (Edge i : g[e.dest]) if(visited[i.dest] == 0) q.push(i);
  }
  sort(res.rbegin(), res.rend());
  return res;
}

int main() {
  while(1) {
    int n,m;
    cin>>n>>m;
    if(!n)break;
    Graph g(n);
    REP(i,m) {
      int a,b,c;
      cin>>a>>b>>c;
      g[a].push_back((Edge){a,b,c});
      g[b].push_back((Edge){b,a,c});
    }
    auto es = prim(g);
    int sum = 0;
    for(auto e:es) sum += e.weight;
    cout<<sum<<endl;
  }
  return 0;
}