#include<iostream>
#include<cstdio>
#include<cmath>
#define INF 1<<29
#define MAX_V 200
using namespace std;
double cost[MAX_V][MAX_V];
double mincost[MAX_V];
bool used[MAX_V];
int V;
double prim(){
	for(int i=0;i<V;i++){
		mincost[i]=INF;
		used[i]=false;
	}
	mincost[0]=0;
	double res=0;
	while(true){
		int v=-1;
		for(int u=0;u<V;u++){
			if(!used[u]&&(v==-1||mincost[u]<mincost[v]))
			v=u;
		}
		if(v==-1)
		break;
		used[v]=true;
		res+=mincost[v];
		for(int u=0;u<V;u++){
			mincost[u]=min(mincost[u],cost[v][u]);
		}
	}
	return res;
}
int main(){
	while(cin>>V,V){
		double x[MAX_V],y[MAX_V],z[MAX_V],r[MAX_V];
		for(int i=0;i<V;i++)
			cin>>x[i]>>y[i]>>z[i]>>r[i];
		for(int i=0;i<V;i++)
		for(int j=0;j<V;j++){
			if(i==j)
			cost[i][j]=INF;
			else{
				cost[i][j]=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])+(z[i]-z[j])*(z[i]-z[j]))-(r[i]+r[j]);
				if(cost[i][j]<=0)
					cost[i][j]=0;
			}
		}
		printf("%.3lf\n",prim());
		
	}
	return 0;
}