#include <iostream>
#include <vector>
#include <utility>
#define INF 1000000
#define MAX_N 100
using namespace std;
int n,m;
int cost[MAX_N][MAX_N];
int mincost[MAX_N];
bool used[MAX_N];

void solve() {
  for(int i=0;i<n;i++) {
    mincost[i] = INF;
    used[i] = false;
  }
  mincost[0] = 0;
  int res = 0;
  while(true) {
    int v = -1;
    for(int i=0;i<n;i++) {
      if(!used[i] && (v == -1 || mincost[i] < mincost[v])) v = i;
    }
    if(v == -1) break;
    res += mincost[v];
    used[v] = true;

    for(int i=0;i<n;i++) {
      mincost[i] = min(mincost[i],cost[v][i]);
    }
  }
  cout << res << endl;
}

int main() {
  while(cin >> n >> m) {
    if(n==0&&m==0) break;
    for(int i=0;i<100;i++) {
      for(int j=0;j<100;j++) {
        cost[i][j] = INF;
      }
    }
    for(int i=0;i<m;i++) {
      int f,t,c;
      cin >> f >> t >> c;
      cost[f][t] = c;
      cost[t][f] = c;
    }
    solve();
  }
}