/* 
   AOJ 1127
   Building a Space Station
*/

#include<stdio.h>
#include<math.h>
#define MAX_CELL 100
#define INF 1000000

typedef struct cell
{
  double x, y, z, r;
}cell;

cell cells[MAX_CELL];
double cost[MAX_CELL][MAX_CELL];
double mincost[MAX_CELL];
int used[MAX_CELL];


double square(double x)
{
  return x * x;
}

double calc_cost(int a, int b)
{
  double dis = sqrt(square(cells[a].x - cells[b].x) + square(cells[a].y - cells[b].y) + square(cells[a].z - cells[b].z));
  double r_dis = dis - (cells[a].r + cells[b].r);
  
  if(r_dis > 0)
    return r_dis;
  else
    0.0;
}

void make_edge(int n)
{
  int i, j;

  for(i = 0; i < n; i++)
    for(j = 0; j < n; j++)
      cost[i][j] = calc_cost(i, j);
}

double min(double a, double b)
{
  if(a < b)
    return a;
  else
    return b;
}

double prim(int n)
{
  int i;
  double res = 0.0;
  for(i = 0; i < n; i++)
    {
      mincost[i] = INF;
      used[i] = 0;
    }

  mincost[0] = 0;

  while(1)
    {
      int v = -1, u;
      for(u = 0; u < n; u++)
	if(!used[u] && (v == -1 || mincost[u] < mincost[v]))
	  v = u;
       
      if (v == -1) break;
      used[v] = 1;
      res += mincost[v];
      for(u = 0; u < n; u++)
	mincost[u] = min(mincost[u], cost[v][u]);
    }
  return res;
}

int main(void)
{
  int n, j, i;

  while(scanf("%d", &n), n != 0)
    {
      for(i = 0; i < n; i++)
	scanf("%lf %lf %lf %lf", &cells[i].x, &cells[i].y, &cells[i].z, &cells[i].r);

      make_edge(n);

      printf("%.3lf\n", prim(n));
    }
  
  return 0;
  
}