#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
#define EPS 1e-10
using namespace std;

int testc,n;

double add(double a,double b){
	if(fabs(a+b)<EPS*(fabs(a)+fabs(b)))return 0;
	return a+b;
}

struct P{
	double x,y;
	int flag;
	P(){}
	P(double xx,double yy){
		x=xx;
		y=yy;
		flag=-1;
	}
	P(double xx,double yy,int f){
		x=xx;
		y=yy;
		flag=f;
	}
	P operator +(P p){
		return P(x+p.x,y+p.y);
	}
	P operator -(P p){
		return P(x-p.x,y-p.y);
	}
	P operator *(double d){
		return P(x*d,y*d);
	}
	P operator /(double d){
		return P(x/d,y/d);
	}
	double dot(P p){
		return add(x*p.x,y*p.y);
	}
	double det(P p){
		return add(x*p.y,-y*p.x);
	}
};

bool on_seg(P p1,P p2,P q){
	return (p1-q).det(p2-q)==0 && (p1-q).dot(p2-q)<=0;
}

bool comp(const P& p1,const P& p2){
	if(add(p1.x,-p2.x)==0)return p1.y<p2.y;
	return p1.x<p2.x;
}

P intersection(P p1,P p2,P q1,P q2){
	return p1+(p2-p1)*((q2-q1).det(q1-p1)/(q2-q1).det(p2-p1));
}

vector<P> data[2][2][2];
P a,b;
vector<P> cross;
int solve(){
	cross.clear();
	for(int i=0;i<2;i++){
		for(int j=0;j<2;j++){
			for(int k=0;k<data[i][j][0].size();k++){
				P p1=data[i][j][0][k],p2=data[i][j][1][k];
				if((p1-p2).det(a-b)==0)continue;
				P r=intersection(p1,p2,a,b);
				if(on_seg(p1,p2,r) && on_seg(a,b,r)){
					if(i==0){
						if(j==0)r.flag=0;
						else r.flag=1;
					}else{
						if(j==0)r.flag=1;
						else r.flag=0;
					}
					cross.push_back(r);
				}
			}
		}
	}
	int now=-1,cnt=0;
	sort(cross.begin(),cross.end(),comp);
	for(int i=0;i<cross.size();i++){
		P p=cross[i];
		if(now==-1){
			now=cross[i].flag;
		}else{
			if(now!=cross[i].flag)cnt++;
			now=cross[i].flag;
		}
	}
	return cnt;
}

int main(void){
	scanf("%d",&testc);
	for(int ddd=0;ddd<testc;ddd++){
		scanf("%lf %lf %lf %lf",&a.x,&a.y,&b.x,&b.y);
		scanf("%d",&n);
		for(int i=0;i<2;i++){
			for(int j=0;j<2;j++){
				for(int k=0;k<2;k++)data[i][j][k].clear();
			}
		}
		for(int i=0;i<n;i++){
			int xs,ys,xt,yt,o,l;
			scanf("%d %d %d %d %d %d",&xs,&ys,&xt,&yt,&o,&l);
			data[o][l][0].push_back(P(xs,ys));
			data[o][l][1].push_back(P(xt,yt));
		}
		printf("%d\n",solve());
	}
	return 0;
}