#include <bits/stdc++.h>

using namespace std;

using ld = long double;
using Point = complex<ld>;

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

constexpr ld eps=1e-9, pi=acos(-1.0);

namespace std {
  bool operator<(const Point &lhs, const Point &rhs) {
    if (lhs.real() < rhs.real() - eps) return true;
    if (lhs.real() > rhs.real() + eps) return false;
    return lhs.imag() < rhs.imag();
  }
}

namespace Geometry{
    bool eq(ld a, ld b){
        return abs(a-b) < eps;
    }
    //内積
    ld dot(Point a, Point b){
        return real(conj(a) * b);
    }
    //外積
    ld cross(Point a, Point b){
        return imag(conj(a) * b);
    }
    //3点の位置関係
    int ccw (Point a, Point b, Point c) {
        b -= a; c -= a;
        if (cross(b, c) > eps) return 1;//a,b,cで反時計周り
        if (cross(b, c) < -eps) return -1;//a,b,cで時計周り
        if (dot(b, c) < 0) return 2;//c,a,bで直線
        if (norm(b) < norm(c)) return -2;//a,b,cで直線
        return 0;//a,c,bで直線
    }

//====================================================
    Point inputPoint(){
        ld x, y;
        cin>>x>>y;
        return Point(x, y);
    }
    //2直線の交差判定
    bool isCrossed_ll(Line l, Line m){
        return !eq(cross(l.b-l.a, m.b-m.a), 0);
    }
    //直線と線分の交差判定
    bool isCrossed_ls(Line l, Line s){
        return isCrossed_ll(l, s) && 
            cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps;
    }
    //線分と線分の交差判定
    bool isCrossed_ss(Line s, Line 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;
    }
    //点が直線上にあるか
    bool isON_l(Line l, Point p){
        return abs(cross(l.b-p, l.a-p)) < eps;
    }
    //点が線分上にあるか
    bool isON_s(Line s, Point p){
        return abs(s.a-p)+abs(s.b-p)-abs(s.b-s.a) < eps;
    }

    //点から直線への垂線の足
    Point foot(Line l, Point p){
        ld t = dot(p-l.a,l.a-l.b)/norm(l.a-l.b);
        return l.a+t*(l.a-l.b);
    }
    //直線と直線の交点
    Point intersection_ll(Line l, Line m){
        Point lv=l.b-l.a, mv=m.b-m.a;
        assert(cross(lv,mv)!=0);//平行
        return l.a+lv*cross(mv,m.a-l.a)/cross(mv,lv);
    }
    //線分と線分の交点
    Point intersection_ss(Line s, Line t){
        assert(isCrossed_ll(s, t));
        return intersection_ll(s,t);
    }

    //点と直線の距離
    ld dist_lp(Line l, Point p) {
        return abs(p - foot(l, p));
    }
    //直線と直線の距離
    ld dist_ll(Line l, Line m){
        return isCrossed_ll(l, m)?0:dist_lp(l, m.a);
    }
    //直線と線分の距離
    ld dist_ls(Line l, Line s){
        return isCrossed_ls(l, s)?0:min(dist_lp(l, s.a), dist_lp(l, s.b));
    }
    //線分と点の距離
    ld dist_sp (Line s, Point p) {
        Point r = foot(s, p);
        return isON_s(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p));
    }
    //線分と線分の距離
    ld dist_ss (Line s, Line t) {
        if (isCrossed_ss(s, t)) return 0;
        return min({dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b)});
    }
};

struct train{
    Point p;
    bool lc;
    train(Point p, bool lc):p(p), lc(lc) {}
    bool operator < (const train &right)const{
        return p < right.p;
    }
};

int main(){
    int n;
    cin>>n;
    using namespace Geometry;
    for(int i=0;i<n;++i){
        Point a(inputPoint()), b(inputPoint());
        int m;
        cin>>m;
        vector<train>mp;
        for(int j=0;j<m;++j){
            bool o,l;
            Point s(inputPoint()), t(inputPoint());
            cin>>o>>l;
            bool loc;
            if(isCrossed_ss(Line(s, t),Line(a, b))){
                if(o)loc=l;
                else loc=(!l);
                mp.emplace_back(intersection_ss(Line(s, t),Line(a, b)),loc);
            }
        }
        sort(mp.begin(),mp.end());
        int ans=0;
        for(int j=1;j<mp.size();++j){
            if(mp[j].lc != mp[j-1].lc)++ans;
        }
        cout<<ans<<endl;
    }
}


Measured memory_bytes: 3312