#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
using namespace std;
typedef pair<double, double> P;
typedef complex<double> Point;
typedef pair<Point, Point> Line;
const double EPS = 1e-10;

// 内積 |a||b|cosθ
double dot(Point a, Point b){
	// return a.real() * b.real() + a.imag() * b.imag();
	return (conj(a) * b).real();
}
// 外積、符号付面積の２倍　|a||b|sinθ
double cross(Point a, Point b){
	// return a.real() * b.imag() - a.imag() * b.real();
	return (conj(a) * b).imag();
}
// 点の位置関係　2点a,bから見た点cの位置関係
int ccw(Point a, Point b, Point c){
    // COUNTER_CLOCKWISE
    if(cross(b - a, c - a) > EPS) return 1;
    // CLOCKWISE
    if(cross(b - a, c - a) < -EPS) return -1;
    // ONLINE_BACK
    if(dot(b - a, c - a) < -EPS) return 2;
    // ONLINE_FRONT
    if(abs(b - a) + EPS < abs(c - a)) return -2;
    // ON_SEGMENT
    return 0;
}
// 線分の交差判定
bool is_cross(Line a, Line b){
    if(ccw(a.first, a.second, b.first) * ccw(a.first, a.second, b.second) <= 0 && ccw(b.first, b.second, a.first) * ccw(b.first, b.second, a.second) <= 0){
        return true;
    }
    return false;
}
// 直線の交点
Point cross_point(Line a, Line b){
    double d1 = cross(b.second - b.first, b.first - a.first);
    double d2 = cross(b.second - b.first, a.second - a.first);
    return a.first + (a.second - a.first) * d1 / d2;
}

int main(){
    int ts;
    cin >> ts;
    rep(it, 0, ts){
        double xa, ya, xb, yb;
        cin >> xa >> ya >> xb >> yb;
        Point a = {xa, ya};
        Point b = {xb, yb};
        Line h = {a, b};
        int n;
        cin >> n;
        vector<Line> ls; 
        vector<int> owner(n), l(n);
        rep(i, 0, n){
            double xs, ys, xt, yt;
            cin >> xs >> ys >> xt >> yt >> owner[i] >> l[i];
            Point s = {xs, ys};
            Point t = {xt, yt};
            ls.push_back({s, t});
        }
        vector<pair<P, int> > crossPnt;
        rep(i, 0, n){
            if(is_cross(h, ls[i])){
                Point tmp = cross_point(h, ls[i]);
                int tmpl = l[i];
                if(owner[i] == 0) tmpl = (tmpl + 1) % 2;
                crossPnt.push_back({P(tmp.real(), tmp.imag()), tmpl});
            }
        }
        int ans = 0;
        sort(crossPnt.begin(), crossPnt.end());
        rep(i, 1, crossPnt.size()){
            if(crossPnt[i - 1].second != crossPnt[i].second){
                ans++;
            }
        }
        cout << ans << endl;
    }
}
