using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Xsl;
using static System.Math;

namespace CSharpSpiral {
	class Program {
		static void Main(string[] args) {
			var sw = new StreamWriter(Console.OpenStandardOutput()) {
				AutoFlush=
#if DEBUG
			true
#else
			false
#endif
			};
			Console.SetOut(sw);
			new Program().Solve(new ConsoleInput(
#if DEBUG
			new StreamReader("../../../inputData.txt")
#else
			Console.In
#endif
			, ' '));
			Console.Out.Flush();
		}
		public void Solve(ConsoleInput cin) {
			var dataSetCount = cin.ReadInt;
			for(int dataSet = 0; dataSet<dataSetCount; dataSet++) {
				var dat = cin.ReadLine.Split(' ').Take(4).Select(int.Parse).ToArray();
				var A = new Point(dat[0], dat[1]);
				var B = new Point(dat[2], dat[3]);
				var newRoad = new Railway(A, B, false, false);
				var N = cin.ReadInt;
				var oldRails = new Railway[N];
				var doesCross = new bool[N];
				var dist = new decimal[N];
				for(int i = 0; i<N; i++) {
					var datum = cin.ReadIntArray(6);
					var start = new Point(datum[0], datum[1]);
					var end = new Point(datum[2], datum[3]);
					var own = datum[4]==1;
					var inf = datum[5]==0;
					oldRails[i]=new Railway(start, end, own, inf);
					doesCross[i]=Railway.isIntersect(newRoad, oldRails[i]);
					dist[i]=!doesCross[i] ? -1 : Point.outer(start-A, end-A)/Point.outer(B-A, end-start);
				}
				var Order = doesCross.Indices().Where(i => doesCross[i]).OrderBy(i => dist[i]);
				if(!Order.Any()) {
					Console.WriteLine(0);
					continue;
				}
				var turn = 0;
				var firstOpponent = oldRails[Order.First()];
				var nowInfra = firstOpponent.infra==firstOpponent.friend;
				foreach(var j in Order.Skip(1)) {
					var rail = oldRails[j];
					if((nowInfra==rail.infra)^rail.friend) {
						nowInfra^=true;
						turn++;
					}
				}
				Console.WriteLine(turn);
			}
		}
		struct Railway {
			public Point start { get; set; }
			public Point end { get; set; }
			public bool friend { get; set; }
			public bool infra { get; set; }
			public Railway(Point ss, Point ee, bool ff, bool ii) {
				start=ss; end=ee; friend=ff; infra=ii;
			}
			public static bool isIntersect(Railway A, Railway B) {
				Point a1 = A.start, a2 = A.end, b1 = B.start, b2 = B.end;
				var parallel = Point.outer(a2-a1, b2-b1)==0m;
				if(parallel) {
					var willIntersect = Point.outer(a2-a1, b1-a1)==0m;
					if(!willIntersect) return false;
					var k1 = Point.inner(b1-a1, b1-a2);
					var k2 = Point.inner(b2-a1, b2-a2);
					if(k1<=0||k2<=0) return true;
					var l1 = Point.inner(a1-b1, a1-b2);
					var l2 = Point.inner(a2-b1, a2-b2);
					return l1<=0||l2<=0;
				} else {
					return Point.outer(b1-a1, a2-a1)*Point.outer(b2-a2, a1-a2)>=0
					&&Point.outer(a1-b1, b2-b1)*Point.outer(a2-b2, b1-b2)>=0;
				}
			}
		}
	}
	static class ExtendClass {
		public static void Swap<T>(this T[] A, int x, int y) {
			var tmp = A[x];
			A[x]=A[y];
			A[y]=tmp;
		}
		public static string Join<T>(this IEnumerable<T> X, string separator = " ") => string.Join(separator, X);
		public static IEnumerable<int> Indices<T>(this T[] X) => Enumerable.Range(0, X.Length);
		public static IEnumerable<int> Indices<T>(this T[,] X, int dim)
			=> Enumerable.Range(0, X.GetLength(dim));
		public static string Join<T>(this T[,] X, string separatorD2 = ",", string separatorD1 = "\n")
			=> X.Indices(0).Select(
				i => X.Indices(1).Select(j => X[i, j]).Join(separatorD2)
			).Join(separatorD1);
	}
	class ConsoleInput {
		private readonly TextReader _stream;
		private readonly char _separator = ' ';
		private readonly Queue<string> inputStream;
		public ConsoleInput(TextReader stream, char separator = ' ') {
			this._separator=separator;
			this._stream=stream;
			inputStream=new Queue<string>();
		}
		public string Read {
			get {
				if(inputStream.Count!=0) return inputStream.Dequeue();
				string[] tmp = _stream.ReadLine().Split(_separator);
				for(int i = 0; i<tmp.Length; ++i)
					inputStream.Enqueue(tmp[i]);
				return inputStream.Dequeue();
			}
		}
		public string ReadLine { get { return _stream.ReadLine(); } }
		public int ReadInt { get { return int.Parse(Read); } }
		public long ReadLong { get { return long.Parse(Read); } }
		public double ReadDouble { get { return double.Parse(Read); } }
		public string[] ReadStrArray(long N) {
			var ret = new string[N];
			for(long i = 0; i<N; ++i)
				ret[i]=Read;
			return ret;
		}
		public int[] ReadIntArray(long N) { var ret = new int[N]; for(long i = 0; i<N; ++i) ret[i]=ReadInt; return ret; }
		public long[] ReadLongArray(long N) { var ret = new long[N]; for(long i = 0; i<N; ++i) ret[i]=ReadLong; return ret; }
		public double[] ReadDoubleArray(long N) { var ret = new double[N]; for(long i = 0; i<N; ++i) ret[i]=ReadDouble; return ret; }
		public bool isEOF {
			get { return _stream.Peek()==-1; }
		}
	}
	public struct Point {
		public decimal x { get; set; }
		public decimal y { get; set; }
		public Point(decimal xx, decimal yy) { x=xx; y=yy; }
		public static Point operator -(Point a, Point b) => new Point(a.x-b.x, a.y-b.y);
		public static decimal outer(Point a, Point b) => a.x*b.y-b.x*a.y;
		public static decimal inner(Point a, Point b) => a.x*b.x+a.y*b.y;
		public decimal Abs2() => x*x+y*y;
		public override string ToString() => $"({x}, {y})";
	}
}


Measured memory_bytes: 21092