//最小全域木したよ!!
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
#define INF ( 1 << 30 )
#define fr first
#define sc second
#define MAX_V 100
typedef pair<int,int> Pi;
typedef pair<int,Pi> Pii;
vector<vector<Pi> > info;
bool used[MAX_V];
int prim(){
  int total = 0;
  fill_n(used,MAX_V,false);
  priority_queue< Pi , vector<Pi> , greater<Pi> > que;
  que.push(Pi(0,0));
  while(!que.empty()){
    Pi p = que.top();
    que.pop();
    if(used[p.sc]++) continue;
    total += p.fr;
    for(int i = 0 ; i < info[p.sc].size() ; i++ ) que.push(info[p.sc][i]);
  }
  return total;
}
int main(){
  int n, m;
  while(cin >> n >> m, n){
    info.resize(n);
    for(int i = 0 ; i < m ; i++ ){
      int a,b,cost;
      cin >> a >> b >> cost;
      info[a].push_back(Pi(cost,b));
      info[b].push_back(Pi(cost,a));
    }
    cout << prim() << endl;
    info.clear();
  }
}