### subroutines

def dist(sp0, sp1)
  dx = sp1[0] - sp0[0]
  dy = sp1[1] - sp0[1]
  dz = sp1[2] - sp0[2]
  rr = sp1[3] + sp0[3]
  d = Math.sqrt(dx * dx + dy * dy + dz * dz) - rr
  (d < 0.0) ? 0.0 : d
end

def root(i)
  ri = i
  while ri != $uft[ri]
    ri = $uft[ri]
  end
  while ri != $uft[i]
    i0 = $uft[i]
    $uft[i] = ri
    i = i0
  end
  ri
end

def union(i, j)
  i = root(i)
  j = root(j)
  $uft[j] = i
end

### main

loop do
  n = gets.to_i
  break if n == 0

  sps = n.times.map{gets.split.map(&:to_f)}

  dists = []
  $uft = (0...n).to_a

  for i in (0...n)
    spi = sps[i]
    for j in ((i + 1)...n)
      spj = sps[j]
      d = dist(spi, spj)
      if d == 0.0
        union(i, j)
      else
        dists << [d, i, j]
      end
    end
  end

  dists.sort!
  #p $uft
  #p dists

  sumd = 0.0

  for dist in dists
    d, i, j = dist
    if root(i) != root(j)
      sumd += d
      union(i, j)
    end
  end

  puts "%.3f" % sumd
end