#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
using namespace std;
typedef pair<int,int> P;
typedef pair<double,P> PP;
#define INF (11111)
struct F{
  double a,b;
  bool ng;

  double xs,ys,xt,yt;
  F(int xs,int ys,int xt,int yt)
  {
    this->xs = (double)xs;
    this->ys = (double)ys;
    this->xt = (double)xt;
    this->yt = (double)yt;
    if(xt-xs==0){
      ng = true;
      a = INF;
      b = INF;
      return;
    }
    ng = false;
    a = (this->yt-this->ys)/(this->xt-this->xs);
    b = this->ys - a * this->xs;
  }
};
int N;
vector<PP> s;
//vector<F> f;
inline double abd(double a){ return a>0?a:-a; };
inline void Fcheck(F a, F b,double& x,double& y){
  if(a.ng){
    if(b.ng){
      x = INF;
      y = INF;
    }else{
      x = a.xs;
      y = b.ys+(a.xs-b.xs)*b.a;
    }
  }else if(b.ng){
    if(a.ng){
      x = INF;
      y = INF;
    }else{
      x = b.xs;
      y = a.ys+(b.xs-a.xs)*a.a;
    }
  }else if(b.a==a.a){
    x = INF;
    y= INF;
  }else{
    y =  (a.b*(-b.a) - b.b*(-a.a))/(-b.a - (-a.a));
    x =  (b.b - a.b)/(-b.a - (-a.a));
  }

  if( !((double)min(a.xs,a.xt) <= x && (double)max(a.xs,a.xt) >= x &&
	(double)min(a.ys,a.yt) <= y && (double)max(a.ys,a.yt) >= y ) ){
    x = INF;
    y = INF;
  }

	//Y=（ａ１ｃ２−ａ２ｃ１）／（ａ１ｂ２−ａ２ｂ１）
}
main(){
  scanf("%d",&N);
  for(int i=0;i<N;i++){
    int xa,ya,xb,yb;
    scanf("%d %d %d %d",&xa,&ya,&xb,&yb);
    F fs = F(xa,ya,xb,yb);
    double nx = (double)min(xa,xb);
    double ny = (double)min(ya,yb);
    double xx = (double)max(xa,xb);
    double xy = (double)max(ya,yb);
    //    printf("y = %lfx + %lf\n",fs.a,fs.b);
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
      int xs,ys,xt,yt,o,l;
      scanf("%d %d %d %d %d %d",&xs,&ys,&xt,&yt,&o,&l);
      F f = F(xs,ys,xt,yt);
      //     printf("y = %lfx + %lf\n",f.a,f.b);
      double X,Y;
      Fcheck(f,fs,X,Y);
      //              printf("X=%lf : Y=%lf\n",X,Y);
      if(nx <= X && xx >= X && 
	  ny <= Y && xy >= Y){
	//		printf("input : k=%lf \n",sqrt((X-fs.xs)*(X-fs.xs)+(Y-fs.ys)*(Y-fs.ys)));
	s.push_back(PP(sqrt((X-fs.xs)*(X-fs.xs)+(Y-fs.ys)*(Y-fs.ys)),P(o,l)));
      }
    }
    sort(s.begin(),s.end());
    /*  
    for(int i=0;i<s.size();i++)
	  printf("xk=%lf, o=%d, l=%d\n",s[i].first,s[i].second.first,s[i].second.second);
    */
    int count=0;
    int res=0;
    int R;
    if(!s.empty()){
      if(!s[0].second.first){//他社
	if(s[0].second.second)//上
	  R = 0;
	else//地下
	  R = 1;
      }
      else{//自社
	if(s[0].second.second)//上
	  R = 1;
	else//下
	  R = 0;
      }
      for(int i=1;i<s.size();i++){
	if(s[i].second.second == R){//一致
	  if(!s[i].second.first){//他社
	    R = (R==1?0:1);
	    //	    printf("change R = %d → %d , %d番目\n", (R==1?0:1),R,i);
	    count++;
	  }
	}else{//不一致
	  if(s[i].second.first){//自社
	    R = (R==1?0:1);
	    //	    printf("change R = %d → %d , %d番目\n", (R==1?0:1),R,i);
	    count++;
	  }
	}
      }
      res = count;
      /*   count = 0;
	   if(!s[s.size()-1].second.first){//他社
	   if(s[0].second.second)//上
	   R = 0;
	   else//下
	   R = 1;
	   }
	   else{//自社
	   if(s[s.size()-1].second.second)//上
	   R = 1;
	   else//下
	   R = 0;
	   }
	   for(int i=s.size()-2;i>-1;i--){
	   if(s[i].second.second == R){//一致
	   if(!s[i].second.first){//他社
	   R = (R==1?0:1);
	   count++;
	   }
	   }else{//不一致
	   if(s[i].second.first){//自社
	   R = (R==1?0:1);
	   count++;
	   }
	   }
      
	   }*/
    }
    //    res = min(res,count);
    
    printf("%d\n",res);
    
    s.clear();
  }
}

Measured memory_bytes: 1192