#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
using P=pair<double,double>;
P operator+(P a,P b){return make_pair(a.first+b.first,a.second+b.second);}
P operator-(P a,P b){return make_pair(a.first-b.first,a.second-b.second);}
P operator*(P a,double t){return make_pair(a.first*t,a.first*t);}
double cross(P a,P b){return a.first*b.second-a.second*b.first;}
int ccw(P a,P b,P c){return cross(b-a,c-a)>0?1:-1;}
bool intersect(pair<P,P>a,pair<P,P>b){
	return ccw(a.first,a.second,b.first)*ccw(a.first,a.second,b.second)<0
		&&ccw(b.first,b.second,a.first)*ccw(b.first,b.second,a.second)<0;
}
double get(pair<P,P>a,pair<P,P>b){
	P base=b.second-b.first;
	double d1=abs(cross(base,a.first-b.first));
	double d2=abs(cross(base,a.second-b.first));
	return d1/(d1+d2);
}
int n,t;
int main()
{
	cin>>t;
	for(;t--;)
	{
		pair<P,P>a;
		cin>>a.first.first>>a.first.second>>a.second.first>>a.second.second>>n;
		vector<pair<double,bool> >Q;
		for(;n--;)
		{
			pair<P,P>b;
			cin>>b.first.first>>b.first.second>>b.second.first>>b.second.second;
			int o,l;cin>>o>>l;
			if(intersect(a,b))
			{
				Q.push_back(make_pair(get(a,b),o==l));
			}
		}
		sort(Q.begin(),Q.end());
		int cnt=0;
		for(int i=1;i<Q.size();i++)if(Q[i-1].second!=Q[i].second)cnt++;
		cout<<cnt<<endl;
	}
}

