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

double x[100],y[100],z[100],r[100];

double dis(int i,int j){
  return sqrt( (x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j]) + (z[i]-z[j])*(z[i]-z[j]) ); 
}

int main(){
  int n;
  double d[100][100];
  int par[100];
  int tmp;
  int i,j,k,h,w;
  double min,ans;

  while(1){
    cin >> n;
    if(!n)break;

    for(i=0;i<n;i++){
      cin >> x[i] >> y[i] >> z[i] >> r[i];
      for(j=0;j<n;j++)d[i][j] = 0.0;
      par[i] = i;
    }

    for(i=0;i<n;i++){
      for(j=0;j<n;j++){
	double len = dis(i,j);
	if(len > r[i]+r[j])d[i][j] = len - (r[i]+r[j]);
	else{
	  tmp = par[j];
	  for(k=0;k<n;k++){
	    if(par[k] == tmp)par[k] = par[i];
	  }
	}
      }
    }

    ans = 0.0;
    while(1){
      for(i=1;i<n;i++){
	if(par[i]!=par[0])break;
      }
      if(i==n)break;

      min = 1000001.0;
      for(i=0;i<n;i++){
	for(j=0;j<n;j++){
	  if(d[i][j] > 0.0 && min > d[i][j] && par[i] != par[j]){
	    min = d[i][j];
	    h = i;
	    w = j;
	  }
	}
      }

      ans += min;
      tmp = par[w];
      for(i=0;i<n;i++){
	if(par[i] == tmp)par[i] = par[h];
      }
    }

    printf("%.3lf\n",ans);
  }
}