#include <bits/stdc++.h>

using namespace std;

typedef pair<double, int> P;
const int INF = numeric_limits<int>::max() / 2;

struct cell {
	double x, y, z, r;
};

double calc(cell p, cell q) {
	double tmp = sqrt((p.x - q.x)*(p.x - q.x) + (p.y - q.y)* (p.y - q.y) + (p.z - q.z)*(p.z - q.z));
	if (tmp <= p.r + q.r) return 0;
	else return (-p.r - q.r + tmp);
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	while (true) {
		int n; cin >> n;
		if (n == 0) break;
		vector<cell> cells;
		for (int i = 0; i < n; i++) {
			cell c;
			cin >> c.x >> c.y >> c.z >> c.r;
			cells.push_back(c);
		}
		vector<vector<double> > dis(n, vector<double>(n, INF));
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (i == j) dis[i][j] = 0;
				else {
					dis[i][j] = calc(cells[i], cells[j]);
				}
			}
		}
		vector<bool> used(n, false);
		priority_queue<P,vector<P>,greater<P> > que;
		double res = 0;
		que.push(P(0, 0));
		while (!que.empty()) {
			P p = que.top(); que.pop();
			if (used[p.second]) continue;
			used[p.second] = true;
			res += p.first;
			for (int i = 0; i < n; i++) {
				if (!used[i]) {
					que.push(P(dis[p.second][i], i));
				}
			}
		}
		cout << fixed<<setprecision(3)<<res << endl;
	}
}