#![allow(non_snake_case)]
#![allow(dead_code)]
#![allow(unused_macros)]
#![allow(unused_imports)]

use std::str::FromStr;
use std::io::*;
use std::collections::*;
use std::cmp::*;

struct Scanner<I: Iterator<Item = char>> {
    iter: std::iter::Peekable<I>,
}

macro_rules! exit {
    () => {{
        exit!(0)
    }};
    ($code:expr) => {{
        if cfg!(local) {
            writeln!(std::io::stderr(), "===== Terminated =====")
                .expect("failed printing to stderr");
        }
        std::process::exit($code);
    }}
}

impl<I: Iterator<Item = char>> Scanner<I> {
    pub fn new(iter: I) -> Scanner<I> {
        Scanner {
            iter: iter.peekable(),
        }
    }

    pub fn safe_get_token(&mut self) -> Option<String> {
        let token = self.iter
            .by_ref()
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>();
        if token.is_empty() {
            None
        } else {
            Some(token)
        }
    }

    pub fn token(&mut self) -> String {
        self.safe_get_token().unwrap_or_else(|| exit!())
    }

    pub fn get<T: FromStr>(&mut self) -> T {
        self.token().parse::<T>().unwrap_or_else(|_| exit!())
    }

    pub fn vec<T: FromStr>(&mut self, len: usize) -> Vec<T> {
        (0..len).map(|_| self.get()).collect()
    }

    pub fn mat<T: FromStr>(&mut self, row: usize, col: usize) -> Vec<Vec<T>> {
        (0..row).map(|_| self.vec(col)).collect()
    }

    pub fn char(&mut self) -> char {
        self.iter.next().unwrap_or_else(|| exit!())
    }

    pub fn chars(&mut self) -> Vec<char> {
        self.get::<String>().chars().collect()
    }

    pub fn mat_chars(&mut self, row: usize) -> Vec<Vec<char>> {
        (0..row).map(|_| self.chars()).collect()
    }

    pub fn line(&mut self) -> String {
        if self.peek().is_some() {
            self.iter
                .by_ref()
                .take_while(|&c| !(c == '\n' || c == '\r'))
                .collect::<String>()
        } else {
            exit!();
        }
    }

    pub fn peek(&mut self) -> Option<&char> {
        self.iter.peek()
    }
}

use std::cmp::Ordering;

#[derive(Eq, PartialEq, Clone, Debug)]
pub struct Rev<T>(pub T);

impl<T: PartialOrd> PartialOrd for Rev<T> {
    fn partial_cmp(&self, other: &Rev<T>) -> Option<Ordering> {
        other.0.partial_cmp(&self.0)
    }
}

impl<T: Ord> Ord for Rev<T> {
    fn cmp(&self, other: &Rev<T>) -> Ordering {
        other.0.cmp(&self.0)
    }
}

#[derive(PartialEq)]
struct MinFloat(f64);

impl Eq for MinFloat {}

impl PartialOrd for MinFloat {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        other.0.partial_cmp(&self.0)
    }
}

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

use std::mem::swap;

struct UnionFind {
    parents: Vec<i64>,
}

impl UnionFind {
    fn new(n: usize) -> UnionFind {
        UnionFind { parents: vec![-1; n] }
    }
    fn unite(&mut self, x: usize, y: usize) {
        let mut px = self.root(x);
        let mut py = self.root(y);
        if px == py {
            return;
        }
        if self.parents[x] < self.parents[y] {
            swap(&mut px, &mut py);
        }
        self.parents[px] += self.parents[py];
        self.parents[py] = px as i64;
    }
    fn root(&mut self, x: usize) -> usize {
        let px = self.parents[x];
        if px < 0 {
            return x;
        }
        self.parents[x] = self.root(px as usize) as i64;
        self.parents[x] as usize
    }
    fn same(&mut self, x: usize, y: usize) -> bool {
        self.root(x) == self.root(y)
    }
    fn size(&mut self, x: usize) -> usize {
        let px = self.root(x);
        -self.parents[px as usize] as usize
    }
}


fn main() {
    let cin = stdin();
    let cin = cin.lock();
    let mut sc = Scanner::new(cin.bytes().map(|c| c.unwrap() as char));
    loop {
        let N: usize = sc.get();
        if N == 0 {
            break;
        }
        let mut cell = Vec::new();
        for _ in 0..N {
            let x: f64 = sc.get();
            let y: f64 = sc.get();
            let z: f64 = sc.get();
            let r: f64 = sc.get();
            cell.push((x, y, z, r))
        }
        let mut bh = BinaryHeap::new();
        for i in 0..N-1 {
            for j in i+1..N {
                let (x1, y1, z1, r1) = cell[i];
                let (x2, y2, z2, r2) = cell[j];
                let d = ((x2 - x1).powf(2.) + (y2 - y1).powf(2.) + (z2 - z1).powf(2.)).sqrt(); 
                bh.push((min(MinFloat(0.), MinFloat(d - (r1 + r2))), i, j));
            }
        }
        let mut ans = 0.;
        let mut uf = UnionFind::new(N);
        while let Some((MinFloat(dist), i, j)) = bh.pop() {
            if !uf.same(i, j) {
                uf.unite(i, j);
                ans += dist;
            }
        }
        println!("{:.3}", ans);
    }
}



Measured memory_bytes: 5100