#include "bits/stdc++.h"
using namespace std;
#define FOR(i,a,b) for(int (i)=(a);i<(int)(b);i++)
#define rep(i,n) FOR(i,0,n)

struct UnionFind {
    vector<int> p;
    UnionFind(int n) : p(n, -1) {};
    bool unite(int u, int v) {
        if ((u = root(u)) == (v = root(v))) return false;
        if (p[u] > p[v]) swap(u, v);
        p[u] += p[v]; p[v] = u;
        return true;
    }
    bool find(int u, int v) { return root(u) == root(v); }
    int root(int u) { return p[u] < 0 ? u : p[u] = root(p[u]); }
    int size(int u) { return -p[root(u)]; }
};

struct Edge {
    int src, dst, cost;
    Edge(int s, int d, int c) : src(s), dst(d), cost(c) {};
    bool operator<(const Edge& e) const {
        return cost < e.cost;
    }
};


int main() {
    int n, m;
    while (cin >> n >> m, n) {
        vector<Edge> es;
        rep(i, m) {
            int a, b, c; cin >> a >> b >> c;
            es.emplace_back(a, b, c);
        }
        sort(es.begin(), es.end());
        UnionFind uf(n);
        int tot = 0;
        for (auto&& e : es) {
            if (uf.unite(e.src, e.dst))
                tot += e.cost;
        }
        cout << tot << "\n";
    }
    return 0;
}
