#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int MAX_V = 1000;

int cost[MAX_V][MAX_V];
int mincost[MAX_V];
bool used[MAX_V];

int V;
const int INF = 10000000;

int prim(){
  for(int i = 0; i < V; i++){
    mincost[i] = INF;
    used[i] = false;
  }
  mincost[0] = 0;
  int res = 0;
  while(1){
    int v = -1;
    // Xツづ可堕ョツつウツづ按つ「ツ陳クツ点ツづ個つ、ツつソXツつゥツづァツづ個陛督づ個コツスツトツつェツ催渉ャツづ可づ按づゥツ点ツづーツ探ツつキ
    for(int u = 0; u < V; u++){
      if(!used[u] && (v == -1 || mincost[u] < mincost[v]))
		  v = u;
	}
	if(v == -1)
		break;
	used[v] = true;
	res += mincost[v];
	
	for(int u = 0; u < V; u++){
		mincost[u] = min(mincost[u],cost[v][u]);
	}

  }
  return res;
}

int main(){

	int n,m;
	while(cin>>n>>m&&!(n==0&&m==0)){
		for(int i = 0; i < MAX_V; i++){
			fill(cost[i],cost[i]+MAX_V,INF);
		}
		V=n;
		int a,b,c;
		for(int i = 0; i < m; i++){
			cin>>a>>b>>c;
			cost[a][b]=c;
			cost[b][a]=c;
		}
		cout<<prim()<<endl;
	}

	return 0;
}