#include <bits/stdc++.h>

class Solve {
private:
	using vd = std::vector<double>;
	using vvd = std::vector<vd>;

	double square(double x) { return x * x; }

public:
	bool is_last_query{};
	Solve()
	{
		int n;
		scanf("%d", &n);
		if (n == 0)
		{
			is_last_query = true;
			return;
		}
		vvd edge(n, vd(n));
		std::vector<double> x(n), y(n), z(n), r(n);
		for (int i{}; i < n; i++)
			scanf("%lf%lf%lf%lf", &x[i], &y[i], &z[i], &r[i]);
		for (int i{}; i < n; i++)
		{
			for (int j{}; j < n; j++)
			{
				double distance{sqrt(square(x[i] - x[j]) + square(y[i] - y[j]) + square(z[i] - z[j]))};
				edge[i][j] = std::max(0.0, distance - r[i] - r[j]);
			}
		}

		std::vector<bool> used(n);
		used[0] = true;
		std::vector<double> distance(n);
		distance = edge[0];
		double ans{};
		for (int size{1}; size < n; size++)
		{
			int min_i{};
			double min{1e9};
			for (int i{}; i < n; i++)
				if (!used[i] && distance[i] < min)
				{
					min_i = i;
					min = distance[i];
				}

			ans += min;
			used[min_i] = true;
			for (int i{}; i < n; i++)
				distance[i] = std::min(distance[i], edge[min_i][i]);
		}
		printf("%.3lf\n", ans);
	}
};

int main()
{
	while (!Solve().is_last_query);

	return 0;
}
