#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
#include <utility>
#include <complex>
using namespace std;

typedef complex<int> pint;
typedef const pint &refpint;
typedef pair<double,bool> pdb;


#define INF 1e99

inline int cross(refpint a, refpint b){
	return a.real() * b.imag() - a.imag() * b.real();
}

inline double intersect(refpint a, refpint dif1, refpint f, refpint negdif2){
	int Aint = cross(dif1, negdif2);
	if( Aint == 0 ) return INF;
	double A = Aint;
	
	pint ddif = f - a;
	double t = cross(ddif, negdif2) / A;
	double s = cross(dif1, ddif) / A;
	
	if(t >= 0.0 && t <= 1.0 && s >= 0.0 && s <= 1.0){
		return t;
	}
	return INF;
}


int main(){
	int dataset;
	vector<pdb> ts;
	ts.reserve(100);
	for(scanf("%d", &dataset); dataset--; ){
		ts.clear();
		pint a, from, dif1, negdif2;
		int n, o, l, x1, x2, y1, y2;

		scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &n);
		a = pint(x1, y1);
		dif1 = pint(x2, y2) - a;

		ts.reserve(n);

		for(int i = 0; i < n; ++i){
			scanf("%d%d%d%d%d%d", &x1, &y1, &x2, &y2, &o, &l);
			from = pint(x1, y1);
			negdif2 = from - pint(x2, y2);

			double t = intersect(a, dif1, from, negdif2);
			if( t != INF ){
				ts.push_back( pdb(t, o != l) );
			}
		}
		
		sort(ts.begin(), ts.end());
		int res = 0;
		for(int i = 1; i < ts.size(); ++i){
			if(ts[i-1].second != ts[i].second){
				++res;
			}
		}
		printf("%d\n", res);
	}
}