import std.stdio;
import std.algorithm;
import std.string;
import std.range;
import std.array;
import std.conv;
import std.complex;
import std.math;
import std.ascii;
import std.bigint;
import std.container;
import std.typecons;

int cross(int[] a, int[] b) {
	return a[0]*b[1]-a[1]*b[0];
}
int dot(int[] a, int[] b){
	return a[0]*b[0]+a[1]*b[1];
}

double[] crosspoint(int[] a, int[] b, int[] c, int[] d) {
	auto n = [a[1]-b[1], b[0]-a[0]];
	int[2] bd = b[]-d[];
	int[2] cb = c[]-b[];
	int[2] cd = c[]-d[];
	int tmp[2] = dot(n, bd)*c.dup[]+dot(n, cb)*d.dup[];
	double div = dot(n, cd);
	return [tmp[0]/div, tmp[1]/div];
}
bool isCross(int[]a, int[] b, int[] c, int[] d){
	int[2][4] v;
	v[0] = a.dup[]-c[];
	v[1] = c.dup[]-b[];
	v[2] = b.dup[]-d[];
	v[3] = d.dup[]-a[];
	int s1, s2;
	foreach(i; 0..4) {
		s1 += cross(v[i], v[(i+1)%4]);
		s2 += abs(cross(v[i], v[(i+1)%4]));
	}
	return abs(s1) == s2;
}

alias Tuple!(double[], "p", int, "o", int, "l") T;

void main() {
	int m = to!int(readln().strip());
	foreach(_1 ; 0..m) {
		auto s = map!(to!int)(readln().strip().split());
		int[] a = [s[0], s[1]];
		int[] b = [s[2], s[3]];
		int n = to!int(readln().strip());

		T[] t;
		foreach(_2; 0..n) {
			auto si = map!(to!int)(readln().strip().split());
			auto c = array(si[0..2]);
			auto d = array(si[2..4]);
			if(isCross(a, b, c,d)){
				t ~= T(crosspoint(a,b,c,d), si[4], si[5]);
			}
		}
		if(a[0] != b[0]){
			t.sort!("a.p[0] < b.p[0]")();
		} else {
			t.sort!("a.p[1] < b.p[1]")();
		}
		auto l = [0,1];
		auto ans = [0,0];
		foreach(ti; t) {
			foreach(i; 0..2){
				if((ti.o == 0 && ti.l == l[i]) || (ti.o == 1 && ti.l != l[i])){
					l[i] ^= 1;
					++ans[i];
				}
			}
		}
		writeln(reduce!min(ans));
	}
}