#include "bits/stdc++.h"

#define REP(i,n) for(ll i=0;i<ll(n);++i)
#define RREP(i,n) for(ll i=ll(n)-1;i>=0;--i)
#define FOR(i,m,n) for(ll i=m;i<ll(n);++i)
#define RFOR(i,m,n) for(ll i=ll(n)-1;i>=ll(m);--i)
#define ALL(v) (v).begin(),(v).end()
#define UNIQUE(v) v.erase(unique(ALL(v)),v.end());
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

constexpr int dx[8] = { 1,1,0,-1,-1,-1,0,1 };
constexpr int dy[8] = { 0,1,1,1,0,-1,-1,-1 };


using namespace std;

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template <class T> bool chmin(T & a, T b) { if (a > b) { a = b; return true; } return false; }
template <class T> bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

using namespace std;
#include "bits/stdc++.h"


//////////////////////////////////////
using ld = long double;
using Point = std::complex<ld>;
const ld  pi = std::acos(-1);
constexpr ld eps = 1e-9;

struct Line {
	Point a, b;
};

struct Lseg {
	Point a, b;
};

struct Circle {
	Point p;
	ld r;
};
bool eq(ld a, ld b) {
	return std::abs(a - b) < eps;
}

ld dot(Point a, Point b) {
	return std::real(std::conj(a) * b);
}

ld cross(Point a, Point b) {
	return std::imag(std::conj(a) * b);
}

ld norm(Point a) {
	return dot(a, a);
}

int ccw(Point a, Point b, Point c) {
	b -= a; c -= a;
	if (cross(b, c) > eps) return 1; //a,b,c: anticlockwise
	if (cross(b, c) < -eps) return -1;//a,b,c: clockwise
	if (dot(b, c) < 0) return 2; //c,a,b:liner
	if (norm(b) < norm(c)) return -2;// a,b,c: liner
	return 0;// a,c,b:liner
}

bool isis_ll(Line l, Line m) {
	return !eq(cross(l.b - l.a, m.a - m.b), 0);
}

bool isis_ls(Line l, Lseg s) {
	return cross(l.b - l.a, l.a - s.a) * cross(l.b - l.a, l.a - s.b) < eps;
}

bool isis_ss(Lseg s, Lseg 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;
}

bool isis_lp(Line l, Point p) {
	return std::abs(cross(l.b - p, l.a - p)) < eps;
}

bool isis_sp(Lseg s, Point p) {
	return eq(abs(s.a - p) + abs(s.b - p), abs(s.b - s.a));
}

Point proj(Line l, Point p) {
	ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);
	return l.a + t * (l.a - l.b);
}

Point proj(Lseg l, Point p) {
	ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);
	return l.a + t * (l.a - l.b);
}

Point is_ll(Line s, Line t) {
	Point sv = s.b - s.a, tv = t.b - t.a;
	assert(cross(sv, tv) != 0);
	return s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);
}

ld dist_lp(Line l, Point p) {
	return abs(p - proj(l, p));
}

ld dist_ll(Line l, Line m) {
	return isis_ll(l, m) ? 0 : dist_lp(l, m.a);
}

ld dist_ls(Line l, Lseg s) {
	return isis_ls(l, s) ? 0 : std::min(dist_lp(l, s.a), dist_lp(l, s.b));
}

ld dist_sp(Lseg s, Point p) {
	Point r = proj(s, p);
	return isis_sp(s, r) ? abs(r - p) : std::min(std::abs(s.a - p), std::abs(s.b - p));
}

ld dist_ss(Lseg s, Lseg t) {
	if (isis_ss(s, t))return 0;
	return std::min({ dist_sp(s,t.a),dist_sp(s,t.b),dist_sp(t,s.a),dist_sp(t,s.b) });
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int t; cin >> t;
	REP(aa, t) {
		ld xa, ya, xb, yb;
		cin >> xa >> ya >> xb >> yb;
		Lseg l{ Point{xa,ya},Point{xb,yb} };
		int n; cin >> n;
		vector<Lseg> v(n);
		vi p(n);
		vector<pair<ld, int>> pos;
		REP(i, n) {
			ld xs, ys, xt, yt;
			cin >> xs >> ys >> xt >> yt;
			int a, b;
			cin >> a >> b;
			Lseg tmp = Lseg{ Point{xs,ys},Point{xt,yt} };
			ld d = isis_ss(l, tmp);
			if (d > eps) {
				Point p = is_ll(Line{ l.a,l.b }, Line{ tmp.a,tmp.b });
				pos.push_back({ real(p),a ^ b });
			}
		}
		sort(ALL(pos));
		int ans = 0;
		REP(i, (int)pos.size() - 1) {
			if (pos[i].second != pos[i + 1].second)ans++;
		}
		cout << ans << endl;
	}
}


Measured memory_bytes: 3276