/*
最小生成树 + 几何判断
Kruskal 球心之间的距离 - 两个球的半径 < 0 则说明是覆盖的!此时的距离按照0计算
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int f[];
struct ball{
double x, y, z, r;
};
struct connect{
double dist;
int a, b;
};
connect c[];
ball b[];
bool cmp(connect a, connect b){
return a.dist < b.dist;
}
int n;
int getFather(int x){
return x==f[x] ? x : f[x]=getFather(f[x]);
}
int Union(int a, int b){
int fa=getFather(a), fb=getFather(b);
if(fa!=fb){
f[fa]=fb;
return ;
}
return ;
}
int main(){
int i, j;
while(scanf("%d", &n) && n){
for(i=; i<=n; ++i)
scanf("%lf%lf%lf%lf", &b[i].x, &b[i].y, &b[i].z, &b[i].r);
int cnt=;
for(i=; i<n; ++i)
for(j=i+; j<=n; ++j){
double d = sqrt((b[i].x-b[j].x)*(b[i].x-b[j].x) + (b[i].y-b[j].y)*(b[i].y-b[j].y) + (b[i].z-b[j].z)*(b[i].z-b[j].z))
- (b[i].r + b[j].r);
c[cnt].dist= d< ? : d;
c[cnt].a=i;
c[cnt++].b=j;
}
sort(c, c+cnt, cmp);
double minSum=0.0;
for(i=; i<=n; ++i)
f[i]=i;
for(i=; i<cnt; ++i){
if(Union(c[i].a, c[i].b))
minSum+=c[i].dist;
}
printf("%.3lf\n", minSum);
}
return ;
}