#include "bits/stdc++.h"

using namespace std;

typedef long double ld;
typedef complex<ld> Point;
typedef pair<double, int> P;

const ld eps = 1e-9, pi = acos(-1.0);

namespace std {
	bool operator <(const Point &lhs, const Point &rhs) {
		if (lhs.real() < rhs.real() - eps) return true;
		if (lhs.real() > rhs.real() + eps) return false;
		return lhs.imag() < rhs.imag();
	}
}

Point input_point() {
	ld x, y;
	cin >> x >> y;
	return Point(x, y);
}

bool eq(ld a, ld b) {
	return (abs(a - b) < eps);
}

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

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

class Line {
public:
	Point a, b;
	int own;//a?????????1 b?????????0
	int pos;//?????¶??????1 ??°?????????0
	Line():a(Point(0,0)),b(Point(0,0)),own(-1),pos(-1){}
	Line(Point a, Point b,int own,int pos) :a(a), b(b),own(own),pos(pos) {}
};

int ccw(Point a, Point b, Point c) {
	b -= a; c -= a;
	if (cross(b, c) > eps) return 1;//a,b,c??????????¨???????
	if (cross(b, c) < -eps) return - 1;//a,b,c???????¨???????
	if (dot(b, c) < 0) return 2;//c,a,b????????§??´??????
	if (norm(b) < norm(c)) return -2;//a,b,c????????§??´??????
	return 0;//a,c,b??§??´??????
}

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;
}

bool isis_sp(Line s, Point p) {//?????????????????????
	return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);
}

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);
}

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

	int num; cin >> num;
	while (num--) {
		ld xa, ya, xb, yb; cin >> xa >> ya >> xb >> yb;
		Point a(xa, ya), b(xb, yb);
		int n; cin >> n;
		vector<Line> rosen;//????????¨??¨
		for (int i = 0; i < n; i++) {
			ld xs, ys, xt, yt; int o, l;
			cin >> xs >> ys >> xt >> yt >> o >> l;
			Point s(xs, ys), t(xt, yt);
			Line li(s, t, o, l);
			rosen.push_back(li);
		}
		Line route(a, b, 1, -1);//?§??????????
		vector<P> lines;//P(?????¢???index) ???????????????????????????
		for (int i = 0; i < n; i++) {
			if (cross(route.b - route.a, rosen[i].b - rosen[i].a) == 0) continue;
			if (isis_ss(route, rosen[i])) {
				Point kouten = is_ll(route, rosen[i]);
				double dis = norm(route.a - kouten);
				lines.push_back(P(dis, i));
			}
		}
		sort(lines.begin(), lines.end());
		int res = 0, pos;
		if (lines.size() == 0) {
			cout << 0 << endl;
			continue;
		}
		int idx = lines[0].second;
		if (rosen[idx].own == 1) {
			pos = rosen[idx].pos;
		}
		else {
			pos = 1 - rosen[idx].pos;
		}
		for (int i = 0; i < (int)lines.size(); i++) {
			int idx = lines[i].second;
			//cout << idx<<" "<<pos << " " << rosen[idx].own << " " << rosen[idx].pos << endl;
			if (rosen[idx].own == 1) {
				if (rosen[idx].pos == pos) continue;
				else {
					pos = 1 - pos;
					res++;
				}
			}
			else {
				if (rosen[idx].pos != pos) continue;
				else {
					pos = 1 - pos;
					res++;
				}
			}
			//cout << i<<" "<<pos << endl;
		}
		cout << res << endl;
	}
}