#include <stdio.h>
#include <math.h>
#include <string.h>

double max(double x, double y){
	if(x > y)
		return x;
	else
		return y;
}
double min(double x, double y){
	if(x < y)
		return x;
	else
		return y;
}

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

double dist(double x,double y,double z){
	return sqrt(x * x + y * y + z * z);
}

int main(void){
	int n, i, j, f[100], v;
	struct sphere s[100];
	double mincost[100], ans;
	while(1){
		scanf("%d",&n);
		if(!n) break;
		for(i = 0;i < n;i++)
			scanf("%lf%lf%lf%lf",&s[i].x,&s[i].y,&s[i].z,&s[i].r);
		for(i = 0;i < n;i++)
			mincost[i] = 0x3FFFFFFF;
		mincost[0] = 0;
		ans = 0;
		memset(f,0,sizeof(f));
		while(1){
			v = -1;
			for(i = 0;i < n;i++)
				if(!f[i] && (v == -1 || mincost[i] < mincost[v])) v = i;
			if(v == -1) break;
			ans += mincost[v];
			f[v] = 1;
			for(i = 0;i < n;i++)
				mincost[i] = min(mincost[i],(max(dist(s[v].x - s[i].x,s[v].y - s[i].y,s[v].z - s[i].z) - (s[v].r + s[i].r),0)));
		}
		printf("%.3f\n",ans);
	}
	return 0;
}