#define DEBUG_ON_
#define CONDITION true
using namespace std;
#include <algorithm>/*{{{*/
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

static const double PI (3.14159265358979323846);
static const double EPS (1e-9);

typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef complex<double> P;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

#define FOR(i, b, e) for (typeof(b) i = (b); assert((i) <= (e)), i != (e); ++i)
#define REP(i, n) FOR(i, 0, n)

#define OPOVER(_op, _type) inline bool operator _op (const _type &t) const

#define arrsz(a) ( sizeof(a) / sizeof(a[0]) )

#define F first
#define S second
#define MP(a, b) make_pair(a, b)

#define SZ(a) ((ll)a.size())
#define PB(e) push_back(e)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(t,i,c) for(t::iterator i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())

#define BIT(n) (1ULL << (n))
#define BITOF(n, m) ((n) >> (m) & 1)

#define RANGE(a, b, c) ((a) <= (b) && (b) <= (c))

#ifdef DEBUG_ON
#define dprt(fmt, ...) if(CONDITION) fprintf(stderr, fmt, __VA_ARGS__)
#define darr(a) if(CONDITION) copy( (a), (a) + arrsz(a), ostream_iterator<int>(cerr, " ") ); cerr << endl
#define darr_range(a, f, t) if(CONDITION) copy( (a) + (f), (a) + (t), ostream_iterator<int>(cerr, " ") ); cerr << endl
#define dvec(v) if(CONDITION) copy( ALL(v), ostream_iterator<int>(cerr, " ") ); cerr << endl
#define darr2(a, n, m) if(CONDITION) FOR(i, 0, (n)){ darr_range( (a)[i], 0, (m) ); }
#define dvec2(v) if(CONDITION) FOR(i, 0, SZ(v)){ dvec( (v)[i] ); }
#define WAIT() if(CONDITION) { string _wait_; cerr << "(hit return to continue)" << endl; getline(cin, _wait_); }
#else
#define cerr if(0) cerr
#define dprt(fmt, ...)
#define darr(a)
#define darr_range(a, f, t)
#define dvec(v)
#define darr2(a, n, m)
#define dvec2(v)
#define WAIT()
#endif
/*}}}*/

double cross(P a, P b) {
	return (a.real() * b.imag() - a.imag() * b.real());
}

int is_intersected_ls(P a1, P a2, P b1, P b2) {
	return ( cross(a2-a1, b1-a1) * cross(a2-a1, b2-a1) < EPS ) &&
		( cross(b2-b1, a1-b1) * cross(b2-b1, a2-b1) < EPS );
}

P intersection_ls(P a1, P a2, P b1, P b2) {
	P b = b2-b1;
	double d1 = abs(cross(b, a1-b1));
	double d2 = abs(cross(b, a2-b1));
	double t = d1 / (d1 + d2);

	return a1 + (a2-a1) * t;
}

struct interpoint {
	bool mine;
	bool overground;
	double dist;
	OPOVER(<, interpoint) {
		return dist < t.dist;
	}
};
vector<interpoint> interpoints;

int main(int argc, char const* argv[])
{
	int N;
	cin >> N;
	REP (ii, N) {
		interpoints.clear();

		int xa, ya, xb, yb;
		cin >> xa >> ya >> xb >> yb;
		int n;
		cin >> n;

		P a = P(xa, ya);
		P b = P(xb, yb);
		//cout << a.real() << "," << a.imag() << endl << b.real() << "," << b.imag() << endl;
		dprt("a-b =  (%lf, %lf)-(%lf, %lf)\n", a.real(), a.imag());

		REP (i, n) {
			int xs, ys, xt, yt, o, l;
			cin >> xs >> ys >> xt >> yt >> o >> l;
			P s = P(xs, ys);
			P t = P(xt, yt);
			dprt("line_seg (%lf, %lf)-(%lf, %lf)\n", s.real(), s.imag(), t.real(), t.imag());
			//if(ii==10)cout << endl << s.real() << "," << s.imag() << endl << t.real() << "," << t.imag() << endl;
			if (is_intersected_ls(a, b, s, t)) {
				if(!(abs(cross(b-a, t-s)) < EPS)){
					P _ip = intersection_ls(a, b, s, t);
					interpoint ip = { o, l, abs(a - _ip) };
					interpoints.PB(ip);
					dprt("\tintersects at (%lf, %lf), dist from a: %lf, (mine, over) = (%d, %d)\n", _ip.real(), _ip.imag(), abs(a - _ip), o, l);
				}
			}
		}
		SORT(interpoints);

		cerr << endl << "after sorted:" << endl;
		REP (i, SZ(interpoints)) {
			dprt("interpoint #%d: dist(%lf)\n", i, interpoints[i].dist);
		}

		int ans = 0;
		if (SZ(interpoints) > 0) {
			interpoint ip = interpoints[0];
			bool cur_over = ip.mine? ip.overground : !ip.overground;
			dprt("nearest point: dist(%lf)\n", ip.dist);
			dprt("start: cur_over = %d\n", cur_over);
			FOR (i, 1, interpoints.size()) {
				interpoint ip = interpoints.at(i);
				dprt("interpoint #%d, dist: %lf, cur_over: %d, points: (mine, over) = (%d, %d)\n", i, ip.dist, cur_over, ip.mine, ip.overground);
				if ( (ip.mine && cur_over != ip.overground) ||
						(!ip.mine && cur_over == ip.overground) ) {
					++ans;
					cur_over = !cur_over;
					dprt("\tchange state: cur_over = %d\n", cur_over);
				}
			}
		}

		cout << ans << endl;
	}
	return 0;
}

Measured memory_bytes: 940