#include <iostream>
#define TOSHI_MAX 100
#define COST_MAX 10
using namespace std;

int prim(int data[TOSHI_MAX][TOSHI_MAX], int toshiNum) {
  bool visited[toshiNum];
  int cost[toshiNum];
  for(int i = 0; i < toshiNum; i++) {
    visited[i] = false;
    cost[i] = COST_MAX;
  }
  cost[0] = 0;
  while(1) {
    int min = COST_MAX;
    int p;
    for(int i = 0; i < toshiNum; i++) {
      if(!visited[i] && cost[i] < min) {
	min = cost[i];
	p = i;
      }
    }
    if(min == COST_MAX) break;

    visited[p] = true;
    for(int i = 0; i < toshiNum; i++) {
      if(!visited[i] && data[p][i] < cost[i]) {
	cost[i] = data[p][i];
      }
    }
  }

  int ans = 0;
  for(int i = 0; i < toshiNum; i++) {
    if(cost[i] != COST_MAX) ans += cost[i];
  }

  return ans;
}

int main() {
  while(1) {
    int n, m;
    cin >> n >> m;
    if(n == 0 && m == 0) break;

    int data[TOSHI_MAX][TOSHI_MAX];
    for(int i = 0; i < n; i++) {
      for(int j = 0; j < n; j++) {
	data[i][j] = COST_MAX;
      }
    }

    for(int i = 0; i < m; i++) {
      int a, b, cost;
      cin >> a >> b >> cost;
      data[a][b] = data[b][a] = cost;
    }

    cout << prim(data, n) << endl;
  }

  return 0;
}