using static System.Math;
using static System.Console;
using System.Collections.Generic;
using System.Linq;
using System;


class Program
{
    #region Reader

    static string ReadStr => Console.ReadLine();
    static string[] ReadStrs => Console.ReadLine().Split(' ');
    static int ReadInt => Convert.ToInt32(Console.ReadLine());
    static int[] ReadInts => Console.ReadLine().Split(' ').Select(s => Convert.ToInt32(s)).ToArray();
    static long ReadLong => Convert.ToInt64(Console.ReadLine());
    static long[] ReadLongs => Console.ReadLine().Split(' ').Select(s => Convert.ToInt64(s)).ToArray();
    static long[,] ReLongMatrix(int r, int c)
    {
        var mat = new long[r, c];
        for (int i = 0; i < r; ++i)
        {
            var x = ReadLongs;
            for (int j = 0; j < c; ++j)
            {
                mat[i, j] = x[j];
            }
        }
        return mat;
    }
    static Tuple<string, string>[] ReTupleStr(int n)
    {
        List<Tuple<string, string>> list = new List<Tuple<string, string>>();
        for (int i = 0; i < n; ++i)
        {
            var ab = ReadStrs;
            list.Add(new Tuple<string, string>(ab[0], ab[1]));
        }
        return list.ToArray();
    }
    static Tuple<long, long>[] ReTupleLong(int n)
    {
        List<Tuple<long, long>> list = new List<Tuple<long, long>>();
        for (int i = 0; i < n; ++i)
        {
            var ab = ReadLongs;
            list.Add(new Tuple<long, long>(ab[0], ab[1]));
        }
        return list.ToArray();
    }
    static Tuple<int, int>[] ReadTupleInt(int n)
    {
        List<Tuple<int, int>> list = new List<Tuple<int, int>>();
        for (int i = 0; i < n; ++i)
        {
            var ab = ReadInts;
            list.Add(new Tuple<int, int>(ab[0], ab[1]));
        }
        return list.ToArray();
    }
    static Tuple<double, double>[] ReTupleDouble(int n)
    {
        List<Tuple<double, double>> list = new List<Tuple<double, double>>();
        for (int i = 0; i < n; ++i)
        {
            var ab = ReadLongs;
            list.Add(new Tuple<double, double>(ab[0], ab[1]));
        }
        return list.ToArray();
    }
    static Dictionary<int, HashSet<int>> ReAdjacencyList(int N, int M)
    {
        Dictionary<int, HashSet<int>> dict = new Dictionary<int, HashSet<int>>();
        for (int i = 0; i <= N; ++i)
        {
            dict.Add(i, new HashSet<int>());
        }
        for (int i = 0; i < M; ++i)
        {
            var ab = ReadInts;
            dict[ab[0]].Add(ab[1]);
            dict[ab[1]].Add(ab[0]);
        }
        return dict;
    }
    #endregion
    static void Swap<T>(ref T a, ref T b)
    {
        T temp = a;
        a = b;
        b = temp;
    }
    static int mod = (int)Pow(10, 9) + 7;


    public class UnionFind
    {
        public UnionFind(int number)
        {
            Data = new List<int>();
            for (int i = 0; i < number; i++) Data.Add(-1);
        }

        /// <summary>
        /// 負なら親(絶対値はサイズ)　正なら子(絶対値は親のID)
        /// </summary>
        public List<int> Data { get; }

        public int Find(int node)
        {
            //自身が親なら、そのまま返す
            if (Data[node] < 0) return node;

            //子なら、その親を呼び、さらにその親を探す。再帰的に呼び出しrootを見つけ、全ての子の親をrootにする
            return Data[node] = Find(Data[node]);
        }

        public bool Unite(int node1, int node2)
        {
            //二つの点の親をさがす
            node1 = Find(node1);
            node2 = Find(node2);

            //二つの親が一致しているなら、することはない
            if (node1 == node2) return false;

            int parent = 0;
            int newSon = 0;

            //サイズが小さいほうが、大きい方の子になる(親のDataの値は負であることに注意)
            if (Data[node1] > Data[node2])
            {
                parent = node2;
                newSon = node1;
            }
            else
            {
                parent = node1;
                newSon = node2;
            }

            Data[parent] += Data[newSon];
            Data[newSon] = parent;

            return true;
        }
    }




    static void Main()
    {
        List<string> ans = new List<string>();
        while (true)
        {
            int N = ReadInt;
            if (N == 0) break;
            UnionFind tree = new UnionFind(N);

            double[][] nodes = new double[N][];
            List<Root> roots = new List<Root>();

            for (int i = 0; i < N; i++)
            {
                nodes[i] = Console.ReadLine().Split(' ').Select(s => double.Parse(s)).ToArray();
            }

            for (int i = 0; i < N; i++)
            {
                for (int j = i + 1; j < N; j++)
                {
                    double powerD = Pow(nodes[i][0] - nodes[j][0], 2) + Pow(nodes[i][1] - nodes[j][1], 2) + Pow(nodes[i][2] - nodes[j][2], 2);
                    roots.Add(new Root(i, j, Max(0, Sqrt(powerD) - (nodes[i][3] + nodes[j][3]))));
                }
            }

            roots.Sort((x, y) => (int)((x.cost - y.cost) * 10000));


            int count = 0;
            double cost = 0;
            for (int i = 0; i < roots.Count; i++)
            {
                if (tree.Find(roots[i].a) != tree.Find(roots[i].b))
                {
                    tree.Unite(roots[i].a, roots[i].b);
                    count++;
                    cost += roots[i].cost;
                    if (count == N - 1) break;
                }
            }

            ans.Add(cost.ToString("f3"));
        }
        WriteLine(string.Join("\n", ans));
    }

    public class Root
    {
        public Root(int a,int b,double cost)
        {
            this.a = a;
            this.b = b;
            this.cost = cost;
        }

        public int a;
        public int b;
        public double cost;
    }

}



Measured memory_bytes: 26776