### classes

class Pt
  attr_accessor :x, :y

  def initialize() end
  def initialize(x, y) @x = x; @y = y end

  def ==(pt) @x == pt.x && @y == pt.y end
  def +(pt) Pt.new(@x + pt.x, @y + pt.y) end
  def -(pt) Pt.new(@x - pt.x, @y - pt.y) end
  def -@() Pt.new(-@x, -@y) end
  def *(t) Pt.new(@x * t, @y * t) end
  def /(t) Pt.new(@x / t, @y / t) end
  def dot(v) @x * v.x + @y * v.y end
  def cross(v) @x * v.y - @y * v.x end
  def d2() @x * @x + @y * @y end
  def d() Math.sqrt(d2()) end
  def to_s() "(#{@x},#{@y})" end
end

### subroutines

def cross_segs(a0, a1, b0, b1)
  av = a1 - a0
  bv = b1 - b0
  cr01 = av.cross(bv)
  return nil if cr01 == 0.0

  ab = b0 - a0
  cr0 = ab.cross(av)
  cr1 = ab.cross(bv)

  t0 = cr1 / cr01
  t1 = cr0 / cr01

  cl = av * t0 + a0
  #p [ap.to_s, av.to_s, bp.to_s, bv.to_s, cl.to_s]
  return nil if t0 < 0.0 || t0 > 1.0 || t1 < 0.0 || t1 > 1.0
  [cl, t0]
end

### main

gets.to_i.times do
  xa, ya, xb, yb = gets.split.map(&:to_f)
  pta = Pt.new(xa, ya)
  ptb = Pt.new(xb, yb)
  
  n = gets.to_i

  lns = []
  for i in (0...n)
    x0, y0, x1, y1, oi, li = gets.split.map(&:to_i)
    lns << [Pt.new(x0.to_f, y0.to_f), Pt.new(x1.to_f, y1.to_f), oi, li]
  end
  #p lns
  
  cts = []

  for i in (0...n)
    pt0, pt1, oi, li = lns[i]
    csg = cross_segs(pta, ptb, pt0, pt1)
    if csg
      cts << [csg[1], (oi ^ 1) ^ li]
    end
  end
  cts.sort!
  #p cts

  count = 0
  if cts.length > 1
    prvl = cts[0][1]
    for i in (1...cts.length)
      count += 1 if prvl != cts[i][1]
      prvl = cts[i][1]
    end
  end

  puts count
end