#include <iostream>
#include <cmath>
#include <complex>
#include <vector>
#include <assert.h>
#include <queue>
using namespace std;

const double EPS = 1e-9;
const double INF = 1e12;
typedef complex<double> P;
namespace std {
  bool operator < (const P& a, const P& b) {
    return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
  }
}
double cross(const P& a, const P& b) {
  return imag(conj(a)*b);
}
double dot(const P& a, const P& b) {
  return real(conj(a)*b);
}

struct L : public vector<P> {
  L(){}
  L(const P &a, const P &b) {
    push_back(a); push_back(b);
  }
};

typedef vector<P> G;

struct C {
  P p; double r;
  C(){ }
  C(const P &p, double r) : p(p), r(r) { }
};

int ccw(P a, P b, P c) {
  b -= a; c -= a;
  if (cross(b, c) > 0)   return +1;       // counter clockwise
  if (cross(b, c) < 0)   return -1;       // clockwise
  if (dot(b, c) < 0)     return +2;       // c--a--b on line
  if (norm(b) < norm(c)) return -2;       // a--b--c on line
  return 0;
}

//直線x直線交差判定
bool intersectLL(const L &l, const L &m) {
  return abs(cross(l[1]-l[0], m[1]-m[0])) > EPS || // non-parallel
         abs(cross(l[1]-l[0], m[0]-l[0])) < EPS;   // same line
}

//直線x線分交差判定
bool intersectLS(const L &l, const L &s) {
  return cross(l[1]-l[0], s[0]-l[0])*       // s[0] is left of l
         cross(l[1]-l[0], s[1]-l[0]) < EPS; // s[1] is right of l
}

//直線x点交差判定
bool intersectLP(const L &l, const P &p) {
  return abs(cross(l[1]-p, l[0]-p)) < EPS;
}

//線分x線分交差判定
bool intersectSS(const L &s, const L &t) {
  return ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= 0 &&
         ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= 0;
}

//線分x点交差判定
bool intersectSP(const L &s, const P &p) {
  return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS; // triangle inequality
}

//点の直線への射影
P projection(const L &l, const P &p) {
  double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);
  return l[0] + t*(l[0]-l[1]);
}


//直線と点の距離
double distanceLP(const L &l, const P &p) {
  return abs(p - projection(l, p));
}

//直線と直線の距離
double distanceLL(const L &l, const L &m) {
  return intersectLL(l, m) ? 0 : distanceLP(l, m[0]);
}

//直線と線分の距離
double distanceLS(const L &l, const L &s) {
  if (intersectLS(l, s)) return 0;
  return min(distanceLP(l, s[0]), distanceLP(l, s[1]));
}

//線分と点の距離
double distanceSP(const L &s, const P &p) {
  const P r = projection(s, p);
  if (intersectSP(s, r)) return abs(r - p);
  return min(abs(s[0] - p), abs(s[1] - p));
}

//線分と線分の距離
double distanceSS(const L &s, const L &t) {
  if (intersectSS(s, t)) return 0;
  return min(min(distanceSP(s, t[0]), distanceSP(s, t[1])),
             min(distanceSP(t, s[0]), distanceSP(t, s[1])));
}

//直線と直線の交点
P crosspoint(const L &l, const L &m) {
  double A = cross(l[1] - l[0], m[1] - m[0]);
  double B = cross(l[1] - l[0], l[1] - m[0]);
  if (abs(A) < EPS && abs(B) < EPS) return m[0]; // same line
  if (abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!
  return m[0] + B / A * (m[1] - m[0]);
}

int n, xa, xb, ya, yb, N;
L newline;

class Line{
public:
	double distance;
	L vec;
	int owner;
	int pos;
	bool operator< (const Line &opp) const {
		return distance>opp.distance;
	}
};



main(){
	
	cin >> n;
	while(n--){
		Line t;
		priority_queue<Line> oldline;
		cin >> xa >> ya >> xb >> yb >> N;
		newline = L(P(xa,ya), P(xb,yb));
		for(int i=0;i<N;i++){
			cin >> xa >> ya >> xb >> yb >> t.owner >> t.pos;
			t.vec = L(P(xa,ya), P(xb,yb));
			if(intersectSS(newline, t.vec)){
				t.distance = norm(crosspoint(newline, t.vec) - newline[0]);
				oldline.push(t);
			}
		}
		int ans;
		int now=5;
		for(ans=0;!oldline.empty();){
			t = oldline.top();
			oldline.pop();
//			printf("%d %d %d\n", now, t.owner, t.pos);
			if(!t.owner) t.pos = !t.pos;
			if(now == 5){
				now = t.pos;
			}else{
				if(now != t.pos){
					now = !now;
					ans ++;
				}
			}
		}
		cout << ans << endl;
	}
}

Measured memory_bytes: 1236