function Main(input) {
    let l = 0;
    input = input.trim().split("\n").map(function(x) { return x.split(" ")});
    let n = parseInt(input[l][0],10);
    while(n !== 0){
        
        let arr_xyzr = [];
        for (let i = 0; i < n; i++){
            arr_xyzr.push(input[l + 1 + i].map(e => parseFloat(e, 10)));
        }

        // console.log(arr_xyzr);

        // let lVl = parseInt(input[0][0], 10);
        // let lEl = parseInt(input[0][1], 10);

        let graph = [];
        for (let i = 0; i < n - 1; i++){
            let tmp =[];
            tmp[0] = i;
            for (let j = i + 1; j < n; j++){
                tmp[1] = j;
                tmp[2] = Math.max(0, Math.sqrt(Math.pow((arr_xyzr[j][0] - arr_xyzr[i][0]), 2) + Math.pow((arr_xyzr[j][1] - arr_xyzr[i][1]), 2) + Math.pow((arr_xyzr[j][2] - arr_xyzr[i][2]), 2)) - arr_xyzr[i][3] - arr_xyzr[j][3]);
                graph.push(tmp.slice());
            }
        }
        graph.sort((a, b) => a[2] - b [2]);
        // console.log(graph);
        let uf = new UnionFind(n);
        let ans = 0;
        for (let i = 0; i < graph.length; i++){
            if (uf.find(graph[i][0]) !== uf.find(graph[i][1])) {
                uf.union(graph[i][0], graph[i][1])
                ans += graph[i][2];
            }
        }
        console.log(ans.toFixed(3));

        l += n + 1;
        n = parseInt(input[l][0],10);
    }

}

function UnionFind (n) {
    this.parents = [];
    for (let i = 0; i < n; i++) {
        this.parents.push(-1);
    }
}

UnionFind.prototype.find = function (x) {
    if (this.parents[x] < 0) return x;
    return this.parents[x] = this.find(this.parents[x]);    
}

UnionFind.prototype.union = function (x, y) {
    x = this.find(x);
    y = this.find(y);
    if (x === y) return;
    if (this.parents[x] > this.parents[y]) [x, y] = [y, x];
    
    this.parents[x] += this.parents[y];
    this.parents[y] = x;
}

UnionFind.prototype.size = function (x) {
    x = this.find(x);
    while (x >= 0) {
        x = this.parents[x];
    }
    return x * -1;
}

Main(require("fs").readFileSync("/dev/stdin", "utf8"));

