#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <complex>
#include <math.h>
#include <assert.h>

using namespace std;

typedef complex<double> Complex;
static const double E = 0.0000001;

struct Point {
  Complex p;
  int o;
  int h;
  Point() {;}
  Point(Complex p1, int o1, int h1) : p(p1), o(o1), h(h1) {;}
  bool operator<(const Point &rhs) const {
    return p.real() + p.imag() < rhs.p.real() + rhs.p.imag();
  }
};

struct Line {
  Complex p1;
  Complex p2;
  int o;
  int h;
  Line() {;}
  Line(double x1, double y1, double x2, double y2, int o1, int h1) : p1(x1, y1), p2(x2, y2), o(o1), h(h1) {;}
};

struct Comp {
  bool operator()(const Complex &rhs, const Complex &lhs) const { return rhs.real() < lhs.real(); }
};


inline void line_to_equation(const Complex s1, const Complex e1, double *a, double *b, double *c);
inline double ccw(const Complex vect1, const Complex vect2);
inline double ccw3(const Complex s, const Complex e1, const Complex e2);
inline double distance_point_line(const Complex p1, const double r1, const Complex s1, const Complex e1);
inline double distance_point_segment(const Complex p1, const Complex s1, const Complex e1);
inline double distance_line_line(const Complex s1, const Complex e1, const Complex s2, const Complex e2);
inline double distance_segment_segment(const Complex s1, const Complex e1, const Complex s2, const  Complex e2);
inline double distance_line_segment(const Complex s1, const Complex e1, const Complex s2, const Complex e2);
inline bool collide_line_line(const Complex s1, const Complex e1, const Complex s2, const Complex e2);
inline bool collide_rect_rect(const Complex p11, const Complex p12, const Complex p21, const Complex p22);
inline bool collide_segment_segment(const Complex s1, const Complex e1, const Complex s2, const Complex e2);
inline bool collide_circle_circle(const Complex p1, const double r1, const Complex p2, const double r2);
inline bool collide_rect_point(const Complex p11, const Complex p12, const Complex p2);
inline bool collide_rect_line(const Complex p11, const Complex p12, const Complex s1, const Complex e1);
inline bool collide_rect_segment(const Complex p11, const Complex p12, const Complex s1, const Complex e1);
inline bool collide_line_point(const Complex s1, const Complex e1, const Complex p1);
inline bool collide_line_segment(const Complex s1, const Complex e1, const Complex s2, const Complex e2);
inline bool collide_segment_point(const Complex s1, const Complex e1, const Complex p1);
inline bool collide_circle_point(const Complex p1, const double r1, const Complex p2);
inline bool collide_circle_line(const Complex p1, const double r1, const Complex s1, const Complex e1);
inline bool collide_circle_segment(const Complex p1, const double r1, const Complex s1, Complex e1);
inline Complex intersection_line_line(const Complex s1, const Complex e1, const Complex s2, Complex e2);
inline void intersection_circle_circle(const Complex p1, const double r1, const Complex p2, const double r2, int *size, Complex *ret1, Complex *ret2);
inline void intersection_circle_line(const Complex p1, const double r1, const Complex s1, const Complex e1, int *size, Complex *ret1, Complex *ret2);
inline void intersection_circle_segment(const Complex p1, const double r1, const Complex s1, const Complex e1, int *size, Complex *ret1, Complex *ret2);






template<class T>
inline T square(const T &x) { return x * x; }

// ax+by+c=0
inline void line_to_equation(const Complex s1, const Complex e1, double *a, double *b, double *c) {
  assert(s1 != e1);
  assert(a != NULL && b != NULL && c != NULL);
  Complex vect = e1 - s1;
  *a = vect.imag();
  *b = -vect.real();
  *c = s1.imag() * vect.real() - s1.real() * vect.imag();
}

inline double ccw(const Complex vect1, const Complex vect2) {
  return vect1.real() * vect2.imag() - vect2.real() * vect1.imag();
}

inline double ccw3(const Complex s, const Complex e1, const Complex e2) {
  return ccw(e1 - s, e2 - s);
}


