#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
#include <complex>

using namespace std;

#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define rep(i,n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(),(r).end()
#define rall(r) (r).rbegin(),(r).rend()
#define fi first
#define se second
#define DBG(X) cout<<" "<<#X<<" : "<<X<<endl

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;

const int INF = 1e9;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const ll LINF = 1e18;

struct Input
{
	int n;char c;char str[10010];string s;ll l;double d;
	int nextInt(){scanf("%d", &n);return n;}
	ll nextLong(){scanf("%lld", &l);return l;}
	double nextDouble(){scanf("%lf", &d);return d;}
	char nextChar(){scanf("%c", &c);return c;}
	string next(){scanf("%s", str);return string(str);}
	string nextLine(){getline(cin, s);return s;}
};
Input in;

typedef complex<double> P;
namespace std {
  bool operator < (const P& a, const P& b) {
    return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
  }
}
double cross(const P& a, const P& b) {return imag(conj(a)*b);}
double dot(const P& a, const P& b) {return real(conj(a)*b);}

struct L : public vector<P>{
	L(const P& a, const P& b){
		clear(); pb(a); pb(b);
	}
};

typedef vector<P> G;

struct C{
	P p;
	double r;
	C(const P& p, double r):p(p), r(r){};
};

int ccw(P a, P b, P 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;
}
bool intersectSS(const L &s, const L &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;
}

P projection(const L &l, const P &p) {
  double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);
  return l[0] + t*(l[0]-l[1]);
}
P reflection(const L &l, const P &p) {
  return p + 2.0 * (projection(l, p) - p);
}
P crosspoint(const L &l, const L &m) {
  double A = cross(l[1] - l[0], m[1] - m[0]);
  double B = cross(l[1] - l[0], l[1] - m[0]);
  if (abs(A) < EPS && abs(B) < EPS) return m[0]; // same line
  if (abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!
  return m[0] + B / A * (m[1] - m[0]);
}
double dist2(const P& a, const P& b){
	P p(a-b);
	return dot(p, p);
}

bool getNext(bool a, bool b){
	return a?b:!b;
}

void printP(const P& p){
	cout<<" "<<real(p)<<" "<<imag(p);
}

int main(){
	int N = in.nextInt();
	while(N--){
		double xa = in.nextInt(), ya = in.nextInt(), xb = in.nextInt(), yb = in.nextInt();
		L l({{xa, ya}, {xb, yb}});
		int n = in.nextInt();
		vector<pii> ps;
		vector<pair<double, pair<int, int> > > ds;
		rep(i, n){
			double xs = in.nextInt(), ys = in.nextInt(), xt = in.nextInt(), yt = in.nextInt();
			L t{{xs, ys}, {xt, yt}};
			ps.pb({in.nextInt(), in.nextInt()});
			//printP(t[0]);
			//printP(t[1]);
			//cout<<endl;
			if(intersectSS(l, t))
			//if(ccw(l[0], crosspoint(l, t), l[1])==-2 && ccw(t[0], crosspoint(l, t), t[1]) == -2)
				ds.pb({sqrt(dist2(crosspoint(l, t), l[0])), {ps.back().fi, ps.back().se}});
			//}
		}
		//cout<<" "<<ds.size()<<endl;
		sort(all(ds));
		int ans = 0;
		if(ds.size()==0){
			cout<<0<<endl;
			continue;
		}
		bool now = getNext(ds[0].se.fi, ds[0].se.se);
		rep(i, ds.size()){
			if(now!=getNext(ds[i].se.fi, ds[i].se.se)){
				ans++;
				now = !now; 
			}
		}
		cout<<ans<<endl;
	}
}

Measured memory_bytes: 3228