#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; double w; };

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

double prim(int n, vector<edge> G[]) {
	vector<double> w(n, DBL_MAX); w[0] = 0;
	priority_queue< d_i, vector<d_i>, greater<d_i> > q;
	q.push(d_i(0, 0));
	double res = 0;
	while (!q.empty()) {
		d_i p = q.top(); q.pop();
		int u = p.second;
		if (p.first > w[u]) continue;
		res += w[u]; w[u] = -DBL_MAX;
		for (int i = 0; i < G[u].size(); i++) {
			edge e = G[u][i];
			if (w[e.v] > e.w) {
				w[e.v] = e.w;
				q.push(d_i(e.w, e.v));
			}
		}
	}
	return res;
}

vector<edge> G[100];

int main() {
	for (;;) {
		int n; cin >> n;
		if (n == 0) break;
		vector<double> x(n), y(n), z(n), r(n);
		for (int i = 0; i < n; i++)
			cin >> x[i] >> y[i] >> z[i] >> r[i];
		for (int i = 0; i < n; i++)
			G[i].clear();
		for (int i = 0; i < n; i++)
			for (int j = i + 1; j < n; j++) {
				double dx = x[i] - x[j], dy = y[i] - y[j], dz = z[i] - z[j];
				double l = max(0.0, sqrt(dx * dx + dy * dy + dz * dz) - (r[i] + r[j]));
				edge e = {i, j, l}, _e = {j, i, l};
				G[i].push_back(e); G[j].push_back(_e);
			}
		printf("%.3f\n", prim(n, G));
	}
}