inline bool collide_line_point(const Complex s1, const Complex e1, const Complex p1) {
  double a, b, c;
  line_to_equation(s1, e1, &a, &b, &c);
  return fabs(a * p1.real() + b * p1.imag() + c ) < E;
}

inline bool collide_line_line(const Complex s1, const Complex e1, const Complex s2, const Complex e2) {
  Complex vect1 = e1 - s1;
  Complex vect2 = e2 - s2;
  assert(abs(vect1) > E && abs(vect2) > E);
  if (fabs(vect1.real() * vect2.imag() - vect2.real() * vect1.imag()) >= E) { return true; }
  return collide_line_point(s1, e1, s2);

}

inline bool collide_rect_rect(const Complex p11, const Complex p12, const Complex p21, const Complex p22) {
  return min(p11.real(), p12.real()) <= max(p21.real(), p22.real()) &&
    max(p11.real(), p12.real()) >= min(p21.real(), p22.real()) &&
    min(p11.imag(), p12.imag()) <= max(p21.imag(), p22.imag()) &&
    max(p11.imag(), p12.imag()) >= min(p21.imag(), p22.imag());
}

inline bool collide_segment_segment(const Complex s1, const Complex e1, const Complex s2, const Complex e2) {
  if (!collide_rect_rect(s1, e1, s2, e2)) { return false; }
  if (ccw3(s1, e1, s2) * ccw3(s1, e1, e2) > 0 ||
      ccw3(s2, e2, s1) * ccw3(s2, e2, e1) > 0) { return false; }
  return collide_line_line(s1, e1, s2, e2);
}

// include rect inside
inline bool collide_rect_point(const Complex p11, const Complex p12, const Complex p2) {
  return collide_rect_rect(p11, p12, p2, p2);
}

inline bool collide_line_segment(const Complex s1, const Complex e1, const Complex s2, const Complex e2) {
  double a, b, c;
  line_to_equation(s1, e1, &a, &b, &c);
  return (a * s2.real() + b * s2.imag() + c) * (a * e2.real() + b * e2.imag() + c) <= 0;
}

inline Complex intersection_line_line(const Complex s1, const Complex e1, const Complex s2, Complex e2) {
  double a, b, c, d, e, f;
  line_to_equation(s1, e1, &a, &b, &c);
  line_to_equation(s2, e2, &d, &e, &f);
  assert(fabs(a * e - b * d) > E && fabs(b * d - a * e) > E);
  return Complex(-(b * f - c * e) / (a * e - b * d), -(a * f - c * d) / (b * d - a * e));
}


inline bool check_cross(const Line &l1, const Line &l2) {
  return collide_segment_segment(l1.p1, l1.p2, l2.p1, l2.p2);
}

inline Complex cross(const Line &l1, const Line &l2) {
  return intersection_line_line(l1.p1, l1.p2, l2.p1, l2.p2);
}

Point cross_point[101];
Line line[100];

int main() {
  int test;
  scanf("%d", &test);
  while (test--) {
    Line sinkansen;
    int a, b, c, d;
    scanf("%d %d %d %d", &a, &b, &c, &d);
    sinkansen = Line(a, b, c, d, 0, 0);
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      int o, h;
      scanf("%d %d %d %d %d %d", &a, &b, &c, &d, &o, &h);
      line[i] = Line(a, b, c, d, o, h);
    }
    int count = 0;
    for (int i = 0; i < n; i++) {
      if (!check_cross(sinkansen, line[i])) { continue; }
      cross_point[count] = Point(cross(line[i], sinkansen), line[i].o, line[i].h);
      count++;
    }
    sort(cross_point, cross_point + count);
    int ans = 0;
    int prev;
    if (cross_point[0].o == 1) {
      prev = cross_point[0].h;
    } else {
      prev = 1 - cross_point[0].h;
    }
    for (int i = 1; i < count; i++) {
    if (cross_point[i].o == 1) {
      if (cross_point[i].h != prev) { ans++; }
      prev = cross_point[i].h;
    } else {
      if (cross_point[i].h == prev) { ans++; }
      prev = 1 - cross_point[i].h;
    }
    }
    printf("%d\n", ans);
  }
}

Measured memory_bytes: 908