#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <fstream>
using namespace std;

#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl

#define INF 1000000000
#define EPS 1e-9

#define EQ(x,y) fabs((x)-(y))<(EPS)

typedef pair<pair<double, double>, bool> P;

bool sorter(const P p1, const P p2){
  return p1.first<p2.first;
}

int main(){
  int tc;
  cin>>tc;
  while(tc-->0){
    vector<P> points;

    double xa,ya,xb,yb;
    scanf("%lf %lf %lf %lf", &xa, &ya, &xb, &yb);

    double lf=min(xa,xb), r=max(xa,xb);
    double b=min(ya,yb), t=max(ya,yb);

    int n;
    cin>>n;
    rep(i,n){
      double x1,y1,x2,y2;
      int o,l;
      scanf("%lf %lf %lf %lf %d %d", &x1, &y1, &x2, &y2, &o, &l);

      double ll=min(x1,x2), rr=max(x1,x2);
      double bb=min(y1,y2), tt=max(y1,y2);

      // if pararell
      if( EQ((xa-xb)*(y1-y2), (ya-yb)*(x1-x2)) ) continue;

      // where is intersection?
      double nx, ny;
      if(EQ(xa,xb)){
	double d2 = (y1-y2)/(x1-x2);
	nx = xa;
	ny = d2*(nx-x1)+y1;
      } else if( EQ(x1,x2) ){
	double d1 = (ya-yb)/(xa-xb);
	nx = x1;
	ny = d1*(nx-xa)+ya;
      } else {
	double d1 = (ya-yb)/(xa-xb);
	double d2 = (y1-y2)/(x1-x2);
	nx = (y1-d2*x1-(ya-d1*xa))/(d1-d2);
	ny = d1*(nx-xa)+ya;
	//assert(EQ(ny, d2*(nx-x1)+y1));
      }
      
      if( nx-lf>-EPS && r-nx>-EPS && ny-b>-EPS && t-ny>-EPS &&
	  nx-ll>-EPS &&rr-nx>-EPS && ny-bb>-EPS&&tt-ny>-EPS){
      // if cross with new line, add point to vector
	points.pb(mp(mp(nx,ny), (o==l)));
      }
    }

    sort(all(points));

    int res=0;
    repl(i,1,points.size()){
      if(points[i-1].second != points[i].second) res++;
    }

    cout<<res<<endl;

  }
  return 0;
}