#include <iostream>
#include <vector>
#include <complex>
#include <algorithm>

using namespace std;
typedef complex<double> point;

#define all(v) v.begin(),v.end()
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

namespace std{
    bool operator < (const point &lhs,const point &rhs){
        if(real(lhs) == real(rhs)){
            return imag(lhs) < imag(rhs);
        }else{
            return real(lhs) < real(rhs);
        }
    }
}
const double EPS = 1e-9;

// 内積 (dot product) : a・b = |a||b|cosΘ
double dot(point a,point b){
    return real(conj(a)*b);
}

// 外積 (cross product) : |a×b| = |a||b|sinΘ
double cross(point a,point b){
    return imag(conj(a)*b);
}

// a1,a2を端点とする線分(la)とb1,b2を端点(lb)とする線分の交差判定
bool is_intersected_linesegment(point a1,point a2,point b1,point b2){
    if(max(a1.real(),a2.real()) + EPS < min(b1.real(),b2.real())) return false;
    if(max(b1.real(),b2.real()) + EPS < min(a1.real(),a2.real())) return false;
    if(max(a1.imag(),a2.imag()) + EPS < min(b1.imag(),b2.imag())) return false;
    if(max(b1.imag(),b2.imag()) + EPS < min(a1.imag(),a2.imag())) return false;
    return (cross(a2-a1,b1-a1)*cross(a2-a1,b2-a1) < EPS) and
           (cross(b2-b1,a1-b1)*cross(b2-b1,a2-b1) < EPS);

}

// a1,a2を端点とする線分(la)とb1,b2を端点とする線分(lb)の交点計算
point intersection_point_linesegment(point a1,point a2,point b1,point b2) {
    if(a1 == b1 or a1 == b2) return a1;
    if(a2 == b1 or a2 == b2) return a2;
    point b = b2-b1;
    double d1 = abs(cross(b, a1-b1));
    double d2 = abs(cross(b, a2-b1));
    double t = d1 / (d1 + d2);
    return a1 + (a2-a1) * t;
}
struct CrossPoint{
    point p;
    char is_under;
    CrossPoint(point _p,char _is_under) :
        p(_p),is_under(_is_under) {}
};

int solve(const point& s,const point& t,const vector<vector<point>>& lines,
          const vector<char>& under){

    int ret = 1000000000;
    vector<CrossPoint> cross_point;

    for(size_t i=0;i<lines.size();i++){
        point a = lines[i][0];
        point b = lines[i][1];
        if(is_intersected_linesegment(s,t,a,b)){
            point p = intersection_point_linesegment(s,t,a,b);

            cross_point.push_back(CrossPoint(p,under[i]));
        }
    }

    sort(all(cross_point),[s](const CrossPoint& lhs,const CrossPoint& rhs){
                                   return abs(lhs.p-s) < abs(rhs.p-s);
                             });

    for(char istate=0;istate<2;istate++){
        int cnt = 0;
        char state = istate;
        for(size_t i=0;i<cross_point.size();i++){
            char u = cross_point[i].is_under;

            if(u != state){
                state = 1 - state;
                cnt++;
            }
        }
        ret = min(ret,cnt);
    }
    return ret;
}

int main(){
    int test_case;
    cin >> test_case;
    for(int test=0;test<test_case;test++){
        int xa,ya,xb,yb;
        int n;
        cin >> xa >> ya >> xb >> yb;
        cin >> n;

        point a(xa,ya),b(xb,yb);

        vector<vector<point>> lines(n);
        vector<char> underground(n);

        for(int i=0;i<n;i++){
            int xs,ys,xt,yt,o,l;
            cin >> xs >> ys >> xt >> yt >> o >> l;
            if(o == 0) l = 1 - l;
            // must cross.
            underground[i]= l;
            lines[i].push_back(point(xs,ys));
            lines[i].push_back(point(xt,yt));
        }
        cout << solve(a,b,lines,underground) << endl;
    }
    return 0;
}