#include<iostream>
#include<vector>
#include<cfloat>
#include<cmath>
#include<set>
using namespace std;

#define NONE DBL_MAX

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

double dist(Sphere a, Sphere b)
{
  double d=sqrt( pow(a.x-b.x,2.0)+pow(a.y-b.y,2.0)+pow(a.z-b.z,2.0) );
  return a.r+b.r<d?d-a.r-b.r:0.0;
}

double prim(vector< vector<double> >v)
{
  double sum=0.0;
  int n=v.size();
  set<int>selected;
  set<int>::iterator it;
  double mincost;
  int mini;
  selected.insert(0);
  while(selected.size()!=n){
    mincost=DBL_MAX;
    for(it=selected.begin();it!=selected.end();it++){
      for(int i=0;i<n;i++){
        if(selected.find(i)!=selected.end())continue;
        if(v[*it][i]==NONE)continue;
        if(v[*it][i]>=mincost)continue;
        mincost=v[*it][i];
        mini=i;
       }
     }
    if(mini==NONE)break;
    sum+=mincost;
    selected.insert(mini);
  }
  return sum;
}

int main()
{
  int n;
  double x,y,z,r;
  while(cin>>n,n){
    vector<Sphere>list(n);
    vector< vector<double> >A(n,vector<double>(n,NONE));
    for(int i=0;i<n;i++)cin>>list[i].x>>list[i].y>>list[i].z>>list[i].r;
    for(int i=0;i<n;i++){
      for(int j=i+1;j<n;j++){
	A[i][j]=A[j][i]=dist(list[i],list[j]);
      }
    }
    printf("%.3f\n",prim(A));
  }
}