#include <iostream>
#include <cmath>
#define COST_MAX 1000000.0
using namespace std;

struct Cell{
  double x, y, z, r;
};

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

    Cell c[n];
    for(int i = 0; i < n; i++){
      cin >> c[i].x >> c[i].y >> c[i].z >> c[i].r;
    }

    bool visited[n];
    double cost[n];
    for(int i = 0; i < n; i++) {
      visited[i] = false;
      cost[i] = COST_MAX;
    }
    cost[0] = 0.0;
    while(1) {
      double min = COST_MAX;
      int p;
      for(int i = 0; i < n; i++) {
	if(!visited[i] && cost[i] < min) {
	  min = cost[i];
	  p = i;
	}
      }
      if(min == COST_MAX) break;

      visited[p] = true;
      for(int i = 0; i < n; i++) {
	if(i == p || visited[i]) continue;
	double dist = sqrt(pow(c[p].x-c[i].x, 2)+pow(c[p].y-c[i].y, 2)
			   +pow(c[p].z-c[i].z, 2))-(c[p].r+c[i].r);
	if(dist < cost[i]) {
	  cost[i] = dist;
	}
      }
    }
    double ans = 0.0;
    for(int i = 0; i < n; i++) {
      if(cost[i] > 0.0) {
	ans += cost[i];
      }
    }
    printf("%.3f\n", ans);
  }
  return 0;
}