#include <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <bitset>
#include <complex>
#include <unistd.h>
#include <cassert>
#include <cctype>
#include <random>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-11;

double cross_product (xy_t a, xy_t b) {
    return (conj(a) * b).imag();
}

bool seginter (xy_t a, xy_t b, xy_t c, xy_t d) {
    double d1 = cross_product(b - a, c - a);
    double d2 = cross_product(b - a, d - a);
    double d3 = cross_product(d - c, a - c);
    double d4 = cross_product(d - c, b - c);
    // cout << a << " " << b << " " << c << " " << d << endl;
    // cout << b - a << " " << c - a << endl;
    // cout << conj(b - a) << endl;
    // cout << conj(b - a) * (c - a) << endl;
    // cout << d1 << " " << d2 << " " << d3 << " " << d4 << endl;
    if (d1 * d2 < -eps && d3 * d4 < -eps) {
        return true;
    } else {
        return false;
    }
}

double interpoint (xy_t a, xy_t b, xy_t c, xy_t d) {
    xy_t p = b - a;
    xy_t q = d - c;
    return cross_product(q, c - a) / cross_product(q, p);
}

int main() {
    int N;
    cin >> N;
    for (int i = 0; i < N; i++) {
        int xa, ya, xb, yb;
        cin >> xa >> ya >> xb >> yb;
        xy_t p(xa, ya);
        xy_t q(xb, yb);
        int n;
        cin >> n;
        vector<pdlg> vec;
        for (int j = 0; j < n; j++) {
            int x1, y1, x2, y2, o, l;
            cin >> x1 >> y1 >> x2 >> y2 >> o >> l;
            xy_t p1(x1, y1);
            xy_t p2(x2, y2);
            bool ok = seginter(p, q, p1, p2);
            if (ok) {
                double d = interpoint(p, q, p1, p2);
                int mark = 1;
                if ((o == 1 && l == 0) || (o == 0 && l == 1)) {
                    mark = 0;
                }
                //cout << d << " " << mark << " " << j << endl;
                vec.push_back(pdlg(d, mark));
            }
        }
        sort(vec.begin(), vec.end());
        if (vec.size() == 0) {
            cout << 0 << endl;
        } else {
            int mark = vec[0].second;
            int len = vec.size();
            // for (int j = 0; j < len; j++) {
            //     cout << vec[j].first << " " << vec[j].second << endl;
            // }
            ll ans = 0;
            for (int j = 1; j < len; j++) {
                if (vec[j].second != mark) {
                    ans++;
                    mark = vec[j].second;
                }
            }
            cout << ans << endl;
        }
    }
}

