EPS = 1e-9

def cross(p)
  x0 = (p[2] - p[0]).to_f
  y0 = (p[3] - p[1]).to_f
  x1 = (p[6] - p[4]).to_f
  y1 = (p[7] - p[5]).to_f
  x2 = (p[4] - p[0]).to_f
  y2 = (p[5] - p[1]).to_f
  r = (y1 * x2 - x1 * y2) / (x0 * y1 - x1 * y0)
  s = (y0 * x2 - x0 * y2) / (x0 * y1 - x1 * y0)
  p[0] + r * (p[2] - p[0]) if r > -EPS && r < 1.0 + EPS && s > -EPS && s < 1.0 + EPS
end

gets.to_i.times do
  xa, ya ,xb, yb = gets.split.map(&:to_i)
  arr = []
  gets.to_i.times do
    xc, yc, xd, yd, o, l = gets.split.map(&:to_i)
    px = cross([xa, ya, xb, yb, xc, yc, xd, yd])
    arr << [px, o, l] unless px.nil?
  end
  arr.sort!
  _, so, sl = arr.shift
  cl = (so == 1 ? sl : 1 - sl)
  p arr.reduce(0){ |cnt, (_, o, l)|
    if o != 1 && cl == l
      cl = 1 - cl; cnt + 1
    elsif o == 1 && cl != l
      cl = l; cnt + 1
    else cnt
    end
  }
end