#include <iostream>
#include <utility>
#include <string>
#include <algorithm>
#include <set>
#include <deque>
#include <vector>
#define MAX 25000
#define rep(i,p) for(int i=0;i<p;i++)
#define rep1(i,p) for(int i=1;i<=p;i++)
#define pb push_back
using namespace std;
int n,m,sum,num;
int A[MAX];
int B[MAX];
int C[MAX];
int V[MAX];
pair<int,int> bridge[MAX];
int root (int child) {
  int a = child;
  while (V[a] != a) {
    a = V[a];
  }
  return a;
}
int main(){
  while (1) {
    cin >> n >> m;
    if (n==0 && m==0) return 0;
    // n : city
    // m : bridge
    rep(j,m) {
      cin >> A[j] >> B[j] >> C[j];
      bridge[j].first = C[j];
      bridge[j].second = j;
    }
    // sort by lenght of bridge
    sort(bridge, bridge+m);
    sum = 0;
    // Union Find Tree
    rep(i,m) V[i] = i;
    rep(i,m) {
      num = bridge[i].second;
      int ra = root(A[num]);
      int rb = root(B[num]);
      if (ra != rb) {
        V[ra] = rb;
        sum += bridge[i].first;
      }
      // cout << bridge[i].first << " " << bridge[i].second << endl;
    }
    cout << sum << endl;
  }
  return 0;
}