#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PA;
const int INF = (1<<30)-1;
int main(){
  int n,m,a,b,c,sum,ma,hiku;
  priority_queue<PA,vector<PA>,greater<PA> > pq;
  while(cin >> n >> m,n!=0||m!=0){
    sum = 0;
    vector<PA> vec[100];
    for(int i=0;i<m;i++){
      cin >> a >> b >> c;
      vec[a].push_back(PA(c,b));
      vec[b].push_back(PA(c,a));
      sum += c;
    }
    bool town[100]={false};
    hiku = 0;
    pq.push(PA(0,0));
    while(!pq.empty()){
      PA p = pq.top();
      pq.pop();
      if(town[p.second]) continue;
      
      hiku += p.first;
      town[p.second] = true;
      
      for(int i=0;i<vec[p.second].size();i++){
	int to = vec[p.second][i].second;
	if(!town[to]) pq.push(PA(vec[p.second][i].first,to));
      }
      
    }
    printf("%d\n",hiku);
  }
  return(0);
}

