#include <iostream>
#include <map>
using namespace std;

#define rep(i,x) for(int i = 0 ; i < x ; i ++)
#define rep1(i,x) for(int i = 1 ; i <= x ; i ++)

typedef long double ld;

struct po{
	ld x;
	ld y;
	po(ld a){ x = a; y = 0; }
	po(){}
	inline ld operator()(const po& rhs) const {
		return x*rhs.x+y*rhs.y;
	}
	inline po& operator+=(const po& rhs){
		x += rhs.x;
		y += rhs.y;
		return *this;
	}
	inline po& operator-=(const po& rhs){
		x -= rhs.x;
		y -= rhs.y;
		return *this;
	}
	inline po& operator*=(const po& rhs){
		ld x_ = x*rhs.x-y*rhs.y;
		y = x*rhs.y+y*rhs.x;
		x = x_;
		return *this;
	}
	inline po& operator/=(const po& rhs){
		ld x_ = x*rhs.x+y*rhs.y;
		y = -x*rhs.y+y*rhs.x;
		x = x_;
		ld nor = rhs(rhs);
		x /= nor;
		y /= nor;
		return *this;
	}
	inline void in(){
		scanf("%Lf%Lf",&x,&y);
	}
};
po operator+(const po& a, const po& b){ return po(a) += b; }
po operator-(const po& a, const po& b){ return po(a) -= b; }
po operator*(const po& a, const po& b){ return po(a) *= b; }
po operator/(const po& a, const po& b){ return po(a) /= b; }

inline po similar(const po& a,const po& b,const po& c,const po& p,const po& q){
	return p+(q-p)*(c-a)/(b-a);
}

struct li{
	ld a;
	ld b;
	ld c;
	li(po p,po q){
		a = q.y-p.y;
		b = p.x-q.x;
		c = p.y*q.x-p.x*q.y;
	}
	li(){}
	inline ld operator()(const po& p) const {
		return a*p.x+b*p.y+c;
	}
};

void intersect(const li &l,const li &m,po* &ret){
	ret = new po;
	ret->x = -(l.c*m.b-m.c*l.b)/(l.a*m.b-m.a*l.b);
	ret->y = -(l.a*m.c-m.a*l.c)/(l.a*m.b-m.a*l.b);
}

typedef pair<po,po> se;
#define fr first
#define sc second

bool on(const po &p,const se &s){
	return (s.fr-p)(s.sc-p) <= 1e-9;
}

void intersect(se s,se t,po* &ret){
	li l = li(s.fr,s.sc);
	li m = li(t.fr,t.sc);
	po *p;
	intersect(l,m,p);
	if(on(*p,s)&&on(*p,t))ret = p;
	else ret = nullptr;
}

int main(){
	int T;
	scanf("%d",&T);
	rep(loop,T){
		po a,b;
		int n;
		po s[102],t[102];
		int o[102],l[102];
		a.in();
		b.in();
		scanf("%d",&n);
		rep1(i,n){
			s[i].in();
			t[i].in();
			scanf("%d%d",&o[i],&l[i]);
		}
		map<ld,int> M;
		rep1(i,n){
			po *p;
			intersect(se(a,b),se(s[i],t[i]),p);
			if(p == nullptr)continue;
			po q = similar(a,b,*p,0,1000000000.0);
			M[q.x] = o[i]^l[i];
		}
		if(M.size() == 1){
			printf("0\n");
			continue;
		}
		int ret = 0;
		int pre = M.begin()->sc;
		for(pair<ld,int> p: M){
			if(p.sc != pre){
				ret ++;
				pre = p.sc;
			}
		}
		printf("%d\n",ret);
	}
}


