#include <iostream>
#include <vector>
#include <queue>
#define INF (1e9)
#define cost first
#define node second
using namespace std;
typedef pair <int, int> P;
int n,m;
vector<P> G[101];

int prim(){
  int res = 0;
  bool visited[101]={};
  int D[101];
  for(int i=0;i<n;i++) D[i] = INF;
  priority_queue <P, vector<P>, greater<P> > Q;
  Q.push(P(0,0));
  while(!Q.empty()){
    P t = Q.top(); Q.pop();
    if(visited[t.node]) continue;
    visited[t.node] = true;
    res += t.cost;
    for(int i=0;i<G[t.node].size();i++){
      int nnode = G[t.node][i].node;
      int ncost = G[t.node][i].cost;
      if(D[nnode] > ncost) Q.push(P(ncost,nnode)),D[nnode]=ncost;
    }
  }
  return res;
}

int main() {
  while(1){
    cin >> n >> m;
    if(n == 0 && m ==0) break;
    for(int i=0;i<m;i++) {
      int a,b,c;
      cin >> a>>b>>c;
      G[a].push_back((P){c,b});
      G[b].push_back((P){c,a});
    }
    cout << prim()<<endl;
  }
  return 0;
}