#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;

//int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;

struct  Coordinate {
	long double x;
	long double y;
	Coordinate(long double a, long double b) {
		x = a, y = b;
	}
	Coordinate() {
	}
	Coordinate operator + (const Coordinate& c)const {
		Coordinate box;
		box.x = x + c.x;
		box.y = y + c.y;
		return box;
	}
	Coordinate operator - (const Coordinate& c)const {
		Coordinate box;
		box.x = x - c.x;
		box.y = y - c.y;
		return box;
	}
};

struct Line {
	long double a, b, c;
	Line(const int aa, const int bb, const int cc) {
		a = aa, b = bb, c = cc;
	}
	Line(const Coordinate s, const Coordinate t) {
		if (abs(s.x - t.x) < EPS) {
			a = 1;
			b = 0;
		}
		else {
			b = 1;
			a = (t.y - s.y) / (s.x - t.x);
		}
		c = s.x*a + s.y*b;
	}
};

long double dot(Coordinate a, Coordinate b) {
	return a.x*b.x + a.y*b.y;
}

long double cross(Coordinate a, Coordinate b) {
	return a.x*b.y - b.x*a.y;
}

long double Distance(Coordinate a, Coordinate b) {
	return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
}

bool LineCross(Coordinate a1, Coordinate a2, Coordinate b1, Coordinate b2) {
	return cross(a2 - a1, b1 - a1)*cross(a2 - a1, b2 - a1) < EPS&&cross(b2 - b1, a1 - b1)*cross(b2 - b1, a2 - b1) < EPS && !((a1 - a2).x*(a1 - b1).y == (a1 - a2).y*(a1 - b1).x && (a1 - a2).x*(a1 - b2).y == (a1 - a2).y*(a1 - b2).x&&abs(Distance(a1, a2) - Distance(a1, b1) - Distance(a2, b1)) > EPS&&abs(Distance(a1, a2) - Distance(a2, b2) - Distance(a1, b2)) > EPS&&abs(Distance(b1, b2) - Distance(a1, b1) - Distance(a1, b2)) > EPS&&abs(Distance(b1, b2) - Distance(a2, b1) - Distance(a2, b2)) > EPS);
}

Coordinate LineCross(Line a, Line b) {
	Coordinate ret;
	ret.x = (a.c*b.b - a.b*b.c) / (a.a*b.b - a.b*b.a);
	ret.y = -(a.c*b.a - a.a*b.c) / (a.a*b.b - a.b*b.a);
	return ret;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> T;
	while (T--) {
		long double ax, ay, bx, by;
		cin >> ax >> ay >> bx >> by;
		cin >> N;
		vector<long double>sx(N);
		vector<long double>sy(N);
		vector<long double>gx(N);
		vector<long double>gy(N);
		vector<int>a(N);
		vector<int>b(N);
		vector<pair<long double, int>>p;
		for (int i = 0; i < N; i++) {
			cin >> sx[i] >> sy[i] >> gx[i] >> gy[i] >> a[i] >> b[i];
			if (!a[i])b[i] ^= 1;
			if (LineCross(Coordinate(sx[i], sy[i]), Coordinate(gx[i], gy[i]), Coordinate(ax, ay), Coordinate(bx, by))) {
				auto c = LineCross(Line(Coordinate(sx[i], sy[i]), Coordinate(gx[i], gy[i])), Line(Coordinate(ax, ay), Coordinate(bx, by)));
				p.push_back({ Distance(c,Coordinate(ax,ay)),b[i] });
			}
		}
		sort(p.begin(), p.end());
		int ans = 0;
		for (int i = 1; i < p.size(); i++) {
			if (p[i].second != p[i - 1].second)ans++;
		}
		cout << ans << endl;
	}
}


Measured memory_bytes: 3212