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

#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b-1);i>=a;i--)
using vi=vector<int>;
using vvi=vector<vi>;
using pii=pair<int,int>;

constexpr int INF = 1e9;

const double EPS = 1e-9;
const double PI = acos(-1);
#define equals(a, b) (fabs((a)-(b)) < EPS)
#define next(P, i) P[(i + 1) % P.size()]
#define prev(P, i) P[(i + P.size() - 1) % P.size()]

struct Point {
    double x, y;
    Point() : x(0.0), y(0.0) {}
    Point(double x, double y) : x(x), y(y) {}

    Point &operator+=(const Point &p) { x += p.x; y += p.y; return *this; }
    Point &operator-=(const Point &p) { x -= p.x; y -= p.y; return *this; }
    Point &operator*=(double a) { x *= a; y *= a; return *this; }
    Point &operator/=(double a) { x /= a; y /= a; return *this; }

    double abs() { return sqrt(norm()); }
    double norm() { return x * x + y * y; }
};
Point operator+(const Point &p1, const Point &p2) { return Point(p1) += p2; }
Point operator-(const Point &p1, const Point &p2) { return Point(p1) -= p2; }
Point operator*(const Point &p1, double a) { return Point(p1) *= a; }
Point operator/(const Point &p1, double a) { return Point(p1) /= a; }
bool operator==(const Point &p1, const Point &p2) { return equals(p1.x, p2.x) && equals(p1.y, p2.y); }
bool cmpx(const Point &p1, const Point  &p2) {
    return p1.x != p2.x ? p1.x < p2.x : p1.y < p2.y;
}
bool cmpy(const Point &p1, const Point &p2) {
    return p1.y != p2.y ? p1.y < p2.y : p1.x < p2.x;
}
bool operator<(const Point &p1, const Point &p2) {
    return cmpx(p1, p2);
}
bool operator>(const Point &p1, const Point &p2) { return p2 < p1; }

struct Vector : public Point {
    using Point::Point;
    Vector() {}
    Vector(const Point &P) { x = P.x; y = P.y; }
    Vector rotate(double rad) { return Vector(x * cos(rad) - y * sin(rad), x * sin(rad) + y * cos(rad)); }
    Vector unit() { return *this / abs(); }
};

struct Line {
    Point p1, p2;
    int o,l;
    Line() {}
    Line(Point p1, Point p2,int o,int l) : p1(p1), p2(p2),o(o),l(l) {}
};

struct Segment : public Line {
    using Line::Line;
    Segment() {}
    Segment(const Line &L) { p1 = L.p1; p2 = L.p2; }
    Vector vec() { return p2 - p1; }
};


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; }

enum { ONLINE_FRONT = -2, CLOCKWISE, ON_SEGMENT, COUNTER_CLOCKWISE, ONLINE_BACK };
int ccw(Point p0, Point p1, Point p2)  {
    Vector a = p1 - p0, b = p2 - p0;
    if (cross(a, b) > EPS) return COUNTER_CLOCKWISE;
    if (cross(a, b) < -EPS) return CLOCKWISE;
    if (dot(a, b) < -EPS) return ONLINE_BACK;
    if (a.norm() < b.norm()) return ONLINE_FRONT;
    return ON_SEGMENT;
}

bool intersect(Segment a, Segment b) {
    Point p1 = a.p1, p2 = a.p2,  p3 = b.p1, p4 = b.p2;
    return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <=  0 &&
            ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0);
}

Point get_cross_point(Segment a, Segment b) {
    assert(intersect(a, b));
    Vector base = b.p2 - b.p1;
    double area1 = abs(cross(base, a.p1 - b.p1));
    double area2 = abs(cross(base, a.p2 - b.p1));
    double t = area1 / (area1 + area2);
    return a.p1 + (a.p2 - a.p1) * t;
}

void solve(){
    double xa,ya,xb,yb;
    cin>>xa>>ya>>xb>>yb;
    Point s(xa,ya),t(xb,yb);
    int n;cin>>n;
    vector<double>xs(n),ys(n),xt(n),yt(n);
    vi o(n),l(n);
    Segment shinkan(s,t,-1,-1);
    vector<Segment>segs;
    rep(i,0,n){
        cin>>xs[i]>>ys[i]>>xt[i]>>yt[i];
        cin>>o[i]>>l[i];
        segs.emplace_back(Point(xs[i],ys[i]),Point(xt[i],yt[i]),o[i],l[i]);
    }
    vector<pair<Point,int>>touch;
    rep(i,0,n){
        if(intersect(shinkan,segs[i])){
            Point x=get_cross_point(shinkan,segs[i]);
            touch.emplace_back(x,i);
        }
    }
    sort(touch.begin(),touch.end());
    auto calc=[&](int state){
        int cnt=0;
        rep(i,0,touch.size()){
            Point p;
            int idx;
            tie(p,idx)=touch[i];
            if(o[idx]==1){
                cnt+=state!=l[idx];
                state=l[idx];
            }
            else{
                cnt+=state==l[idx];
                state=not l[idx];
            }
        }
        return cnt;
    };
    cout<<min(calc(0),calc(1))<<endl;

}

signed main() {
    int T;cin>>T;
    while(T--){
        solve();
    }
}




Measured memory_bytes: 3232