#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
using namespace std;

#define rep(i,n) for(int (i)=0; (i)<(int)(n); ++(i))
#define foreach(c,i) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)

const int MAX_V = 102, INF = 1 << 30;
int cost[MAX_V][MAX_V];
int mincost[MAX_V];
bool used[MAX_V];
int V, E;

int prim() {
  for (int u = 0; u < V; ++u) {
    mincost[u] = INF;
    used[u] = 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 solve() {
  int a, b, c;
  rep (i,V) rep (j,V) cost[i][j] = INF;
  rep (i,E) {
    cin >> a >> b >> c;
    cost[a][b] = cost[b][a] = c;
  }
  return prim();
}

int main() {
  while (cin >> V >> E, V|E) {
    cout << solve() << endl;
  }
  return 0;
}