// tsukasa_diary's programing contest code template
#include <bits/stdc++.h>
using namespace std;
// define
#define for_(i,a,b) for(int i=a;i<b;++i)
#define for_rev(i,a,b) for(int i=a;i>=b;--i)
#define allof(a) a.begin(),a.end()
#define minit(a,b) memset(a,b,sizeof(a))
#define size_of(a) (int)a.size()
// typedef
typedef long long lint;
typedef double Double;
typedef pair<int, int> pii;
//
typedef vector< int > Array;
typedef vector< Array > Matrix;
typedef vector< Double > DArray;
typedef vector< DArray > DMatrix;
typedef vector< string > SArray;
typedef vector< pii > PArray;
// popcount
inline int POPCNT(int _x) { return __builtin_popcount(_x); }
inline int POPCNT(lint _x) { return __builtin_popcountll(_x); }
// inf
const int iINF = 1L << 30;
const lint lINF = 1LL << 60;
// eps
Double EPS = 1e-9;
// in range
inline bool in_range(int _v, int _mx, int _mi) { return _mi <= _v && _v < _mx; }
inline bool in_range(Double _v, Double _mi, Double _mx) { return -EPS < _v - _mi && _v - _mx < EPS; }
inline bool in_range(int _x, int _y, int _W, int _H) { return 0 <= _x && _x < _W && 0 <= _y && _y < _H; }
// neighbor clockwise
const int DX[4] = {0,1,0,-1}, DY[4] = {-1,0,1,0};
const int DX_[8] = {0,1,1,1,0,-1,-1,-1}, DY_[8] = {-1,-1,0,1,1,1,0,-1};
// variable update
inline void modAdd(int& _a, int _b, int _m) { _a = (_a + _b) % _m; }
inline void modAdd(lint& _a, lint _b, lint _m) { _a = (_a + _b) % _m; }
inline void minUpdate(int& _a, int _b) { _a = min(_a, _b); }
inline void minUpdate(lint& _a, lint _b) { _a = min(_a, _b); }
inline void maxUpdate(int& _a, int _b) { _a = max(_a, _b); }
inline void maxUpdate(lint& _a, lint _b) { _a = max(_a, _b); }

// Point & Vector
typedef struct Point {
	Double x, y;
	int type;
	Point() {}
	Point(Double _x, Double _y) : x(_x), y(_y) {}
} Vector;
// operator
Point operator + (const Point& _a, const Point& _b) { return Point(_a.x + _b.x, _a.y + _b.y); }
Point operator - (const Point& _a, const Point& _b) { return Point(_a.x - _b.x, _a.y - _b.y); }
Point operator * (const Point& _a, const Point& _b) { return Point(_a.x * _b.x, _a.y * _b.y); }
Point operator / (const Point& _a, const Point& _b) { return Point(_a.x / _b.x, _a.y / _b.y); }
Point operator * (const Point& _a, Double _b) { return Point(_a.x * _b, _a.y * _b); }
Point operator / (const Point& _a, Double _b) { return Point(_a.x / _b, _a.y / _b); }
bool operator < (const Point& _a, const Point& _b) { return _a.x != _b.x ? _a.x < _b.x : _a.y < _b.y; }
bool operator > (const Point& _a, const Point& _b) { return _b < _a; }
bool operator == (const Point& _a, const Point& _b) { return fabs(_a.x - _b.x) < EPS && fabs(_a.y - _b.y) < EPS; }

//
Double norm(const Point& _a) { return _a.x * _a.x + _a.y * _a.y; }
Double abs(const Point& _a) { return sqrt(norm(_a)); }
Double dot(const Vector& _a, const Vector& _b) { return _a.x * _b.x + _a.y * _b.y; }
Double cross(const Vector& _a, const Vector& _b) { return _a.x * _b.y - _a.y * _b.x; }

//
int ccw(const Point& _a, const Point& _b, const Point&  _c) {
	Vector ba = _b - _a, ca = _c - _a;
	if (cross(ba, ca) > EPS) return +1; // ccw
	if (cross(ba, ca) < -EPS) return -1; // cw
	if (dot(ba, ca) < -EPS) return +2; // c-a-b
	if (abs(ba) + EPS < abs(ca)) return -2; // a-b-c
	return 0; // a-c-b
}

// Line & Segment
typedef struct Line {
	Point s, e;
	Line() {}
	Line(Point _s, Point _e) : s(_s), e(_e) {}
} Segment;

//
bool is_cross(const Segment& _a, const Segment& _b) {
	return (ccw(_a.s, _a.e, _b.s) * ccw(_a.s, _a.e, _b.e) <= 0
			&& ccw(_b.s, _b.e, _a.s) * ccw(_b.s, _b.e, _a.e) <= 0);
}

//
Point cross_point(const Line& _a, const Line& _b) {
	Double d1 = cross(_b.e - _b.s, _b.s - _a.s);
	Double d2 = cross(_b.e - _b.s, _a.e - _a.s);
	return _a.s + (_a.e - _a.s) * d1 / d2;
}

Segment ab;
int n;
vector< Segment > segs;
Array pos, type;

void solve() {
	vector< Point > vp;
	
	for_(i,0,n) {
		if (!is_cross(ab, segs[i])) continue;
		
		Point p = cross_point(ab, segs[i]);
		
		if (pos[i]) p.type = type[i];
		else p.type = 1 - type[i];
		
		vp.push_back(p);
	}
	
	sort(allof(vp));
	
	int ans = 0;
	int v_size = size_of(vp);
	
	for_(i,0,v_size-1) if (vp[i].type != vp[i + 1].type) ++ans;
	cout << ans << endl;
}

int main() {
	int CASE; cin >> CASE;
	
	for_(c,0,CASE) {
		segs.clear();
		pos.clear();
		type.clear();
		
		Double xa, ya, xb, yb; cin >> xa >> ya >> xb >> yb;
		ab = Segment(Point(xa, ya), Point(xb, yb));
		
		cin >> n;
		for_(i,0,n) {
			Double xs, ys, xe, ye;
			int o, l;
			cin >> xs >> ys >> xe >> ye >> o >> l;
			segs.push_back(Segment(Point(xs, ys), Point(xe, ye)));
			pos.push_back(o);
			type.push_back(l);
		}
		
		solve();
	}
	
	return 0;
}