#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
 
#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
 
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

const double EPS = 1e-10;

double intersect(double xa, double ya, double xb, double yb, double xc, double yc, double xd, double yd){
    double g =  (xb - xa)*(yd - yc) - (yb - ya)*(xd - xc);
    if (fabs(g) < EPS) return -1;
    double r = ((yd - yc)*(xc - xa) - (xd - xc)*(yc - ya)) / g;
    double s = ((yb - ya)*(xc - xa) - (xb - xa)*(yc - ya)) / g;
    if (r < 0 || r > 1 || s < 0 || s > 1) return -1;
    return r;
}

int calc(){
    double xa, ya, xb, yb;
    cin >> xa >> ya >> xb >> yb;
    int n;
    cin >> n;
    vector<pair<double, int> > a;
    while (n--){
        double xs, ys, xt, yt;
        int p, q;
        cin >> xs >> ys >> xt >> yt >> p >> q;
        double r = intersect(xa, ya, xb, yb, xs, ys, xt, yt);
        if (r > 0 && r < 1){
            a.push_back(make_pair(r, p^q));
        }
    }
    sort(a.begin(), a.end());
    if (a.size() <= 1) return 0;
    int ans = 0;
    REP(i,a.size()-1){
        if (a[i].second != a[i+1].second) ans++;
    }
    return ans;
}

int main() {
    int m;
    cin >> m;
    while (m--){
        cout << calc() << endl;
    }

    return 0;
}