ERROR = 1e-5

include Math
class Line
	require 'matrix'
	#ax+by = c   (a > 0 or (a == 0 && b > 0))
	def self.through_two_points(x1, y1, x2, y2)
		a = y1 - y2
		b = x2 - x1
		c = a * x1 + b * y1
		new(a, b, c, x1, y1, x2, y2)
	end

	def cross_point(l)
		m = Matrix[
			[@a, @b],
			[l.a, l.b],
		];
		v = Vector[@c, l.c]
		(m.inv * v).to_a
	end

	def include?(x, y)
		(@a * x + @b * y - c).abs < ERROR && 
		(x - @x1) * (x - @x2) <= ERROR &&
		(y - @y1) * (y - @y2) <= ERROR
	end

	def parallel?(l)
		(@a * l.b - @b * l.a).abs < ERROR
	end

	private_class_method :new

	def initialize(*args)
		@a, @b, @c, @x1, @y1, @x2, @y2 = args	
		if @a < 0
			@a *= -1
			@b *= -1
			@c *= -1
		elsif @a == 0 && @b < 0
			@b *= -1
			@c *= -1
		end
	end

	attr_reader :a, :b, :c
end

t = gets.to_i
t.times do
	nl = Line.through_two_points(*gets.split.map(&:to_i))
	n = gets.to_i
	arr = []
	(1..n).each do
		x1, y1, x2, y2, a, b = gets.split.map(&:to_i)
		l = Line.through_two_points(x1, y1, x2, y2)
		next if nl.parallel?(l)
		cx, cy = nl.cross_point(l)
		arr << [cx, cy, a ^ b] if nl.include?(cx, cy)		
	end
	p arr.sort.map(&:last).each_cons(2).map{|x, y| x ^ y}.inject(:+)
end