#include<bits/stdc++.h>
using namespace std;

using i64 = int64_t;

const double EPS = 1e-8;
const double INF = 1e12;
const double PI = acos(-1);

using Point = complex<double>;
using Polygon = vector<Point>;

inline int sgn(const double &a) { return (a < -EPS ? -1 : (a > EPS ? 1 : 0)); }

inline bool eq(const Point &a, const Point &b) { return abs(a - b) < EPS; }

namespace std {
inline bool operator<(const Point &a, const Point &b) {
    if (sgn(a.real() - b.real()))
        return sgn(a.real() - b.real()) < 0;
    return sgn(a.imag() - b.imag()) < 0;
}
} // namespace std

double dot(const Point &a, const Point &b) { return real(conj(a) * b); }

double det(const Point &a, const Point &b) { return imag(conj(a) * b); }

struct Line {
    Point p1, p2;

    Line(Point p1 = Point(), Point p2 = Point()) : p1(p1), p2(p2) {}

    bool operator<(const Line &rhs) const {
        if (eq(p2, rhs.p2))
            return p1 < rhs.p1;
        return p2 < rhs.p2;
    }
    bool operator==(const Line &rhs) const {
        return (eq(p1, rhs.p1) && eq(p2, rhs.p2)) ||
               (eq(p1, rhs.p2) && eq(p2, rhs.p1));
    }
};

inline Point curr(const Polygon &p, const int &i) { return p[i % p.size()]; }

inline Point next(const Polygon &p, const int &i) {
    return p[(i + 1) % p.size()];
}

inline Point prev(const Polygon &p, const int &i) {
    return p[(i + p.size() - 1) % p.size()];
}

enum CCW {
    ONLINE_FRONT = -2,
    CLOCKWISE,
    ON_SEGMENT,
    COUNTER_CLOCKWISE,
    ONLINE_BACK
};

int ccw(Point p0, Point p1, Point p2) {
    Point a = p1 - p0;
    Point b = p2 - p0;
    if (sgn(det(a, b)) == 1)
        return COUNTER_CLOCKWISE;
    if (sgn(det(a, b)) == -1)
        return CLOCKWISE;
    if (sgn(dot(a, b)) == -1)
        return ONLINE_BACK;
    if (sgn(norm(b) - norm(a)) == 1)
        return ONLINE_FRONT;
    return ON_SEGMENT;
}

bool intersectLS(Line a, Line b, bool segflag) {
    if (segflag)
        return (ccw(a.p1, a.p2, b.p1) * ccw(a.p1, a.p2, b.p2) <= 0) &&
               (ccw(b.p1, b.p2, a.p1) * ccw(b.p1, b.p2, a.p2) <= 0);
    else
        return (sgn(det(a.p2 - a.p1, b.p1 - a.p1) *
                    det(a.p2 - a.p1, b.p2 - a.p1)) <= 0) &&
               (sgn(det(b.p2 - b.p1, a.p1 - b.p1) *
                    det(b.p2 - b.p1, a.p2 - b.p1)) <= 0);
}

Point intersection(Line a, Line b) {
    Point v1 = a.p2 - a.p1, v2 = b.p2 - b.p1;
    return a.p1 + v1 * det(v2, b.p1 - a.p1) / det(v2, v1);
}

int main(){
    int T;
    cin >> T;
    for(int t=0;t<T;++t){
        double xa, ya, xb, yb;
        cin >> xa >> ya >> xb >> yb;
        Line new_rail(Point(xa, ya), Point(xb, yb));
        int n;
        cin >> n;
        vector<pair<Point, int>> intersections;
        for(int j=0;j<n;++j){
            double xs, ys, xt, yt;
            int o, i;
            cin >> xs >> ys >> xt >> yt >> o >> i;
            i ^= o;
            Line rail(Point(xs, ys), Point(xt, yt));
            if(!intersectLS(new_rail, rail, true))continue;
            intersections.emplace_back(intersection(new_rail, rail), i);
        }
        if(intersections.size() == 0){
            cout << 0 << endl;
            continue;
        }
        sort(intersections.begin(), intersections.end());
        int ans = 0, f = intersections[0].second;
        for(int i=1;i<intersections.size();++i){
            if(f != intersections[i].second){
                ans++;
                f = intersections[i].second;
            }
        }
        cout << ans << endl;
    }

    return 0;
}



Measured memory_bytes: 3156