### main

loop do
  n, qn = gets.split.map(&:to_i)
  break if (n | qn) == 0

  wrps = n.times.map{gets.split.map(&:to_f)}
  #p wrps

  qn.times do
    mi = gets.to_i

    pts = mi.times.map{gets.split.map(&:to_f)}
    #p pts

    swrps = []
    for pt in pts
      min_d = Float::INFINITY
      for k in (0...n)
        dx = wrps[k][0] - pt[0]
        dy = wrps[k][1] - pt[1]
        dz = wrps[k][2] - pt[2]
        d = Math.sqrt(dx * dx + dy * dy + dz * dz)
        min_d = d if min_d > d
      end
      swrps << min_d
    end
    #p swrps

    sum_d = 0.0
    for i in (0...(mi - 1))
      j = i + 1
      dxij = pts[j][0] - pts[i][0]
      dyij = pts[j][1] - pts[i][1]
      dzij = pts[j][2] - pts[i][2]
      dij = Math.sqrt(dxij * dxij + dyij * dyij + dzij * dzij)
      dwij = swrps[i] + swrps[j]

      sum_d += (dij < dwij) ? dij : dwij
    end

    puts "%.9f" % sum_d
  end
end