#include <iostream>
#include <algorithm>
#include <limits>
#include <vector>
#include <cmath>
#include <utility>
using namespace std;

const double EPS = 1e-9;

bool tolerant_eq(double a, double b){ return abs(a - b) < EPS; }


struct Point {
	double x, y;
	explicit Point(const double &x = 0.0, const double &y = 0.0) :
		x(x), y(y)
	{ }

	static Point invalid(){
		double qnan = numeric_limits<double>::quiet_NaN();
		return Point(qnan, qnan);
	}
	bool is_valid() const { return !(isnan(x) || isnan(y)); }

	Point operator+(const Point &p) const { return Point(x + p.x, y + p.y); }
	Point &operator+=(const Point &p){ return *this = *this + p; }
	Point operator-(const Point &p) const { return Point(x - p.x, y - p.y); }
	Point &operator-=(const Point &p){ return *this = *this - p; }
	Point operator*(double s) const { return Point(x * s, y * s); }
	Point &operator*=(double s){ return *this = *this * s; }
	Point operator/(double s) const { return Point(x / s, y / s); }
	Point &operator/=(double s){ return *this = *this / s; }

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

Point operator*(double s, const Point &p){ return p * s; }

bool tolerant_eq(const Point &a, const Point &b){
	return tolerant_eq(a.x, b.x) && tolerant_eq(a.y, b.y);
}
double abs(const Point &p){ return sqrt(p.x * p.x + p.y * p.y); }
double norm(const Point &p){ return p.x * p.x + p.y * p.y; }
Point unit(const Point &p){ return p / abs(p); }
Point ortho(const Point &p){ return Point(-p.y, p.x); }
double cross(const Point &a, const Point &b){ return a.x * b.y - a.y * b.x; }
double dot(const Point &a, const Point &b){ return a.x * b.x + a.y * b.y; }
int ccw(const Point &a, const Point &b, const Point &c){
	Point d = b - a, e = c - a;
	if(cross(d, e) > 0.0){ return 1; }
	if(cross(d, e) < 0.0){ return -1; }
	if(dot(d, e) < 0.0){ return 2; }
	if(abs(d) < abs(e)){ return -2; }
	return 0;
}


struct Segment {
	Point a, b;
	explicit Segment(const Point &a = Point(), const Point &b = Point()) :
		a(a), b(b)
	{ }

	static Segment invalid(){
		Point inv = Point::invalid();
		return Segment(inv, inv);
	}
	bool is_valid() const { return a.is_valid() && b.is_valid(); }

	bool operator<(const Segment &s) const {
		return (a == s.a) ? (b < s.b) : (a < s.a);
	}
};

bool tolerant_eq(const Segment &a, const Segment &b){
	if(tolerant_eq(a.a, b.a) && tolerant_eq(a.b, b.b)){ return true; }
	if(tolerant_eq(a.a, b.b) && tolerant_eq(a.b, b.a)){ return true; }
	return false;
}


bool intersect(const Segment &a, const Segment &b){
	if(ccw(a.a, a.b, b.a) * ccw(a.a, a.b, b.b) > 0){ return false; }
	if(ccw(b.a, b.b, a.a) * ccw(b.a, b.b, a.b) > 0){ return false; }
	return true;
}


Point crosspoint(const Segment &a, const Segment &b){
	if(!intersect(a, b)){ return Point::invalid(); }
	double x = cross(a.b - a.a, b.b - b.a);
	double y = cross(a.b - a.a, a.b - b.a);
	if(abs(x) < EPS && abs(y) < EPS){
		return b.a;
	}else if(abs(x) < EPS){
		return Point::invalid();
	}else{
		return b.a + y / x * (b.b - b.a);
	}
}

int main(){
	int T;
	cin >> T;
	while(T--){
		double xa, ya, xb, yb;
		cin >> xa >> ya >> xb >> yb;
		Segment s(Point(xa, ya), Point(xb, yb));
		int N;
		cin >> N;
		vector< pair<Point, int> > v;
		while(N--){
			double x1, y1, x2, y2;
			int o, l;
			cin >> x1 >> y1 >> x2 >> y2 >> o >> l;
			Segment t(Point(x1, y1), Point(x2, y2));
			Point cp = crosspoint(s, t);
			if(cp.is_valid()){
				if(xa == xb){ cp.x = xa; }
				v.push_back(make_pair(cp, o ^ l));
			}
		}
		sort(v.begin(), v.end());
		int answer = 0;
		for(int i = 1; i < v.size(); ++i){
			if(v[i - 1].second != v[i].second){ ++answer; }
		}
		cout << answer << endl;
	}
	return 0;
}

Measured memory_bytes: 1240