/* ----- Header Files ----- */
// IO
#include <cstdio>
#include <iomanip>
#include <ios>
#include <iostream>

// algorithm
#include <algorithm>
#include <cmath>
#include <numeric>

// container
#include <vector>
#include <string>
#include <tuple>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <complex>

// others
#include <random>
#include <limits>
#include <functional>
#include <ctime>
#include <cassert>
#include <cstdint>


/* ----- Type Alias ----- */
using Bool = bool;
using Int = long long int;
using Real = long double;
using Char = char;
using String = std::string;
template <class... Ts>
using Tuple = std::tuple<Ts...>;

template <class T>
using Vector = std::vector<T>;
template <class T>
using Matrix = Vector<Vector<T>>;
template <class T>
using Queue = std::queue<T>;
template <class T>
using Stack = std::stack<T>;
template <class T>
using Deque = std::deque<T>;

template <class T>
using MaxHeap = std::priority_queue<T>;
template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T>
using Set = std::set<T>;
template <class T, class U>
using Map = std::map<T, U>;

template <class T, class... Us>
using Func = std::function<T(Us...)>;

template <class T>
T genv(T v) { return v; }

template <class T, class... Ts>
auto genv(size_t l, Ts... ts) {
    return Vector<decltype(genv<T>(ts...))>(l, genv<T>(ts...));
}


/* ----- Misc ----- */
void fastio() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
}

struct Fout {
    Int precision;
    Fout(Int precision) : precision(precision) {}
};
std::ostream& operator<<(std::ostream& os, const Fout& fio) {
    os << std::fixed << std::setprecision(fio.precision);
    return os;
}


/* ----- Constants ----- */
// constexpr Int INF = std::numeric_limits<Int>::max() / 3;
// constexpr Int MOD = 1000000007;
// constexpr Real PI = acos(-1);
// constexpr Real EPS = 1e-10;
// std::mt19937 mt(int(std::time(nullptr)));

template <class T>
inline T sq(T x) { return x * x; }

using Real = long double;
using Point = std::complex<Real>;
using Segment = std::pair<Point, Point>;
using Polygon = std::vector<Point>;

bool operator<(const Point& a, const Point& b) {
    return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag();
}

std::istream& operator>>(std::istream& is, Point& p) {
    Real x, y;
    is >> x >> y;
    p = Point(x, y);
    return is;
}

const Real EPS = 1e-10;

// 宇宙船演算子
inline int compare(Real a, Real b) {
    if (std::abs(a - b) < EPS) return 0;
    return a - b > 0 ? 1 : -1;
}

inline Real dist(Point a, Point b) {
    return std::abs(a - b);
}

inline Real length(Segment s) {
    return dist(s.first, s.second);
}

// inner product
inline Real dot(Point x, Point y) {
    return std::real(std::conj(x) * (y));
}

// outer product
inline Real cross(Point x, Point y) {
    return std::imag(std::conj(x) * (y));
}

// lに対するxの正射影
Point proj(Segment s, Point p) {
    Real ratio = dot(s.second - s.first, p - s.first) / sq(length(s));
    return s.first + (s.second - s.first) * ratio;
}

// lを軸としてxに対称な点
Point refl(Segment s, Point p) {
    Point t = proj(s, p);
    return t + (t - p);
}

// lに対するpの位置
// 0: on segment
// 1: counter clockwise  -1: clockwise
// 2: online front       -2: online back
int side(Segment s, Point p) {
    Real c = cross(s.second - s.first, p - s.first);
    if (compare(c, 0) != 0) return compare(c, 0);

    Real d = dot(s.second - s.first, p - s.first);
    if (compare(d, 0) < 0) return -2;

    return (compare(length(Segment(s.first, p)), length(s)) > 0 ? 2 : 0);
}

inline bool isorthogonal(Segment s1, Segment s2) {
    return compare(dot(s1.second - s1.first, s2.second - s2.first), 0) == 0;
}

inline bool isparallel(Segment s1, Segment s2) {
    return compare(cross(s1.second - s1.first, s2.second - s2.first), 0) == 0;
}

// bound: 線分の端点を含むか
inline bool intersect(Segment s1, Segment s2, bool bound) {
    return (side(s1, s2.first) * side(s1, s2.second) < bound) &&
           (side(s2, s1.first) * side(s2, s1.second) < bound);
}

Point intersection(Segment s1, Segment s2) {
    Real c1 = cross(s2.second - s2.first, s1.second - s1.first);
    Real c2 = cross(s2.second - s2.first, s1.first - s2.first);
    return s1.first + (s1.second - s1.first) * (-c2 / c1);
}


void solve() {
    Segment l;
    std::cin >> l.first >> l.second;

    Int n;
    std::cin >> n;

    Vector<Tuple<Point, Bool>> ps;
    for (Int i = 0; i < n; ++i) {
        Segment s;
        Int a, b;
        std::cin >> s.first >> s.second >> a >> b;

        if (intersect(l, s, false)) {
            Point p = intersection(l, s);
            ps.emplace_back(p, a == b);
        }
    }
    std::sort(ps.begin(), ps.end(), [](auto a, auto b) {
        return std::get<0>(a) < std::get<0>(b);
    });

    Int ans = 0;
    for (Int i = 0; i + 1 < ps.size(); ++i) {
        if (std::get<1>(ps[i]) != std::get<1>(ps[i + 1])) ++ans;
    }
    std::cout << ans << std::endl;
}

int main() {
    Int n;
    std::cin >> n;
    for (Int i = 0; i < n; ++i) solve();
    return 0;
}



Measured memory_bytes: 3196