#include<bits/stdc++.h>
#define eps 1e-9
using namespace std;
typedef complex<double> point;
typedef pair<point, point> line;

//b??????a??????????????????
double dis(point a, point b){
  return abs(a - b);
}

//??????
double dot(point a, point b){
  return a.real()*b.real() + a.imag()*b.imag();
}

//??????
double cross(point a, point b){
  return imag(conj(a) * b);
}

//counter clock wise
int ccw(point a, point b, point c){
  if(cross(b - a, c - a) >  eps)return  1;//??????ab????????´
  if(cross(b - a, c - a) < -eps)return -1;//??????ab????????´
  if(dot(b - a, c - a) < -eps)return 2;//??´???ab??????a???????????????
  if(abs(b - a) + eps < abs(c - a))return -2;//??´???ab??????b???????????????
  return 0;//??´???ab???
}

double line_to_point_dis(line l, point p){
  return abs(cross(p - l.first, l.second - l.first))/
    abs(l.second - l.first);
}

double seg_to_point_dis(line l, point p){
  point a = l.first, b = l.second, c = p;
  if(dot(b - a, c - a) < eps)return abs(c - a);
  if(dot(a - b, c - b) < eps)return abs(c - b);
  return line_to_point_dis(l, p);
}

double seg_to_seg_dis(line a, line b){
  double res = seg_to_point_dis(a, b.first);
  res = min(res, seg_to_point_dis(a, b.second));
  res = min(res, seg_to_point_dis(b, a.first));
  res = min(res, seg_to_point_dis(b, a.second));
  return res;
}

bool is_cross(line a, line b){
  if(ccw(a.first, a.second, b.first) *
     ccw(a.first, a.second, b.second) <= 0 &&
     ccw(b.first, b.second, a.first) *
     ccw(b.first, b.second, a.second) <= 0){
    return true;
  }
  return false;
}

//point a, b, point c, d
point seg_to_seg_cross_point(line a, line c){
  if(not is_cross(a, c)) return point(1e9, 1e9);
  double xa = a.first.real(),  ya = a.first.imag();
  double xb = a.second.real(), yb = a.second.imag();
  double xc = c.first.real(),  yc = c.first.imag();
  double xd = c.second.real(), yd = c.second.imag();
  double s  = (ya - yc)*(xb - xa) + (yb - ya)*(xc - xa);
  double t  = (xb - xa)*(yd - yc) - (xd - xc)*(yb - ya);
  if(abs(t) < eps) return point(1e9, 1e9);
  return c.first + s*(c.second - c.first)/t;
}

typedef struct ColorLine{
  ColorLine(){};
  ColorLine(line _ll, int _o, int _l, point _cp):
    ll(_ll), o(_o), l(_l), cp(_cp){};
  void calc(point a){
    d = dis(a, cp);
  }
  line ll;
  int o, l;
  point cp;
  double d;
  bool operator<(const ColorLine &c){
    return d < c.d;
  }
}ColorLine;

int main(int argc, char *argv[]){
  point a, b;
  int n, roop;
  std::cin >> roop;
  for (int r = 0; r < roop; r++) {
    double x, y;
    int o, l;
    std::cin >> x >> y;
    a = point(x, y);
    std::cin >> x >> y >> n;
    b = point(x, y);
    line ll = line(a, b);
    vector<ColorLine> vl(n);
    for (int i = 0; i < n; i++) {
      std::cin >> x >> y;
      a = point(x, y);
      std::cin >> x >> y >> o >> l;
      b = point(x, y);
      point cp = seg_to_seg_cross_point(line(a, b), ll);
      vl[i] = ColorLine{line(a, b), o, l, cp};
      vl[i].calc(ll.first);
    }
    sort(vl.begin(), vl.end());
    bool up = (vl[0].o and vl[0].l) or (not vl[0].o and not vl[0].l);
    int ans = 0;
    for (int i = 1; i < n and vl[i].cp != point(1e9, 1e9); i++) {
      // std::cout << ((vl[i].o)?"b":"r") << " " << ((vl[i].l)?"o":"u") << std::endl;
      // std::cout << "bool:" << ((vl[i].o and vl[i].l) or (not vl[i].o and not vl[i].l)) << std::endl;
      // std::cout << up << std::endl;
      if(up != ((vl[i].o and vl[i].l) or (not vl[i].o and not vl[i].l))){
        up = not up, ans++;        
      }
      //std::cout << "up:" << up << std::endl;
    }
    std::cout << ans << std::endl;
  }
  return 0;
}

Measured memory_bytes: 3252