#include <cstdio>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

const double EPS = 1.0e-10;

class Point
{
public:
    double y, x;
    Point(){
        y = x = 0.0;
    }
    Point(double y0, double x0){
        y = y0;
        x = x0;
    }
    Point operator+(const Point& p) const{
        return Point(y + p.y, x + p.x);
    }
    Point operator-(const Point& p) const{
        return Point(y - p.y, x - p.x);
    }
    Point operator*(double a) const{
        return Point(y * a, x * a);
    }
    Point operator/(double a) const{
        return Point(y / a, x / a);
    }
    double length() const{
        return sqrt(y * y + x * x);
    }
    double dist(const Point& p) const{
        return sqrt(pow(y - p.y, 2) + pow(x - p.x, 2));
    }
    double dot(const Point& p) const{
        return y * p.y + x * p.x; // |a|*|b|*cosθ
    }
    double cross(const Point& p) const{
        return x * p.y - y * p.x; // |a|*|b|*sinθ
    }
};

class Line
{
public:
    double a, b, c; // a*x + b*y + c = 0
    Line(){
        a = c = 0.0;
        b = 1.0;
    }
    Line(double a0, double b0, double c0){
        a = a0;
        b = b0;
        c = c0;
    }
    Line(const Point& p1, const Point& p2){ // 2点を結ぶ直線
        double dy = p2.y - p1.y;
        double dx = p2.x - p1.x;
        if(abs(dy / dx) < EPS){
            a = 0.0;
            b = 1.0;
            c = -p1.y;
        }else if(abs(dx / dy) < EPS){
            a = 1.0;
            b = 0.0;
            c = -p1.x;
        }else{
            a = - dy / dx;
            b = 1.0;
            c = dy / dx * p1.x - p1.y;
        }
    }
};

bool segmentsCollide(const Point& a1, const Point& a2, const Point& b1, const Point& b2)
{
    if(min(a1.x, a2.x) > max(b1.x, b2.x) + EPS || min(b1.x, b2.x) > max(a1.x, a2.x) + EPS || min(a1.y, a2.y) > max(b1.y, b2.y) + EPS || min(b1.y, b2.y) > max(a1.y, a2.y) + EPS)
        return false;

    return ((a2-a1).cross(b1-a1) * (a2-a1).cross(b2-a1) <= EPS) && ((b2-b1).cross(a1-b1) * (long long)(b2-b1).cross(a2-b1) <= EPS);
}

bool linesIntersection(const Line& l1, const Line& l2, Point& intersection)
{
    double s = l1.a*l2.b - l2.a*l1.b;
    if(abs(s) < EPS)
        return false;

    intersection = Point(l2.a*l1.c - l1.a*l2.c, l1.b*l2.c - l2.b*l1.c) / s;
    return true;
}

int main()
{
    int d;
    cin >> d;

    while(--d >= 0){
        Point a, b;
        cin >> a.x >> a.y >> b.x >> b.y;

        int n;
        cin >> n;
        vector<pair<pair<double, double>, int> > v;
        for(int i=0; i<n; ++i){
            Point s, t;
            int o, l;
            cin >> s.x >> s.y >> t.x >> t.y >> o >> l;

            if(segmentsCollide(a, b, s, t)){
                Point p;
                linesIntersection(Line(a, b), Line(s, t), p);
                v.push_back(make_pair(make_pair(p.x, p.y), o ^ l));
            }
        }

        int m = v.size();
        if(abs(b.y-a.y) > abs(b.x-b.y)){
            for(int i=0; i<m; ++i)
                swap(v[i].first.first, v[i].first.second);
        }
        sort(v.begin(), v.end());

        int ret = 0;
        for(int i=1; i<m; ++i){
            if(v[i].second != v[i-1].second)
                ++ ret;
        }
        cout << ret << endl;
    }

    return 0;
}

Measured memory_bytes: 1240