//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <queue>
#include <tuple>

using namespace std;

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);


const double mEPS = 1e-10;

//???????????????????????????
class Point{
public:
	double x, y;

	Point(double x = 0.0, double y = 0.0) : x(x), y(y){}

	Point operator + (Point p){ return Point(x + p.x, y + p.y); }
	Point operator - (Point p){ return Point(x - p.x, y - p.y); }
	Point operator * (double a){ return Point(a*x, a*y); }
	Point operator / (double a){ return Point(x / a, y / a); }

	double abs(){ return sqrt(norm()); }
	double norm(){ return x*x + y*y; }

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

	//EPS??????????°??????????
	bool operator == (const Point &p) const {
		return fabs(x - p.x) < mEPS && fabs(y - p.y) < mEPS;
	}

};

class Segment{
public:
	Point p1, p2;
};

class Circle{
public:
	Point c;
	double r;
};

typedef vector<Point> Polygon;

//???????????¨????????±???
typedef Point Vector;
//???????????´?????¨????????±???
typedef Segment Line;

//??????
double dot(Vector a, Vector b){
	return a.x*b.x + a.y*b.y;
}

//???????????§??????
double cross(Vector a, Vector b){
	return a.x*b.y - a.y*b.x;
}

int ccw(Point p0, Point p1, Point p2){

	//p0->p1???p0?§???????p1?????????????????????????????¨????????¨???

	//p0->p2??????????¨??????????????????¨???
	static const int COUNTER_CLOCKWISE = 1;
	//p0->p2???????¨??????????????????¨???
	static const int CLOCKWISE = -1;
	//p2,p0,p1????????§????????´?????????????????¨???
	static const int ONLINE_BACK = 2;
	//p0,p1,p2 ????????§????????´?????????????????´???
	static const int ONLINE_FRONT = -2;
	//p2 ????????? p0p1 ??????????????´???
	static const int ON_SEGMENT = 0;

	Vector a = p1 - p0;
	Vector b = p2 - p0;

	if (cross(a, b) > mEPS) return COUNTER_CLOCKWISE;
	if (cross(a, b) < -mEPS) return CLOCKWISE;
	if (dot(a, b) < -mEPS) return ONLINE_BACK;
	if (a.norm() < b.norm()) return ONLINE_FRONT;

	return ON_SEGMENT;

}

bool intersect(Segment s1, Segment s2){
	return(ccw(s1.p1, s1.p2, s2.p1) * ccw(s1.p1, s1.p2, s2.p2) <= 0 &&
		ccw(s2.p1, s2.p2, s1.p1) * ccw(s2.p1, s2.p2, s1.p2) <= 0);
}

Point getCrossPoint(Segment s1, Segment s2){
	Vector base = s2.p2 - s2.p1;
	double d1 = abs(cross(base, s1.p1 - s2.p1));
	double d2 = abs(cross(base, s1.p2 - s2.p1));
	double t = d1 / (d1 + d2);
	return s1.p1 + (s1.p2 - s1.p1)*t;
}


int main(){
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int T; cin >> T;
  while(T--){
	Segment st;
	cin >> st.p1.x >> st.p1.y >> st.p2.x >> st.p2.y;
	int N; cin >> N;
	vector<tuple<double,int,int>> crp;
	REP(i,N){
	  Segment u;
	  int o, l;
	  cin >> u.p1.x >> u.p1.y >> u.p2.x >> u.p2.y >> o >> l;
	  if(intersect(st,u)){
		Point cp = getCrossPoint(st,u);
		crp.PB(make_tuple((cp-st.p1).abs(), o, l));
	  }
	}
	SORT(crp);
	int b = 0, sum1 = 0;
	for(int i=0;i<SZ(crp);++i){
	  if(get<1>(crp[i])){
		if(b != get<2>(crp[i])){
		  b = 1-b;
		  ++sum1;
		}
	  }
	  else{
		if(b == get<2>(crp[i])){
		  b = 1 - b;
		  ++sum1;
		}
	  }
	}
	int sum2 = 0; b = 1;
	for(int i=0;i<SZ(crp);++i){
	  if(get<1>(crp[i])){
		if(b != get<2>(crp[i])){
		  b = 1-b;
		  ++sum2;
		}
	  }
	  else{
		if(b == get<2>(crp[i])){
		  b = 1 - b;
		  ++sum2;
		}
	  }
	}
	cout << min(sum1, sum2) << endl;
  }

  return 0;
}

Measured memory_bytes: 1248