#include <iostream>
#include <vector>
#include <queue>
#include <functional>
using namespace std;

class UnionFind {
public:
	UnionFind(size_t n) : parent(n, -1) {}

	void init(size_t n)
	{
		parent.assign(n, -1);
	}

	int root(int x)
	{
		int x_parent = parent[x];
		if (x_parent < 0)
		{
			return x;
		} else
		{
			return parent[x] = root(x_parent);
		}
	}

	bool unite(int x, int y)
	{
		int x_parent = root(x);
		int y_parent = root(y);
		if (x_parent == y_parent)
		{
			return false;
		} else if (x_parent < y_parent)
		{
			parent[x_parent] += parent[y_parent];
			parent[y_parent] = x_parent;
			return true;
		} else
		{
			parent[y_parent] += parent[x_parent];
			parent[x_parent] = y_parent;
			return true;
		}
	}

	bool same(int x, int y)
	{
		return root(x) == root(y);
	}

	int size(int x)
	{
		return -parent[root(x)];
	}

private:
	// negative(parent): num of members
	// positive(child) : parent index
	std::vector<int> parent;
};

struct Edge
{
	int a;
	int b;
	int cost;

	bool operator<(const Edge& rhs) const
	{
		return cost > rhs.cost;
	}
};

int main()
{
	int n, m;
	int ans_sum;
	UnionFind uf(500);
	priority_queue<Edge> que;

	while (true)
	{
		cin >> n >> m;
		if (n == 0)
			return 0;

		ans_sum = 0;
		uf.init(m);
		que = priority_queue<Edge>();

		for (int i = 0; i < m; ++i)
		{
			int a, b, cost;
			cin >> a >> b >> cost;
			que.push(Edge{ a, b, cost });
		}

		while (!que.empty())
		{
			Edge e = que.top(); que.pop();
			if (uf.unite(e.a, e.b))
			{
				ans_sum += e.cost;
			}
		}
		cout << ans_sum << endl;
	}
}