#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
#include <bitset>
#include <time.h>
#include <tuple>
#include <iomanip>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e12;
const ll MOD = 1e9 + 7;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,N) for(int i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1

ll sq(ll num) { return num*num; }
ll mod_pow(ll x, ll n) {
	if (n == 0)return 1;
	if (n == 1)return x%MOD;
	ll res = sq(mod_pow(x, n / 2));
	res %= MOD;
	if (n % 2 == 1) {
		res *= x;
		res %= MOD;
	}
	return res;
}
ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a*b % MOD; }

// 内積 (dot product) : a⋅b = |a||b|cosθ
double dot(Point a, Point b) {
	return (a.real() * b.real() + a.imag() * b.imag());
}
// 外積 (cross product) : a×b = |a||b|sinθ
double cross(Point a, Point b) {
	return (a.real() * b.imag() - a.imag() * b.real());
}

double norm(Point p){
	return sqrt(p.real() * p.real() + p.imag() * p.imag());
}

int ccw(Point a, Point b, Point c) {
  b -= a; c -= a;
  if (cross(b, c) > 0)   return +1;       // counter clockwise
  if (cross(b, c) < 0)   return -1;       // clockwise
  if (dot(b, c) < 0)     return +2;       // c--a--b on line
  if (norm(b) < norm(c)) return -2;       // a--b--c on line
  return 0;
}

struct Line : public vector<Point> {
  Line(const Point &a, const Point &b) {
    push_back(a); push_back(b);
  }
};

bool intersectSS(const Line &s, const Line &t) {
  return ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= 0 &&
         ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= 0;
}

Point intersection_ls(Point a1, Point a2, Point b1, Point b2) {
	Point b = b2 - b1;
	double d1 = abs(cross(b, a1 - b1));
	double d2 = abs(cross(b, a2 - b1));
	if(d1<EPS && d2<EPS) return b1;
	double t = d1 / (d1 + d2);

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

typedef pair<double,bool> PD;
int N;
Point pa,pb;


int main(){
	cin >> N;
	int cnt = 0;
	while(N--){
		cnt++;
		int xa,ya,xb,yb;
		cin >> xa >> ya >> xb >> yb;
		pa = Point(xa,ya);
		pb = Point(xb,yb);
		vector<PD> vp;
		int n;
		cin >> n;
		rep(i,n){
			int xa,ya,xb,yb;
			bool o,l;
			cin >> xa >> ya >> xb >> yb >> o >> l;
			// if(cnt==451){
			// 	cout << xa <<" "<< ya << " " << xb << " " << yb << " " << o << " " << l << endl;
			// }
			Point qa = Point(xa,ya);
			Point qb = Point(xb,yb);
			if(o)l = !l;
			if(intersectSS(Line(pa,pb),Line(qa,qb))){
				Point c = intersection_ls(pa,pb,qa,qb);
				vp.push_back(PD(c.real(),l));
			}
		}
		if(vp.size() == 0){
			cout << 0 << endl;
			continue;
		}
		sort(ALL(vp));
		int ans = 0;
		// if(cnt<10){
			// rep(i,vp.size()){
			// 	cout << vp[i].first << " " << vp[i].second << endl;
			// }
		// }
		rep(i,vp.size()-1){
			if(vp[i].second != vp[i+1].second)ans++;
		}
		cout << ans << endl;
	}
}


Measured memory_bytes: 3016