### constants

N_DIV = 1000
B_GRD = 100
N_GRD = (B_GRD * 2 + 1)

### main

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

  grids = N_GRD.times.map{N_GRD.times.map{N_GRD.times.map{[]}}}

  n.times do
    x, y, z = gets.split.map(&:to_f)
    gx = (x / N_DIV).floor + B_GRD
    gy = (y / N_DIV).floor + B_GRD
    gz = (z / N_DIV).floor + B_GRD
    grids[gx][gy][gz] << [x, y, z]
  end

  qn.times do
    mi = gets.to_i

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

    swrps = []
    for pt in pts
      x, y, z = pt
      gx = (x / N_DIV).floor + B_GRD
      gy = (y / N_DIV).floor + B_GRD
      gz = (z / N_DIV).floor + B_GRD
      min_d = Float::INFINITY

      for gx0 in ((gx - 1)..(gx + 1))
        next if gx0 < 0 || gx0 >= N_GRD
        for gy0 in ((gy - 1)..(gy + 1))
          next if gy0 < 0 || gy0 >= N_GRD
          for gz0 in ((gz - 1)..(gz + 1))
            next if gz0 < 0 || gz0 >= N_GRD

            for wrp in grids[gx0][gy0][gz0]
              wx, wy, wz = wrp
              dx = wx - x
              dy = wy - y
              dz = wz - z
              d = Math.sqrt(dx * dx + dy * dy + dz * dz)
              min_d = d if min_d > d
            end
          end
        end
      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