#include <iostream>
#include <cmath>
#include <cstdio>
#define INF 1000000000
using namespace std;
double mp[101][101];
bool flg[101];
double dis[101];
int n;

double prim() {
  double sum = 0;
  for(int i=0;i<=n;i++) flg[i] = 0,dis[i] = INF;
  dis[0] = 0;

  for(int q=0;q<n;q++) {
    int nx;
    double dmin = INF;
    for(int i=0;i<n;i++) 
      if(dmin > dis[i] && flg[i] == 0) dmin = dis[i],nx = i;
    flg[nx] = 1;
    sum+=dmin;
    for(int i=0;i<n;i++) 
      if(mp[nx][i] < dis[i]) dis[i] = mp[nx][i];
  }
  return sum;

}

int main() {


  while(1) {
    cin >> n;
    if(n == 0) break;

    for(int i=0;i<n;i++)
      for(int j=0;j<n;j++)
	mp[i][j] = INF;

    for(int i=0;i<n;i++) {
      double x[101],y[101],z[101],r[101];
      cin >> x[i] >> y[i] >> z[i] >> r[i];
      
      for(int j=0;j<i;j++) {
	mp[i][j] = 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]) )-r[i]-r[j];
	//	cout << mp[i][j] << endl;
	if(mp[i][j] < 0) mp[i][j] = 0;
	mp[j][i] = mp[i][j];
      }
    }

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

  return 0;
}