#include<iostream>
#include<vector>
#include<algorithm>
#include<complex>

using namespace std;
#define EPS 1e-12
#define rep(i,n) for(int i=0;i<(int)n;i++)
const double PI = 4*atan(1.0);

typedef complex<double> C;
struct L : public vector<C>
{
  L(){}
  L(const C a, const C b){
    push_back(a); push_back(b);
  }
  L(double xa,double ya,double xb,double yb){
    push_back(C(xa,ya));push_back(C(xb,yb));
  }
};

struct mycp
{
  C cp;
  int ul;
  int st;
  bool operator<(const mycp& another) const {
    return cp.real()<another.cp.real();
  }
  mycp(const C cpp,int ull,int stt){
    cp = cpp; ul = ull; st = stt;
  }
};

bool eq(double a,double b)
{
  return (-EPS<a-b&&a-b<EPS);
}

bool eq(C c1,C c2)
{
  return (eq(c1.real(),c2.real()) && eq(c1.imag(),c2.imag()));
}

//?????¶??????sqrt
double Sqrt(double x)
{
    if(x<0) return 0;
    else    return sqrt(x);
}

//??£??????
C normalize(C c)
{
    return c / abs(c);
}

//?§????(rad)
double getarg(C a,C b){
    return arg(b*conj(a));
}

//??????
double cross(const C a, const C b)
{
    return imag(conj(a)*b);
}
//??????
double dot(const C a, const C b)
{
    return real(conj(a)*b);
}

int ccw(C a, C b, C 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;   //b--a--c on line
}

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;
}

C crosspointLL(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];
    }
    return m[0] + B / A * (m[1] - m[0]);
}


int main(){
  int dn;
  cin >>dn;
  rep(fdsa,dn){
    int xa,ya,xb,yb,n;
    cin >> xa >> ya >> xb >> yb >> n;
    if (xa>xb){swap(xa,xb);swap(ya,yb);}
    L newl(xa,ya,xb,yb);

    vector<int> o(n),l(n);
    vector<L> s,t;
    vector<int> s_ul,t_ul;

    rep(i,n){
      int xs,ys,xt,yt,o,l;
      cin >> xs >> ys >> xt >> yt >> o >> l;
      L line(xs,ys,xt,yt);
      if (o==1){
        s.push_back(line);
        s_ul.push_back(l);
      } else {
        t.push_back(line);
        t_ul.push_back(l);
      }
    }

    vector<mycp> mycps;
    rep(i,s.size()){
      if (intersectSS(newl,s[i])){
        mycp m(crosspointLL(newl,s[i]),s_ul[i],1);
        mycps.push_back(m);
      }
    }
    rep(i,t.size()){
      if (intersectSS(newl,t[i])){
        mycp m(crosspointLL(newl,t[i]),t_ul[i],0);
        mycps.push_back(m);
      }
    }
    sort(mycps.begin(),mycps.end());
    int minn = 1e9;
    rep(i,2){rep(j,2){
      int cur_ul_end = j;
      int cur_ul = i;
      int cnt = 0;
      for(auto s:mycps){
        if (s.st==1){
          if (s.ul!=cur_ul){
            cnt++;
            cur_ul = 1-cur_ul;
          }
        } else {
          if (s.ul==cur_ul){
            cnt++;
            cur_ul = 1-cur_ul;
          }
        }
      }
      if (cur_ul!=cur_ul_end)cnt++;
      if (cnt<minn)minn=cnt;
    }}

    cout << minn << endl;
  }
  

  return 0;
}

Measured memory_bytes: 3144