#include <iostream>
#include<bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef double ld;
typedef long long ll;
typedef pair<int,int> P;
ld n,A[100],B[100],C[100],R[100],ra[101],fa[101],ans;
vector<pair<ld,P>> v;

ll find(ll x){
    if(x==fa[x])return x;
    return fa[x]=find(fa[x]);
}
void uni(ll x,ll y){
    x=find(x),y=find(y);
    if(x==y)return ;
    if(ra[x]<ra[y])swap(x,y);
    fa[y]=x;
    if(ra[x]==ra[y])ra[x]++;
    return ;
}

int main(void){
    while(1){
    cin>>n;
    if(n==0)return 0;
    v={};
    ans=0;
    for(int i=0;i<101;i++)fa[i]=i;
    for(int i=0;i<n;i++){
        cin>>A[i]>>B[i]>>C[i]>>R[i];
    }
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            ld L=(A[i]-A[j])*(A[i]-A[j])+(B[i]-B[j])*(B[i]-B[j])+(C[i]-C[j])*(C[i]-C[j]);
            L=pow(L,0.5);
            L-=R[i]+R[j];
            if(L<0)L=0;
            v.push_back({L,{i,j}});
        }
    }
    sort(v.begin(),v.end());
    for(auto x:v){
        if(find(x.S.F)!=find(x.S.S))ans+=x.F;
        uni(x.S.F,x.S.S);
    }
    //cout<<ans<<endl;
    //cout<<setprecision(10)<<ans<<endl;
    printf("%.3f\n",ans);
    }
}

