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]
		return (m.inv * v).to_a
	end

	def cross?(l)
		(@a * l.x1 + @b * l.y1 - @c) * (@a * l.x2 + @b * l.y2 - @c) < 0 &&
		(l.a * @x1 + l.b * @y1 - l.c) * (l.a * @x2 + l.b * @y2 - l.c) < 0
	end

	private_class_method :new

	def initialize(*args)
		@a, @b, @c, @x1, @y1, @x2, @y2 = args	
	end

	attr_reader :a, :b, :c, :x1, :y1, :x2, :y2
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.cross?(l)
		arr << [*nl.cross_point(l), a ^ b]
	end
	if arr.size < 2
		p 0
	else
		p arr.sort.map(&:last).each_cons(2).map{|x, y| x ^ y}.inject(:+)
	end
end