import scala.math._

object Main {
  def main(args: Array[String]) {
    val sc = new java.util.Scanner(System.in)
    val T = sc.nextInt()
    for (i <- 1 to T) {
      var xa, ya, xb, yb = sc.nextInt()
      xb -= xa
      yb -= ya
      val theta = atan2(yb, xb)
      val n = sc.nextInt()
      val xlist = (
        for {
          j <- 1 to n
          xs_ = sc.nextInt(); ys_ = sc.nextInt();
          xt_ = sc.nextInt(); yt_ = sc.nextInt();
          o = sc.nextInt(); l = sc.nextInt();
          (xs, ys) = rot(xs_ - xa, ys_ - ya, -theta)
          (xt, yt) = rot(xt_ - xa, yt_ - ya, -theta)
          if ys*yt < 0
        } yield ((xs*yt-xt*ys)/(yt-ys), o, l)
      ).toList.sortWith(_._1 < _._1).filter(_._1 > 0).filter(_._1 < pow(xb*xb+yb*yb,0.5))

      println(min(count(0, xlist), count(1, xlist)))
    }
  }
  def rot(x: Double, y: Double, theta: Double) = {
    val c = cos(theta)
    val s = sin(theta)
    (c*x - s*y, s*x + c*y)
  }
  def count(l_ : Int, xlist: List[(Double, Int, Int)]): Int = {
    if (xlist.size == 0) {
      0
    } else {
      val (x, o, l) = xlist.head;
      if((l == l_ && o == 1) || (l != l_ && o == 0)) {
        1 + count(if(l_ == 1) 0 else 1, xlist.tail)
      } else {
        count(l_ , xlist.tail)
      }
    }
  }
}