#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    loop {
        let n = read::<usize>();
        if n == 0 {
            return;
        }
        let mut cells = vec![];
        for i in 0..n {
            let v = read_vec::<f64>();
            let (x, y, z, r) = (v[0], v[1], v[2], v[3]);
            cells.push((x, y, z, r));
        }
        let mut edges = vec![];
        for i in 0..n {
            let (x1, y1, z1, r1) = cells[i];
            for j in i + 1..n {
                let (x2, y2, z2, r2) = cells[j];
                let dist =
                    ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2)).sqrt();
                if dist <= r1 + r2 {
                    edges.push(Edge {
                        from: i,
                        to: j,
                        cost: 0.0,
                    });
                } else {
                    edges.push(Edge {
                        from: i,
                        to: j,
                        cost: dist - r1 - r2,
                    });
                }
            }
        }
        println!("{:.3}", kruskal(&edges, n));
    }
}

const INF: f64 = 100000000.0;

use std::cmp::Ordering;

#[derive(Debug, Clone)]
struct Edge {
    from: usize,
    to: usize,
    cost: f64,
}

impl PartialEq for Edge {
    fn eq(&self, other: &Edge) -> bool {
        self.cost == other.cost
    }
}

impl Eq for Edge {}

impl Ord for Edge {
    fn cmp(&self, other: &Self) -> Ordering {
        self.cost.partial_cmp(&other.cost).unwrap()
    }
}

impl PartialOrd for Edge {
    fn partial_cmp(&self, other: &Edge) -> Option<Ordering> {
        Some(self.cost.partial_cmp(&other.cost).unwrap())
    }
}

struct UnionFindTree {
    parent_or_size: Vec<isize>,
}

impl UnionFindTree {
    fn new(size: usize) -> UnionFindTree {
        UnionFindTree {
            parent_or_size: vec![-1; size],
        }
    }

    fn find(&self, index: usize) -> usize {
        let mut index = index;
        while self.parent_or_size[index] >= 0 {
            index = self.parent_or_size[index] as usize;
        }
        index
    }

    fn same(&self, x: usize, y: usize) -> bool {
        self.find(x) == self.find(y)
    }

    fn unite(&mut self, index0: usize, index1: usize) -> bool {
        let a = self.find(index0);
        let b = self.find(index1);
        if a == b {
            false
        } else {
            if self.parent_or_size[a] < self.parent_or_size[b] {
                self.parent_or_size[a] += self.parent_or_size[b];
                self.parent_or_size[b] = a as isize;
            } else {
                self.parent_or_size[b] += self.parent_or_size[a];
                self.parent_or_size[a] = b as isize;
            }
            true
        }
    }
}

fn kruskal(edges: &Vec<Edge>, num_apexes: usize) -> f64 {
    let mut edges = edges.clone();
    let mut res = 0.0;
    edges.sort();
    let mut unf = UnionFindTree::new(num_apexes);
    for e in edges {
        if !unf.same(e.to, e.from) {
            unf.unite(e.to, e.from);
            res += e.cost;
        }
    }
    res
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}



Measured memory_bytes: 5092