#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <cassert>
#include <queue>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1<<30;
const int MOD = 1e9 + 7;
struct edge
{
    int to, cost;
    edge(int to, int cost) { this->to = to, this->cost = cost; }
};
int n, m;
int MST(vector<edge> G[])
{
    priority_queue<P, vector<P>, greater<P>> que;
    vector<int> used(n, 0);
    que.push(make_pair(0, 0));
    int res = 0;
    while(que.size())
    {
        P p = que.top(); que.pop();
        int v = p.second;
        if(used[v]) continue;
        used[v] = 1;
        res += p.first;
        for(int u = 0; u < G[v].size(); u++)
        {
            edge e = G[v][u];
            que.push(make_pair(e.cost, e.to));
        }
    }
    return res;
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(cin >> n >> m, n | m)
    {
        vector<edge> G[n];
        for(int i = 0; i < m; i++)
        {
            int a, b, c; cin >> a >> b >> c;
            G[a].push_back(edge(b, c));
            G[b].push_back(edge(a, c));
        }
        cout << MST(G) << endl;
    }
    return 0;
}


