#include <iostream>
#include <vector>
#include <queue>
#include <utility>
using namespace std;

#define INF 1e9

int prim(vector<vector<pair<int, int> > > adjlist){
  int n = adjlist.size();
  vector<int> result(n, INF);
  int sum = 0;
  
  priority_queue<pair<int, int> > wait;
  wait.push(make_pair(0, 0));
  result[0] = 0;
  while(!wait.empty()){
    int nowcost = -wait.top().first;
    int nowpoint = wait.top().second;
    wait.pop();
    if(nowcost > result[nowpoint]){ continue; }

    sum += nowcost;
    result[nowpoint] = -INF;
    
    for(int i = 0; i < (int)adjlist[nowpoint].size(); i++){
      int nextcost = adjlist[nowpoint][i].first;
      int nextpoint = adjlist[nowpoint][i].second;
      if(result[nextpoint] > nextcost){
        result[nextpoint] = nextcost;
        wait.push(make_pair(-nextcost, nextpoint));
      }
    }
  }

  return sum;
}


int main(void){
  while(true){
    int n, m;
    cin >> n >> m;
    if(n == 0 && m == 0){ break; }
    
    int a, b, cost;
    vector<vector<pair<int, int> > > adjlist(n);
    for(int i = 0; i < m; i++){
      cin >> a >> b >> cost;
      adjlist[a].push_back(make_pair(cost, b));
      adjlist[b].push_back(make_pair(cost, a));
    }

    cout << prim(adjlist) << endl;
  }

  return 0;
}