#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <complex>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <iterator>

using namespace std;

#define dump(n) cout<<"# "<<#n<<'='<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define iter(c) __typeof__((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();++i)
#define all(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int,int> pii;

const int INFTY=1<<29;
const double EPS=1e-9;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& a){
	os<<'[';
	rep(i,a.size()) os<<(i?" ":"")<<a[i];
	return os<<']';
}

struct Point{
	double x,y;
	Point(){}
	Point(double x,double y):x(x),y(y){}
};
struct Line{
	Point pos,dir;
	Line(){}
	Line(Point p,Point d):pos(p),dir(d){}
	Line(double x1,double y1,double x2,double y2):pos(x1,y1),dir(x2,y2){}
};
typedef Line Segment;
ostream& operator<<(ostream& os,const Point& p){ return os<<'('<<p.x<<','<<p.y<<')'; }
ostream& operator<<(ostream& os,const Line& l){ return os<<'('<<l.pos<<','<<l.dir<<')'; }

Point operator+(Point a,Point b){
	return Point(a.x+b.x,a.y+b.y);
}
Point operator-(Point a,Point b){
	return Point(a.x-b.x,a.y-b.y);
}
Point operator*(double c,Point p){
	return Point(c*p.x,c*p.y);
}

int Signum(double x){
	return abs(x)<EPS?0:x<0?-1:1;
}
double Abs2(Point p){
	return p.x*p.x+p.y*p.y;
}
double Dot(Point a,Point b){
	return a.x*b.x+a.y*b.y;
}
double Cross(Point a,Point b){
	return a.x*b.y-a.y*b.x;
}
int CCW(Point a,Point b,Point c){
	Point d1=b-a,d2=c-a;
	if(int sign=Signum(Cross(d1,d2)))
		return sign; // 1:ccw,-1:cw
	if(Dot(d1,d2)<-EPS)
		return -2; // c-a-b
	if(Abs2(d1)<Abs2(d2)-EPS)
		return 2; // a-b-c
	return 0; // a-c-b
}
bool IsInterSS(Segment a,Segment b){
	int c1=CCW(a.pos,a.pos+a.dir,b.pos),c2=CCW(a.pos,a.pos+a.dir,b.pos+b.dir);
	int c3=CCW(b.pos,b.pos+b.dir,a.pos),c4=CCW(b.pos,b.pos+b.dir,a.pos+a.dir);
	return c1*c2<=0 && c3*c4<=0;
}
Point InterLL(Line a,Line b){
	if(abs(Cross(a.dir,b.dir))<EPS)
		return a.pos;
	return a.pos+Cross(b.pos-a.pos,b.dir)/Cross(a.dir,b.dir)*a.dir;
}

int main()
{
	int tc; cin>>tc;
	while(tc--){
		Line nl;
		vector<Line> ml,mu,yl,yu;
		{
			double x1,y1,x2,y2; cin>>x1>>y1>>x2>>y2;
			nl=Line(x1,y1,x2-x1,y2-y1);
			int n; cin>>n;
			int owner,layer; // owner=0/1:他社/自社, layer=0/1:地下/高架
			rep(i,n){
				cin>>x1>>y1>>x2>>y2>>owner>>layer;
				if(owner) (layer?mu:ml).emplace_back(x1,y1,x2-x1,y2-y1);
				else      (layer?yu:yl).emplace_back(x1,y1,x2-x1,y2-y1);
			}
		}
		
		vector<pair<Point,int>> p; // (座標,地下と高架のどちらにあるべきか)
		for(Line l:ml) if(IsInterSS(nl,l)) p.emplace_back(InterLL(nl,l),0);
		for(Line l:mu) if(IsInterSS(nl,l)) p.emplace_back(InterLL(nl,l),1);
		for(Line l:yl) if(IsInterSS(nl,l)) p.emplace_back(InterLL(nl,l),1);
		for(Line l:yu) if(IsInterSS(nl,l)) p.emplace_back(InterLL(nl,l),0);
		
		sort(all(p),[nl](pair<Point,int> a,pair<Point,int> b){
			return Abs2(a.first-nl.pos)<Abs2(b.first-nl.pos);
		});
		
		int res=0;
		rep(i,p.size()-1) res+=p[i].second^p[i+1].second;
		cout<<res<<endl;
	}
	
	return 0;
}

Measured memory_bytes: 1256