#include<bits/stdc++.h>
using namespace std;
#define EPS 0.000000001
#define equals(a,b) (fabs((a)-(b)) < EPS)
class Point {
public:
  double x, y;
  Point (double x = 0,double y = 0): 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 a) { return Point(a*x,a*y); }
  Point operator / (double a) { return Point(x/a,y/a); }
  double abs() { return sqrt(norm()); }
  double norm() { return x*x+y*y; }
  bool operator < (const Point &p) const {
    return x != p.x ? x < p.x : y < p.y;
  }
  bool operator == (const Point &p) const {
    return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;
  }
};
typedef Point Vector;
struct Segment { 
  Point p1, p2;
  int o,l;
};
typedef Segment Line;

struct CrossPoint {
  Point p1;
  int o,l;
  bool operator<( const CrossPoint& CP ) const {
    return p1.x == CP.p1.x ? p1.y < CP.p1.y : p1.x < CP.p1.x;
  }
};

Point MakePoint(int x,int y);
Segment MakeSegment(Point x,Point y);
int ccw(Point p1,Point p2,Point p3);
bool intersect(Point p1,Point p2,Point p3,Point p4);
bool intersect(Segment s1,Segment s2);
double dot(Vector a,Vector b);
double cross(Vector a,Vector b);
Point getCrossPoint(Segment s1,Segment s2);

Point MakePoint(int x,int y){
  Point a;
  a.x= x;
  a.y= y;
  return a;
}

Line MakeSegment(Point x,Point y){
  Line a;
  a.p1 = x;
  a.p2 = y;
  return a;
}

CrossPoint MakeCrossPoint(Point p,int o,int l){
  CrossPoint a;
  a.p1 = p; a.o = o; a.l = l;
  return a;
}

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;

/* 反時計回り CCW */
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(a.norm() < b.norm()) return ONLINE_FRONT;

  return ON_SEGMENT;
}

/* 線分p1,p2と線分p3,p4の交差判定 */
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);
}

/* ベクトルaとbの内積 */
double dot(Vector a,Vector b){
  return a.x*b.x + a.y*b.y;
}

/* ベクトルaとbの外積 */
double cross(Vector a,Vector b){
  return a.x*b.y - a.y*b.x;
}

/* 線分s1と線分s2の交点 */
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 data;
  cin >> data;
  while(data--){
    int xa,ya,xb,yb,n;
    cin >> xa >> ya >> xb >> yb;
    Point s,t;
    s = MakePoint(xa,ya);
    t = MakePoint(xb,yb);
    Segment M = MakeSegment(s,t);
    Segment seg[101];
    cin >> n;
    int c=0;
    CrossPoint crosspoint[101];
    for(int i=0;i<n;i++){
      Point a,b;
      int o,l;
      cin >> a.x >> a.y >> b.x >> b.y >> o >> l;
      seg[i].p1 = a; seg[i].p2 = b; seg[i].o = o; seg[i].l = l;
      bool flag = intersect(M,seg[i]);
      if(flag){
	Point p = getCrossPoint(M,seg[i]);
	crosspoint[c] = MakeCrossPoint(p,seg[i].o,seg[i].l); 
	c++;
      }
    }
    sort(crosspoint,crosspoint+c);
    int ans = 100000000;
    for(int z=0;z<2;z++){
      int now = z;
      int cou = 0;
      for(int i=0;i<c;i++){
	if(crosspoint[i].o == 1){
	  if(crosspoint[i].l != now){
	    cou++;
	    now = crosspoint[i].l;
	  }
	}
	else {
	  if(crosspoint[i].l == now){
	    cou++;
	    if(now == 1) now = 0;
	    else now = 1;
	  }
	}
      }
      ans = min(ans,cou);
    }
    cout << ans << endl;
  }
}
  

Measured memory_bytes: 1236