import std.stdio;
import std.c.stdio;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.regex;
import std.typecons;

const double EPS = 1e-9;

struct Point {
    double x, y;
    Point opBinary(string op)(in Point a) const {
        static if (op == "+" || op == "-") {
            return Point(mixin("x" ~ op ~ "a.x"),
                         mixin("y" ~ op ~ "a.y"));
        }
    }
    Point opBinary(string op)(double a) const {
        static if (op == "*" || op == "/") {
            return Point(mixin("x" ~ op ~ "a"),
                         mixin("y" ~ op ~ "a"));
        }
    }
}
double dot(in Point a, in Point b) {
    return a.x * b.x + a.y * b.y;
}
double cross(in Point a, in Point b) {
    return a.x * b.y - a.y * b.x;
}
double norm(in Point a) {
    return sqrt(a.x * a.x + a.y * a.y);
}
int CCW(Point a, Point b, Point c) {
    b = b - a; c = c - a;
    if (cross(b, c) > EPS) return +1;
    if (cross(b, c) < -EPS) return -1;
    if (dot(b, c) < 0) return 2;
    if (b.norm < c.norm) return -2;
    return 0;
}

struct Segment {
    Point a, b;
}
bool intersect(in Segment s, in Segment t) {
    return CCW(s.a, s.b, t.a) * CCW(s.a, s.b, t.b) <= 0 &&
        CCW(t.a, t.b, s.a) * CCW(t.a, t.b, s.b) <= 0;
}
Point intersection(in Segment s, in Segment t) {
    double d = cross(t.b - t.a, s.b - s.a);
    assert(abs(d) >= EPS);
    return s.a + (s.b - s.a) * (cross(t.b - t.a, t.b - s.a) / d);
}

Segment S;
Segment[] T;
int N;
int[] W;
void input() {
    scanf("%lf %lf %lf %lf\n", &S.a.x, &S.a.y, &S.b.x, &S.b.y);
    scanf("%d\n", &N);
    T = new Segment[N];
    W = new int[N];
    foreach (i; 0 .. N) {
        scanf("%lf %lf %lf %lf", &T[i].a.x, &T[i].a.y, &T[i].b.x, &T[i].b.y);
        int o, l;
        scanf("%d %d\n", &o, &l);
        W[i] = (o ^ l);
    }
}

void solve() {
    alias Tuple!(Point, int) Z;
    Z[] ips;
    foreach (i; 0 .. N) {
        if (S.intersect(T[i])) {
            ips ~= Z(S.intersection(T[i]), W[i]);
        }
    }
    ips.sort!"a[0].x < b[0].x";
    int ans = 0;
    if (ips.length <= 1) {
        ans = 0;
    } else {
        int prev = ips[0][1];
        foreach (i; ips[1 .. $].map!("a[1]")) {
            if (i == prev) continue;
            prev = i;
            ans++;
        }
    }
    ans.writeln;
}
 
void main() {
    int T; scanf("%d\n", &T);
    foreach (t; 0 .. T) {
        input;
        solve;
    }
}