#include <stdio.h>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
#include <cctype>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
//constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;
const long double PI = acos((long double)(-1));

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;

using Real = double;
using Point = complex<Real>;
const Real EPS = 1e-8;

struct Circle {
    Point p;
    Real r;
    Circle() :p(Point(0, 0)), r(0) {}
    Circle(Point _p, Real _r) :p(_p), r(_r) {}
};

using Points = vector<Point>;
using Circles = vector<Circle>;

bool eq(Real a, Real b) { return abs(a - b) < EPS; }

int intersect(Circle c1, Circle c2) {
    if (c1.r < c2.r) swap(c1, c2);
    Real d = abs(c1.p - c2.p);
    if (c1.r + c2.r < d) return 4;
    if (eq(c1.r + c2.r, d)) return 3;
    if (c1.r - c2.r < d) return 2;
    if (eq(c1.r - c2.r, d)) return 1;
    return 0;
}

pair<Point, Point> crosspoint(Circle& c1, Circle& c2) {
    Real d = abs(c1.p - c2.p);
    Real a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));
    Real t = arg(c2.p - c1.p);
    Point p1 = c1.p + polar(c1.r, t + a);
    Point p2 = c1.p + polar(c1.r, t - a);
    return { p1, p2 };
}

struct Line {
    Point a, b;

    Line() = default;

    Line(Point a, Point b) : a(a), b(b) {}

    Line(Real A, Real B, Real C) // Ax + By = C
    {
        if (eq(A, 0)) a = Point(0, C / B), b = Point(1, C / B);
        else if (eq(B, 0)) b = Point(C / A, 0), b = Point(C / A, 1);
        else a = Point(0, C / B), b = Point(C / A, 0);
    }
};

struct Segment : Line {
    Segment() = default;

    Segment(Point a, Point b) : Line(a, b) {}
};



Real cross(const Point& a, const Point& b) {
    return real(a) * imag(b) - imag(a) * real(b);
}

Real dot(const Point& a, const Point& b) {
    return real(a) * real(b) + imag(a) * imag(b);
}

int ccw(const Point& a, Point b, Point c) {
    b = b - a, c = c - a;
    if (cross(b, c) > EPS) return +1;  // "COUNTER_CLOCKWISE"
    if (cross(b, c) < -EPS) return -1; // "CLOCKWISE"
    if (dot(b, c) < 0) return +2;      // "ONLINE_BACK"
    if (norm(b) < norm(c)) return -2;  // "ONLINE_FRONT"
    return 0;                         // "ON_SEGMENT"
}

bool intersect(const Segment& s, const 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 crosspoint(const Line& l, const Line& m) {
    Real A = cross(l.b - l.a, m.b - m.a);
    Real B = cross(l.b - l.a, l.b - m.a);
    if (eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;
    return m.a + (m.b - m.a) * B / A;
}

bool intersect(const Line& l, const Segment& s) {
    return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;
}



Point crosspoint(const Segment& l, const Segment& m) {
    return crosspoint(Line(l), Line(m));
}

void solve() {
    Real xa, ya, xb, yb; cin >> xa >> ya >> xb >> yb;
    Segment seg(Point(xa, ya), Point(xb, yb));
    vector<Points> vp(2);
    if (seg.a.real() > seg.b.real()) swap(seg.a, seg.b);
    int n; cin >> n;
    for (int i = 0; i < n; i++) {
        int xa, ya, xb, yb, f, t;
        cin >> xa >> ya >> xb >> yb >> f >> t;
        Segment s(Point(xa, ya), Point(xb, yb));
        if (intersect(s, seg)) {
            if (f) {
                if (t) vp[0].push_back(crosspoint(s, seg));
                else vp[1].push_back(crosspoint(s, seg));
            }
            else {
                if (t) vp[1].push_back(crosspoint(s, seg));
                else vp[0].push_back(crosspoint(s, seg));
            }
        }
    }
    sort(vp[0].begin(), vp[0].end(), [&](auto a, auto b) { return a.real() < b.real(); });
    sort(vp[1].begin(), vp[1].end(), [&](auto a, auto b) { return a.real() < b.real(); });
    int d = 0, u = 0;
    int pre = -1;
    int res = 0;

    while (d < vp[0].size() or u < vp[1].size()) {
        if (d < vp[0].size() and (vp[1].size() == u or vp[0][d].real() < vp[1][u].real())) {
            if (pre == 1) res++;
            pre = 0;
            d++;
        }
        else {
            if (pre == 0) res++;
            pre = 1;
            u++;
        }
    }
    cout << res << endl;
}

int main()
{

	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	int q; cin >> q; while (q--) solve();
	
}



Measured memory_bytes: 3348