#include<bits/stdc++.h>
using namespace std;
#define EPS (1e-10)
static const int COUNTER_CLOCKWISE = 1;
static const int CLOCKWISE = -1;
static const int ONLINE_BACK = 2;
static const int ONLINE_FRONT = -2;
static const int ON_SEGMENT = 0;

struct Point{
  double x,y;
  Point(){}
  Point(double x,double y):x(x),y(y){}
  Point operator+(Point p){return Point(x+p.x,y+p.y);}
  Point operator-(Point p){return Point(x-p.x,y-p.y);}
  Point operator*(double k){return Point(x*k,y*k);}
  Point operator/(double k){return Point(x/k,y/k);}

};

struct Segment{
  Point p1,p2;
  int o,l;
  Point c;
  double x;
  bool operator<(const Segment &s)const{
    return x < s.x;
  }
};

typedef Point Vector;

double norm(Vector a){
  return a.x*a.x+a.y*a.y;
}

double abs(Vector a){
  return sqrt(norm(a));
}


double dot(Vector a,Vector b){
  return a.x*b.y+a.y*b.x;
}

double cross(Vector a,Vector b){
  return a.x*b.y-a.y*b.x;
}

int ccw(Point p0, Point p1, Point p2){
  Vector a = p1-p0;
  Vector b = p2-p0;
  if(cross(a,b) > EPS ) return COUNTER_CLOCKWISE;
  if(cross(a,b) < -EPS ) return CLOCKWISE;
  if(dot(a,b) < -EPS ) return ONLINE_BACK;
  if(norm(a) < norm(b) ) return ONLINE_FRONT;
  
  return ON_SEGMENT;
}

bool intersect(Point p1,Point p2,Point p3,Point p4){
  return (ccw(p1,p2,p3) * ccw(p1,p2,p4) <= 0 &&
	  ccw(p3,p4,p1) * ccw(p3,p4,p2) <= 0 );
}

bool intersect(Segment s1, Segment s2){
  return intersect(s1.p1,s1.p2,s2.p1,s2.p2);
}

Point getCrossPoint(Segment s1,Segment s2){
  Vector base = s2.p2 - s2.p1;
  double d1 = abs(cross(base,s1.p1-s2.p1));
  double d2 = abs(cross(base,s1.p2-s2.p1));
  double t = d1 / (d1+d2);
  return s1.p1 + (s1.p2-s1.p1) * t;
}

int main(){
  int t;cin >> t;
  while(t--){
    Segment r;
    cin >> r.p1.x >> r.p1.y >> r.p2.x >> r.p2.y;
    int n;cin>>n;
    int i,j,k;
    vector<Segment> vs;vs.clear();
    Segment seg;
    for(i=0;i<n;i++){
      cin >> seg.p1.x >> seg.p1.y >> seg.p2.x >> seg.p2.y >> seg.o >> seg.l;
      if(intersect(r,seg)) {
	seg.x = abs(getCrossPoint(r,seg)-r.p1);
	vs.push_back(seg);
      }
    }
    sort(vs.begin(),vs.end());
    int num = vs.size();
    //cout << num << endl;
    //for(i=0;i<num;i++) cout << vs[i].x << endl;

    int inf = 1 << 20;
    int o = inf;
    
    int dp[105][2]={{}};
    fill(dp[0],dp[105],inf);
    
    dp[0][0]=0;dp[0][1]=0;

    for(i=0;i<num;i++){
      if(vs[i].o==1&&vs[i].l==0){
	dp[i+1][0] = min(dp[i][0],dp[i][1]+1);
      }else if(vs[i].o==1&&vs[i].l==1){
	dp[i+1][1] = min(dp[i][0]+1,dp[i][1]);
      }else if(vs[i].o==0&&vs[i].l==0){
	dp[i+1][1] = min(dp[i][0]+1,dp[i][1]);
      }else if(vs[i].o==0&&vs[i].l==1){
	dp[i+1][0] = min(dp[i][0],dp[i][1]+1);
      }
      
    }
    o=min(dp[num][0],dp[num][1]);
    cout << o << endl;
  }
  return 0;
}