#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cmath>
//#include <cfenv>
//#define _GNU_SOURCE 1
#define square(x) ((x)*(x))
using namespace std;
const double INF = 1000.0;

/*static void attribute((constructor)) trapfpe()
  {
  feenableexcept(FE_INVALID|FE_DIVBYZERO|FE_OVERFLOW);
  }
 */ //何かの痕跡
int n,a;
double dis[100][101];
double dis2[100][101];
double fdis[100][101];
double touch[100][101];
double geo[100][5];
double mincost[101];
bool used[101];
int par[101];
int rank[101];
//struct edge{ int to,from,cost; };
vector<int> set[100],set2[100],fset[100];

void init(int n);
int find(int x);
void unite(int x,int y);
bool same(int x,int y);
double prim();


int main()
{
        while(cin >> n,n){
                init(n);

                for(int i=0;i<n;i++){
                        //cin >> (double)geo[i][0] >> (double)geo[i][1] >> (double)geo[i][2] >> (double)geo[i][3]; cout << square(geo[i][3]) << endl;
                        scanf("%lf %lf %lf %lf",&geo[i][0],&geo[i][1],&geo[i][2],&geo[i][3]);
                }
                for(int i=0;i<n;i++){
                        for(int j=0;j<n;j++){
                                if(i==j)continue;
                                dis[i][j]  = (double)sqrt((double)square(geo[i][0]-geo[j][0]) + (double)square(geo[i][1]-geo[j][1]) + (double)square(geo[i][2]-geo[j][2]));
                                touch[i][j] = geo[i][3]+geo[j][3];
                                dis2[i][j] = dis[i][j] - touch[i][j]; //cout <<dis2[i][j] << endl;
                                if(touch[i][j]>dis[i][j]) unite(i,j);
                        }
                }
                int p;
                for(int i=0;i<n;i++){
                        p = find(i);
                        set[p].push_back(i);
                }
                a = 0;
                for(int i=0;i<n;i++){
                        if(set[i].empty()) continue;
                        set2[a] = set[i];
                        a++;
                }

                for(int i=0;i<a;i++){
                        for(int j=0;j<a;j++){
                                if(i==j)continue;
                                double dmin = INF;
                                for(int k=0;k<set2[i].size();k++){
                                        for(int l=0;l<set2[j].size();l++){
                                                dmin = min(dmin,dis2[set2[i][k]][set2[j][l]]);
                                        }
                                }
                                fdis[i][j] = dmin; //cout<< dmin << endl;
                                dmin = INF;
                        }
                }

                printf("%.3f\n",prim());

                for(int i=0;i<n;i++) set[i].clear();
                for(int i=0;i<a;i++) set2[i].clear();

        }

}

void init(int n)
{
        for(int i=0;i<n;i++){
                par[i] = i;
                rank[i] = 0;
        }
}

int find(int x)
{
        if(par[x]==x){
                return x;
        }else{
                return par[x] = find(par[x]);
        }
}

void unite(int x,int y)
{
        x = find(x);
        y = find(y);
        if(x==y) return;

        if(rank[x]<rank[y]){
                par[x] = y;
        }else{
                par[y] = x;
                if(rank[x]==rank[y])rank[x]++;
        }
}

bool same(int x,int y)
{
        return find(x)==find(y);
}

double prim()
{
        for(int i=0;i<a;i++){
                mincost[i] = INF;
                used[i] = false;
        }

        mincost[0] = 0;
        double res = 0.0;

        while(true){
                int v = -1;
                for(int i=0;i<a;i++){
                        if(!used[i] && (v==-1 || mincost[i]<mincost[v])) v = i;
                }

                if(v == -1) break;
                used[v] = true;
                res += mincost[v]; //cout << mincost[v] << ' ' << res << endl;

                for(int i=0;i<a;i++){
                        mincost[i] = min(mincost[i],fdis[v][i]);
                }
        }
        return res;
}

Measured memory_bytes: 1636