#include <iostream>
#include <algorithm>
#define REP(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,a) REP(i,0,a)
using namespace std;

int par[101]; // parent
int ranks[101]; // depth of tree

void init(int n) {
    rep(i, n) {
        par[i] = i;
        ranks[i] = 0;
    }
}

int find(int x) {
    if (par[x] == x) {
        return x;
    }
    else {
        return par[x] = find(par[x]);
    }
}

void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    
    if (ranks[x] < ranks[y]) {
        par[x] = y;
    }
    else {
        par[y] = x;
        if (ranks[x] == ranks[y]) ranks[x]++;
    }
}

bool same(int x, int y) {
    return find(x) == find(y);
}

struct edge {int u; int v; int cost; };

bool comp(const edge& e1, const edge& e2) {
    return e1.cost < e2.cost;
}

// Entry point
int main(void){
    while (1) {
        int N,M;
        cin >> N >> M;
        if (N == 0) break;
        
        edge es[M];
        rep(i,M) cin >> es[i].u >> es[i].v >> es[i].cost;
        sort(es, es+M, comp);
        
        init(N);
        int ans = 0;
        rep(i,M) {
            edge e = es[i];
            if (!same(e.u, e.v)) {
                unite(e.u, e.v);
                ans += e.cost;
            }
        }
        cout << ans << endl;
    }
    return 0;
}