#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cmath>
#include <cassert>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <complex>
#include <list>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <utility>
#include <functional>
#include <iterator>

using namespace std;

#define dump(n) cerr<<"# "<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define iter(c) __typeof__((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();++i)
#define all(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int,int> pii;

struct Edge{
	int src,dst,weight;
	Edge(){}
	Edge(int s,int d,int w):src(s),dst(d),weight(w){}
	bool operator<(const Edge& e)const{return weight<e.weight;}
	bool operator>(const Edge& e)const{return weight>e.weight;}
};
typedef vector<vector<Edge> > Graph;

int Prim(const Graph& g,vector<Edge>& tree,int root=0)
{
	tree.clear();
	
	vi vis(g.size());
	priority_queue<Edge,vector<Edge>,greater<Edge> > pq;
	pq.push(Edge(-1,root,0));
	int res=0;
	while(pq.size()){
		Edge cur=pq.top(); pq.pop();
		if(vis[cur.dst])
			continue;
		vis[cur.dst]=true;
		res+=cur.weight;
		if(cur.src!=-1)
			tree.push_back(cur);
		foreach(e,g[cur.dst])
			pq.push(*e);
	}
	return res;
}

int main()
{
	for(int n,m;scanf("%d%d",&n,&m),n|m;){
		Graph g(n);
		rep(i,m){
			int s,d,w; scanf("%d%d%d",&s,&d,&w);
			g[s].push_back(Edge(s,d,w));
			g[d].push_back(Edge(d,s,w));
		}
		
		vector<Edge> tree;
		int res=Prim(g,tree);
		printf("%d\n",res);
	}
	
	return 0;
}