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

#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define EQ(a,b) (abs((a)-(b))<eps)

using namespace std;

typedef long double ld;
typedef complex<ld> P;
typedef vector<P> VP;
const ld eps = 1e-8, pi = acos(-1.0);


ld dot (P a, P b) { return real(conj(a) * b); }
ld cross (P a, P b) { return imag(conj(a) * b); }

class L{
public:
  P a, b;
  L (P aa, P bb) { a = aa; b = bb; }
  L (ld ax, ld ay, ld bx, ld by) { a = P(ax, ay); b = P(bx, by); }
};

bool isis_sp(L s, P p) {
  return abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps;
}

VP is_ll(L s, L t){
  VP v;
  P sv = s.b - s.a, tv = t.b - t.a;
  if (abs(cross(sv,tv)) > 0)
    v.push_back(s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv));
  return v;
}

int main() {
  int m;
  cin >> m;
  REP(i,m) {
    ld xa,ya,xb,yb;
    cin >> xa >> ya >> xb >> yb;
    P s(xa,ya), t(xb,yb);
    L nl(s,t);
    int n;
    cin >> n;
    vector<L> vl;
    VP crt;
    VP crb;
    REP(j,n){
      double xs, ys, xt, yt;
      int o,l;
      cin >> xs >> ys >> xt >> yt >> o >> l;
      P sj(xs,ys), tj(xt,yt);
      L lj(sj,tj);
      VP vp = is_ll(nl,lj);
      if(!vp.empty()){
        if(isis_sp(nl,vp[0]) && isis_sp(lj,vp[0])){
          if(o == l)
            crt.push_back(vp[0]);
          else
            crb.push_back(vp[0]);
        }
      }
    }
    sort(begin(crt),end(crt), [&](P l, P r){ return norm(l-s) < norm(r-s); });
    sort(begin(crb),end(crb), [&](P l, P r){ return norm(l-s) < norm(r-s); });
    int k=0;
    int cnt = 0;
    REP(j,crt.size()){
      bool b = false;;
      while(k<crb.size() && norm(crb[k]-s) < norm(crt[j]-s)) {
        ++k;
        b = true;
      }
      if(b) {
        if (j)
          cnt += 2;
        else
          cnt += 1;
      }
    }
    if (k < crb.size() && !crt.empty())
      cnt += 1;
    cout << cnt << endl;
  }
  return 0;
}