#include<bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
#define pi M_PI
#define inf 1<<30
#define eps (1e-11)
#define equals(a,b) (fabs((a)-(b))<eps)
using namespace std;

class Point{
public:
  double x,y,z;
  Point(double x=0,double y=0,double z=0):x(x),y(y),z(z){}

  Point operator+(Point p){ return Point(x+p.x,y+p.y,z+p.z);}
  Point operator-(Point p){ return Point(x-p.x,y-p.y,z-p.z);}
  Point operator*(double k){ return Point(x*k,y*k,z*k);}
  Point operator/(double k){ return Point(x/k,y/k,z/k);}
  bool operator<(Point p)const{ return (x!=p.x ? x<p.x : (y!=p.y ? y<p.y : z<p.z));}
  bool operator==(Point p)const{ 
    return (fabs(x-p.x)<eps && fabs(y-p.y)<eps &&  fabs(z-p.z)<eps);}
  
  double abs(){ return sqrt(norm());}
  double norm(){ return (x*x+y*y+z*z);}
};
typedef Point Vector;
typedef vector<Point> Polygon;

class Segment{
public:
  Point p1,p2;
  Segment(Point p1,Point p2):p1(p1),p2(p2){}
};
typedef Segment Line;

class sphere{
public:
  Point c;
  double r;
  sphere(Point c=Point(),double r=0.0):c(c),r(r){}
};

double norm(Vector a){ return (a.x*a.x+a.y*a.y+a.z*a.z);}
double abs(Vector a){ return sqrt(norm(a));}
double dot(Vector a,Vector b){ return (a.x*b.x+a.y*b.y+a.z*b.z);}
Vector cross(Vector a,Vector b){ 
  Vector res;
  res.x=(a.y*b.z)-(a.z*b.y);
  res.y=(a.z*b.x)-(a.x*b.z);
  res.z=(a.x*b.y)-(a.y*b.x);
  return res;
}

double getDistanceLP(Line l,Point p){
  return abs(cross(l.p2-l.p1,p-l.p1))/abs(l.p2-l.p1);
}

double getDistanceSP(Segment s,Point p){
  if(dot(s.p2-s.p1,p-s.p1)<0.0)return abs(p-s.p1);
  if(dot(s.p1-s.p2,p-s.p2)<0.0)return abs(p-s.p2);
  return getDistanceLP(s,p);
}

int main()
{
  int n,q;
  double a,b,c,x,y,z,r,m;
  vector<pair<sphere,long long> > vs;

  cin>>n>>q;
  for(int i=0;i<n;i++){
    cin>>a>>b>>c>>r>>m;
    vs.push_back(mp(sphere(Point(a,b,c),r),m));
  }

  for(int i=0;i<q;i++){
    cin>>a>>b>>c>>x>>y>>z;
    Segment A(Point(a,b,c),Point(x,y,z));
    long long ans=0;
    for(int j=0;j<n;j++){
      // printf("%.10f %.10f\n",getDistanceSP(A,vs[j].f.c),vs[j].f.r);
      if((getDistanceSP(A,vs[j].f.c)-vs[j].f.r)<eps)ans+=vs[j].s;
    }
    cout<<ans<<endl;
  }
  return 0;
}