#include <iostream>
#include <algorithm>
using namespace std;

struct Dset {
  Dset *parent;
  int rank;

  Dset() {
    clear();
  }

  void clear() {
    parent = NULL;
    rank = 0;
  }

  Dset *find() {
    if (!parent) {
      return this;
    } else {
      parent = parent->find();
      return parent;
    }
  }

  bool merge(Dset *a) {
    Dset *myRoot = find();
    Dset *yourRoot = a->find();
    if (myRoot->rank < yourRoot->rank) {
      myRoot->parent = yourRoot;
    } else if (myRoot->rank > yourRoot->rank) {
      yourRoot->parent = myRoot;
    } else if (myRoot != yourRoot) {
      yourRoot->parent = myRoot;
      myRoot->rank++;
    } else {
      return false;
    }
    return true;
  }
};

struct Bridge {
  int a;
  int b;
  int cost;
  
  bool operator<(const Bridge &obj) const {
    return (cost < obj.cost);
  }
};

int main() {
  int n, m, ans;
  Bridge bridge[6000];
  Dset x[100];
  while (1) {
    cin >> n >> m;
    if (!n && !m) break;
    for (int i=0; i<m; i++) {
      cin >> bridge[i].a >> bridge[i].b >> bridge[i].cost;
    }
    sort(bridge, bridge+m);
    for (int i=0; i<m; i++) {
      x[i].clear();
    }
    ans = 0;
    for (int i=0; i<m; i++) {
      if (x[bridge[i].a].merge(&x[bridge[i].b])) {
        ans += bridge[i].cost;
      }
    }
    cout << ans << endl;
  }

  return 0;
}