object Main extends App {
  import scala.io.StdIn._
  val Array(n, q) = readLine.trim.split(' ').map(_.toInt)
  val barrier = Array.tabulate(n){_ ⇒
    val Array(x, y, z, r, l) = readLine.trim.split(' ').map(_.toLong)
    (Point(x, y, z), r, l)
  }
  for (_ ← 0 until q) {
    val Array(sx, sy, sz, dx, dy, dz) = readLine.trim.split(' ').map(_.toInt)
    val from = Point(sx, sy, sz)
    val to = Point(dx, dy, dz)
    val vec = to - from match {case v ⇒ v / v.length}
    //println(vec)
    println(barrier.map{
      case (center, radius, cost) ⇒
        val nearest = vec * vec.dot(center - from) + from
        if ((from - nearest).dot(to - nearest) <= 0 && (center - nearest).length <= radius + 0.0000001) cost else 0L
    }.sum)
  }
}
case class Point(x: Double, y: Double, z: Double) {
  def -(that: Point): Vector = Vector(x - that.x, y - that.y, z - that.z)
  def +(that: Vector): Point = Point(x + that.x, y + that.y, z + that.z)
}
case class Vector(x: Double, y: Double, z: Double) {
  def dot(that: Vector): Double = x * that.x + y * that.y + z * that.z
  def +(that: Point): Point = Point(x + that.x, y + that.y, z + that.z)
  def length: Double = Math.sqrt(x * x + y * y + z * z)
  def /(that: Double): Vector = Vector(x / that, y / that, z / that)
  def *(that: Double): Vector = Vector(x * that, y * that, z * that)
  def toPoint: Point = Point(x, y, z)
}

