#include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <cassert>
using namespace std;
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define EACH(t,i,c) for(t::iterator i=(c).begin(); i!=(c).end(); ++i)
const double EPS = 1e-10;
const double PI  = acos(-1.0);



#include <complex>
typedef complex<double> P;

// 外積 (cross product) : a×b = |a||b|sinΘ
double cross(P a, P b) {
  return (a.real() * b.imag() - a.imag() * b.real());
}
// 点cが線分a,b上にあるかないか(2)
int is_point_on_line(P a, P b, P c) {
  // |a-c| + |c-b| <= |a-b| なら線分上
  return (abs(a-c) + abs(c-b) < abs(a-b) + EPS);
}
// a1,a2を端点とする線分とb1,b2を端点とする線分の交差判定
int is_intersected_ls(P a1, P a2, P b1, P b2) {
  if (( cross(a2-a1, b1-a1) * cross(a2-a1, b2-a1) < -EPS ) &&
      ( cross(b2-b1, a1-b1) * cross(b2-b1, a2-b1) < -EPS ))
	  return true;
  return is_point_on_line(a1, a2, b1) || is_point_on_line(a1, a2, b2) ||
         is_point_on_line(b1, b2, a1) || is_point_on_line(b1, b2, a2);
}
// a1,a2を端点とする線分とb1,b2を端点とする線分の交点計算
P intersection_ls(P a1, P a2, P b1, P b2) {
  P b = b2-b1;
  double d1 = abs(cross(b, a1-b1));
  double d2 = abs(cross(b, a2-b1));
  double t = d1 / (d1 + d2);

  return a1 + (a2-a1) * t;
}
// 2つのスカラーが等しいかどうか
#define EQ(a,b) (abs((a)-(b)) < EPS)
// 2つのベクトルが等しいかどうか
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )

typedef vector<double> vd;
struct line{
	P start,goal;
	int o,l;
	P intersection;
	bool intersect;
	line(P start,P goal,int o,int l):start(start),goal(goal),o(o),l(l),intersect(false){
	}
};
P start,goal;
bool pred(const line &left, const line &right){
	return abs(left.intersection-start)<abs(right.intersection-start);
}
int main(){
	int n_data;
	cin>>n_data;
	REP(i_data,n_data){
		int xa,ya,xb,yb;
		cin>>xa>>ya>>xb>>yb;
		int n;
		cin>>n;
		vector<line> lines;
		REP(i,n){
			int xs,ys,xt,yt,o,l;
			cin>>xs>>ys>>xt>>yt>>o>>l;
			lines.push_back(line(P(xs,ys),P(xt,yt),o,l));
		}
		vector<line> intersections;
		start=P(xa,ya);
		goal=P(xb,yb);
		REP(i,n){
			if(is_intersected_ls(start,goal,lines[i].start,lines[i].goal)){
				lines[i].intersection=intersection_ls(start,goal,lines[i].start,lines[i].goal);
				lines[i].intersect=true;
				intersections.push_back(lines[i]);
			}
		}
		sort(ALL(intersections),pred);
		int res=0;
		int ll=-1;
		REP(i,intersections.size()){
			if(!intersections[i].intersect)continue;
			if(ll!=-1){
				if(ll!=(intersections[i].l+(intersections[i].o==1?0:1))%2){
					res++;
				}
			}
			ll=(intersections[i].l+(intersections[i].o==1?0:1))%2;
		}
		cout<<res<<endl;
	}
}

Measured memory_bytes: 956