#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;

/**************************************************************/

#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)

// ???
class Point {
public:
	double x, y;
    int affi;
    bool isGround;

	Point(double x = 0.0, double y = 0.0) : x(x), y(y) {}

	Point operator + (Point p) {
		return Point(x + p.x, y + p.y);
	}
	Point operator - (Point p) {
		return Point(x - p.x, y - p.y);
	}
	Point operator * (double c) {
		return Point(c * x, c * y);
	}
	Point operator / (double c) {
		return Point(x / c, y / c);
	}
	bool operator < (const Point &p) {
		return (x != p.x ? x < p.x : y < p.y);
	}
	bool operator == (const Point &p) {
		return (fabs(x - p.x) < EPS && fabs(y - p.y) < EPS);
	}
	bool operator != (const Point &p) {
		return (fabs(x - p.x) > EPS || fabs(y - p.y) > EPS);
	}

	// ?????????
	double norm() {
		return (x * x + y * y);
	}
	// ??§??????
	double abs() {
		return sqrt(norm());
	}
};

// ????????????
typedef Point Vector;

// ??????
double dot(Vector a, Vector b) {
	return (a.x * b.x + a.y * b.y);
}

// ??????
double cross(Vector a, Vector b) {
	return (a.x * b.y - a.y * b.x);
}

// ?????????
double norm(Vector a) {
	return (a.x * a.x + a.y * a.y);
}

// ??§??????
double abs(Vector a) {
	return sqrt(norm(a));
}

// ??????
class Segment {
public:
	Point p1, p2;

	Segment(Point p1 = Point(), Point p2 = Point()) : p1(p1), p2(p2) {}
};

// ??´???
typedef Segment Line;

const int COUNTER_CLOCKWISE = 1;
const int CLOCKWISE = -1;
const int ONLINE_BACK = 2;
const int ONLINE_FRONT = -2;
const int ON_SEGMENT = 0;

int ccw(Point p0, Point p1, Point p2) {
	Vector a = p1 - p0;
	Vector b = p2 - p0;

	// ???????¨???????
	if (cross(a, b) > EPS) return COUNTER_CLOCKWISE;
	// ????¨???????
	if (cross(a, b) < -EPS) return CLOCKWISE;
	// p1??¨p2???p0???????????§??´??????
	if (dot(a, b) < -EPS) return ONLINE_BACK;
	// p1??¨p2?????´??????????????????p0?????????????????????????????? abs(p2 - p0) > abs(p1 - p0)
	if (a.norm() < b.norm()) return ONLINE_FRONT;
	// ??????????§????????????? abs(p1 - p0) >= abs(p2 - p0)
	return ON_SEGMENT;
}

// ??????ab??¨??????cd???????????????
bool intersect(Point a, Point b, Point c, Point d) {
	return (ccw(a, b, c) * ccw(a, b, d) <= 0 && ccw(c, d, a) * ccw(c, d, b) <= 0);
}

bool intersect(Segment s1, Segment s2) {
	return intersect(s1.p1, s1.p2, s2.p1, s2.p2);
}

// ??´???s??¨???p????????¢
double distL(Line l, Point p) {
	return abs(cross(l.p2 - l.p1, p - l.p1) / abs(l.p2 - l.p1));
}

// ??????s??¨???p????????¢
double distS(Segment s, Point p) {
	if (dot(s.p2 - s.p1, p - s.p1) < 0.0) return abs(p - s.p1);
	if (dot(s.p1 - s.p2, p - s.p2) < 0.0) return abs(p - s.p2);
	return distL(s, p);
}

// ??????s1??¨??????s2????????¢
double distSS(Segment s1, Segment s2) {
	if (intersect(s1, s2)) return 0.0;
	return min(min(distS(s1, s2.p1), distS(s1, s2.p2)), min(distS(s2, s1.p1), distS(s2, s1.p2)));
}

// ??????s1, s2?????????
Point crossPoint(Segment s1, Segment s2) {
    Vector base = s2.p2 - s2.p1;
    double d1 = abs(cross(base, s1.p1 - s2.p1));
    double d2 = abs(cross(base, s1.p2 - s2.p1));
    double t = d1 / (d1 + d2);
    return s1.p1 + (s1.p2 - s1.p1) * t;
}

/***********************************************************************/

int d, n, sx, sy, gx, gy, o, l, c;
vector<Point> p;
Segment line, tmp;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> d;
    while (d--) {
        cin >> line.p1.x >> line.p1.y >> line.p2.x >> line.p2.y;
        cin >> n;
        c = 0;
        for (int i = 0; i < n; ++i) {
            cin >> sx >> sy >> gx >> gy >> o >> l;
            tmp = Segment(Point(sx, sy), Point(gx, gy));
            if (intersect(line, tmp)) {
                p.push_back(crossPoint(line, tmp));
                p[c].affi = o;
                p[c].isGround = bool(l);
                c++;
				//cout << p[c-1].x << " " << p[c-1].y << endl;
            }
        }

        sort(p.begin(), p.end());

        int size_ = p.size(), ans = 0, pos = -1;

        for (int i = 0; i < size_; ++i) {
            if (pos == -1) {
                if (p[i].affi == 1) pos = p[i].isGround;
				else pos = !p[i].isGround;
            }
            else {
                if (p[i].affi == 1 && p[i].isGround != pos) {
                    pos = p[i].isGround;
                    ans++;
                }
                else if (p[i].affi == 0 && p[i].isGround == pos) {
                    pos = !p[i].isGround;
                    ans++;
                }
            }
        }

        cout << ans << endl;

        ans = 0;
        p.clear();
    }

    return 0;
}

Measured memory_bytes: 3268