#include <bits/stdc++.h>

using namespace std;

#define EPS 1e-10
#define INF 1e9

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

bool eq(double a,double b) {
  return fabs(a-b)<EPS;
}

double f(double a) { return a*a; }

double dist(sphere a,sphere b) {
  double d=sqrt(f(a.x-b.x)+f(a.y-b.y)+f(a.z-b.z));
  if(d<=a.r+b.r) return 0;
  return d-(a.r+b.r);
}

template <typename T> 
T prim(vector<vector<pair<int, T> > > G) {
	T cost = 0;
	const int N = G.size();
	vector<bool> used(G.size());
	priority_queue<pair<T, int>, vector<pair<T, int> >, greater<pair<T, int> > > pq;
	pq.push(make_pair(0, 0));
	while (!pq.empty()) {
		pair<T, int> p = pq.top(); pq.pop();
		int now = p.second;
		if (used[now]) continue;
		used[now] = true;
		cost += p.first;
		for (auto e : G[now])
			pq.push(make_pair(e.second, e.first));
	}
	return cost;
}

int main() {
  int n;
  while(cin >> n,n) {
    vector<sphere> s(n);
    for(auto &a:s) cin >> a.x >> a.y >> a.z >> a.r;
    vector<vector<pair<int,double> > > G(n);
    for(int i=0;i<n;++i) {
      for(int j=i+1;j<n;++j) {
	double d=dist(s[i],s[j]);
	G[i].push_back(make_pair(j,d));
	G[j].push_back(make_pair(i,d));
      }
    }
    printf("%.3f\n",prim(G));
  }
  
  return 0;
}

