#include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) begin(a),end(a)
#define MS(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
const ll INF = 114514810;
const ll MOD = 1000000007;
const double EPS = 1e-8;
const double PI = acos(-1.0);
struct edge
{
	int from, to, cost;
	bool operator < (const edge& e) const { return cost < e.cost; }
	bool operator >(const edge& e) const { return cost > e.cost; }
};
int dx[] = { -1, 0, 0, 1 }; int dy[] = { 0, -1, 1, 0 };
bool valid(int x, int y, int h, int w) { return (x >= 0 && y >= 0 && x < h&&y < w); }
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///
bool eq(double a, double b) { return fabs(a - b) < EPS; }

struct point
{
	double x, y;
	point(double x = 0, double y = 0) :x(x), y(y) {};

	point operator + (point p) { return point(x + p.x, y + p.y); }
	point operator - (point p) { return point(x - p.x, y - p.y); }
	point operator * (double s) { return point(x * s, y * s); }
	point operator / (double s) { return point(x / s, y / s); }

	double norm() { return x*x + y*y; }
	double abs() { return sqrt(norm()); }

	bool operator < (const point &p) const
	{
		return x != p.x ? x < p.x : y < p.y;
	}

	bool operator == (const point &p) const
	{
		return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
	}

};
typedef vector<point> polygon;

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)
{
	b = b - a; c = c - a;
	if (cross(b, c) > EPS) return 1;
	if (cross(b, c) < -EPS) return -1;
	if (dot(b, c) < 0) return 2;
	if (b.norm() < c.norm()) return -2;
	return 0;
}

struct line
{
	point a, b;
	line(double x1 = 0, double y1 = 0, double x2 = 0, double y2 = 0) :a(point(x1, y1)), b(point(x2, y2)) {};

	point univec() { return (b - a) / (b - a).norm(); }
};

point cross_point(line s1, line s2)
{
	point sv = s1.b - s1.a;
	point tv = s2.b - s2.a;
	return s1.a + sv*cross(tv, s2.a - s1.a) / cross(tv, sv);
}
bool isis_ss(line s, line t)
{
	return (ccw(s.a, s.b, t.a)*ccw(s.a, s.b, t.b) <= 0 &&
		ccw(t.a, t.b, s.a)*ccw(t.a, t.b, s.b) <= 0);
}


int main()
{
	int n;
	cin >> n;
	while (n--)
	{
		double xa, ya, xb, yb;
		cin >> xa >> ya >> xb >> yb;
		line sin(xa, ya, xb, yb);
		int k;
		cin >> k;
		vector<pair<double, int>> v;
		REP(i, k)
		{
			double xs, ys, xt, yt; int o, l;
			cin >> xs >> ys >> xt >> yt >> o >> l;
			line den(xs, ys, xt, yt);
			if (isis_ss(sin, den))
			{
				point cp = cross_point(sin, den);
				v.push_back(make_pair((cp - point(xa, ya)).norm(), o^l));
			}
		}
		sort(ALL(v));
		int cnt = 0;
		if (v.size() != 0)
		{
			int tmp = v[0].second;
			REP(i, v.size())
			{
				if (v[i].second != tmp)
				{
					cnt++;
					tmp = v[i].second;
				}
			}
		}
		cout << cnt << endl;
	}

	return 0;
}

Measured memory_bytes: 1240