#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define MAX_N 200
#define INF 999999999


int edge[MAX_N][MAX_N];

int prim(int n){    //n : ???????????°
    bool used[MAX_N];
    int mincost[MAX_N];
    
    rep(i,n){
        mincost[i]=INF;
        used[i]=false;
    }
    mincost[0]=0;
    int res=0;
    
    while(1){
        int v=-1;
        rep(u,n){
            if(used[u]==false && ( v==-1 || mincost[u]<mincost[v] ))
                v=u;
        }
        if(v==-1)break;
        used[v]=true;
        res+=mincost[v];
        
        rep(u,n){
            mincost[u]=min(mincost[u],edge[v][u]);
        }
    }
    return res;
}

int main(){
    int n,m;
    while(cin>>n>>m){
        if(n==0&&m==0)break;
        
        rep(i,200)rep(j,200)edge[i][j]=INF;
        rep(i,m){
            int a,b,c;
            cin>>a>>b>>c;
            edge[a][b]=edge[b][a]=c;
        }
        cout<<prim(n)<<endl;
    }
}