// Aizu 1127: Building a Space Station
// 2017.9.19 bal4u@uu

#include <stdio.h>
#include <math.h>

#define EPS 1e-7
#define INF 1e7
typedef struct { double x, y, z, r; char f; } PP;
PP pp[105];

int main()
{
    int i, j, n, cnt, nx;
	double ans, min, d;
	PP  *p, *q;

    while (scanf("%d", &n) && n) {
		for (i = 0; i < n; i++) 
			scanf("%lf%lf%lf%lf", &pp[i].x, &pp[i].y, &pp[i].z, &pp[i].r), pp[i].f = 0;
        ans = 0, pp[0].f = 1, cnt = 1;
        while (cnt < n) {
            for (nx = -1, min = INF, p = pp, i = 0; i < n; i++, p++) {
                if (p->f == 0) continue;
                for (q = pp, j = 0; j < n; j++, q++) {
                    if (q->f) continue;
                    d = sqrt((p->x-q->x)*(p->x-q->x) + (p->y-q->y)*(p->y-q->y) +
						(p->z-q->z)*(p->z-q->z)) - (p->r+q->r);
					if (nx < 0 || d + EPS < min) min = d, nx = j;
                }
            }
            pp[nx].f = 1, cnt++; if (min > EPS) ans += min;
        }
        printf("%.3lf\n", ans);
    }
    return 0;
}