#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <cstring>
#include <functional>
#include <cmath>
#include <complex>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define rep1(i,n) for(int i=1;i<=(n);++i)
#define all(c) (c).begin(),(c).end()
#define fs first
#define sc second
#define pb push_back
#define show(x) cout << #x << " " << x << endl
typedef long double D;
typedef complex<D> P;
typedef pair<P,P> L;
typedef pair<P,bool> Q;
D eps=1e-11;
D cro(P a,P b){return imag(conj(a)*b);}
bool eq(D a,D b){return abs(a-b)<eps;}
bool comp(const Q& xx,const Q& yy){
	P x=xx.fs,y=yy.fs;
	if(abs(x.real()-y.real())<eps){
		return x.imag()<y.imag();
	}else{
		return x.real()<y.real();
	}
}
int ccw(P a,P b,P c){
	if(cro(b-a,c-a)>eps) return 1;
	if(cro(b-a,c-a)<-eps) return -1;
	if(eq(abs(a-c)+abs(c-b),abs(a-b))) return 0;
	if(eq(abs(a-b)+abs(b-c),abs(a-c))) return -2;
	return 2;
}
bool iSS(L a,L b){
	return ccw(a.fs,a.sc,b.fs)*ccw(a.fs,a.sc,b.sc)<=0&&ccw(b.fs,b.sc,a.fs)*ccw(b.fs,b.sc,a.sc)<=0;
}
P intLL(L a,L b){
	D t=cro(a.sc-a.fs,a.sc-b.fs)/cro(a.sc-a.fs,b.sc-b.fs);
	return b.fs+t*(b.sc-b.fs);
}
int main(){
	int T;
	cin>>T;
	rep(tt,T){
		vector<Q> vc;
		D ax,ay,bx,by;
		int N;
		cin>>ax>>ay>>bx>>by>>N;
		L l=L(P(ax,ay),P(bx,by));
		rep(i,N){
			D a,b,c,d;
			bool e,f;
			cin>>a>>b>>c>>d>>e>>f;
			L s=L(P(a,b),P(c,d));
			if(iSS(l,s)){
				vc.pb(Q(intLL(l,s),e^f));
			}
		}
		if(vc.size()==0){
			puts("0");
			continue;
		}
		sort(all(vc),comp);
		int ans=0;
		bool now=vc[0].sc;
		rep(i,vc.size()){
			if(now^vc[i].sc){
				ans++;
				now=vc[i].sc;
			}
		}
		cout<<ans<<endl;
	}
}