#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;

double dist(double x1, double y1, double z1,
            double x2, double y2, double z2 ){
    return sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) + (z1-z2)*(z1-z2) );
}

const double INF = (double)(1<<30);
double G[100][100];
double mindist[100];
bool used[100];
double x[100], y[100], z[100], r[100];

int main(){
    int n, i, j;
    while(cin>>n,n){
        for(i = 0; i < n; i++) used[i] = false;
        for(i = 0; i < n; i++){
            cin >> x[i] >> y[i] >> z[i] >> r[i];
        }
        for(i = 0; i < n; i++){
            for(j = 0; j < n; j++){
                if( i == j ) G[i][j] = 0.0;
                double d = dist(x[i],y[i],z[i],x[j],y[j],z[j]);
                if( d <= r[i] + r[j] ){
                    G[i][j] = 0.0;
                    G[j][i] = 0.0;
                }else{
                    G[i][j] = d - r[i] - r[j];
                    G[j][i] = d - r[i] - r[j];
                }
            }
        }
        used[0] = true;
        double ans = 0.0;
        for(i = 0; i < n; i++){
            mindist[i] = G[0][i];
        }
        for(i = 0; i < n; i++){
            int id = -1;
            double m = INF;
            for(j = 0; j < n; j++){
                if( used[j] ) continue;
                if( mindist[j] < m ){
                    m = mindist[j];
                    id = j;
                }
            }
            if( id == -1 ) break;
            used[id] = true;
            ans += m;
            for(j = 0; j < n; j++){
                mindist[j] = min( mindist[j], G[id][j] );
            }
        }
        printf("%.3lf\n", ans);
    }
    return 0;
}