#include<vector>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
  int i,j;
  int b[100],m,n,x,y,z;
  int sm;
  vector<pair<int,pair<int,int> > > a;
  while(cin>>n>>m&&n+m){
    a.clear();
    for(i=0;i<m;i++){
      cin>>x>>y>>z;
      a.push_back(make_pair(z,make_pair(x,y)));
    }
    sort(a.begin(),a.end());
    memset(b,-1,sizeof(b));
    sm=0;
    for(i=0;i<m;i++){
      z=a[i].first;
      x=a[i].second.first;
      y=a[i].second.second;
      if(0){
      }else if(b[x]==-1&&b[y]==-1){
	b[x]=b[y]=i;
	sm+=z;
      }else if(b[x]==-1||b[y]==-1){
	b[x]=b[y]=max(b[x],b[y]);
	sm+=z;
      }else if(b[x]!=b[y]){
	for(j=0;j<n;j++){
	  if(j!=y&&b[j]==b[y])
	    b[j]=b[x];
	}
	b[y]=b[x];
	sm+=z;
      }
    }
    cout<<sm<<endl;
  }
  return 0;
}