#include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) begin(a),end(a)
#define MS(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(3)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 114514810;
const int MOD = 1000000007;
const double EPS = 1e-8;
const double PI = acos(-1.0);
struct edge
{
	int from, to;
	double cost;
	bool operator < (const edge& e) const { return cost < e.cost; }
	bool operator >(const edge& e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

struct a
{
	double x, y, z, r;
};
vector<a> v;
int par[105];
vector<edge> es;

int find(int x)
{
	if (par[x] == x) return x;
	else return par[x] = find(par[x]);
}

void unite(int x, int y)
{
	x = find(x); y = find(y);
	if (x == y) return;
	par[y] = x;
}

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

void init_UFT(int n)
{
	REP(i, n)
	{
		par[i] = i;
	}
}



int main()
{
	int n;
	while (cin >> n, n)
	{
		v.clear();
		REP(i, n)
		{
			double x, y, z, r;
			cin >> x >> y >> z >> r;
			v.push_back(a{ x, y, z, r });
		}
		es.clear();
		init_UFT(n);
		REP(i, n)FOR(j, i + 1, n)
		{
			double dif = sqrt(pow((v[i].x - v[j].x), 2) + pow((v[i].y - v[j].y), 2) + pow((v[i].z - v[j].z), 2)) - v[i].r - v[j].r;
			if (dif > 0)
			{
				es.push_back(edge{ i, j, dif });
			}
			else
			{
				unite(i, j);
			}
		}

		sort(ALL(es));
		double sum = 0;
		REP(i, es.size())
		{
			if (!same(par[es[i].from], par[es[i].to]))
			{
				unite(es[i].from, es[i].to);
				sum += es[i].cost;
			}
		}
		cout << D10 << sum << endl;
	}
	return 0;
}