#include <bits/stdc++.h>
using namespace std;

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
#define IINF INT_MAX
#define LLINF LLONG_MAX
#define DEBUG false

#define MAX_V 100
#define INF INT_MAX

int cost[MAX_V][MAX_V];
int mincost[MAX_V];
bool used[MAX_V];
int V;

int prim(){
    for(int i=0;i<V;++i){
        mincost[i]=INF;
        used[i]=false;
    }
    mincost[0]=0;
    int res=0;
    while(true){
        int v=-1;
        rep(u,V){
            if(!used[u] && (v==-1 || mincost[u] <mincost[v])) v=u;
        }
        if(v==-1) break;
        used[v]= true;
        res+=mincost[v];
        rep(u,V){
            mincost[u]=min(mincost[u],cost[v][u]);
        }
    }
    return res;
}
int main(){
    int n,m;
    while(cin>>n>>m){
        if(n==0&&m==0){
            break;
        }
        V=n;    
    rep(i,MAX_V){
        rep(j,MAX_V){
            cost[i][j]=INF;
        }
    }
    int a,b,c;
    rep(i,m){
        cin>>a>>b>>c;
        cost[a][b]=c;
        cost[b][a]=c;
    }
    cout<<prim()<<endl;
    }
    return 0;
}