import java.util.*; import java.awt.geom.*; class RailPoint implements Comparable{ double x, y; int h; RailPoint(double x, double y, int h){ this.x = x; this.y = y; this.h = h; } public int compareTo(RailPoint that){ if (this.x < that.x) return -1; else if(this.x > that.x) return 1; else if(this.y < that.y) return -1; else if(this.y > that.y) return 1; else return 0; } } public class D_nomura{ static Point2D.Double crossPt(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4){ if(!Line2D.linesIntersect(x1, y1, x2, y2, x3, y3, x4, y4)) return null; double dis1 = Line2D.ptLineDist(x1, y1, x2, y2, x3, y3); double dis2 = Line2D.ptLineDist(x1, y1, x2, y2, x4, y4); return new Point2D.Double((dis2 * x3 + dis1 * x4) / (dis1 + dis2), (dis2 * y3 + dis1 * y4) / (dis1 + dis2)); } public static void main(String[] args) throws Exception{ Scanner sc=new Scanner(System.in); for(int qn = sc.nextInt(); qn > 0; qn--){ //入力 int Ax = sc.nextInt(), Ay = sc.nextInt(), Bx = sc.nextInt(), By = sc.nextInt(); int n = sc.nextInt(); int[][] ls = new int[n][6]; for(int i = 0; i < n; i++) for(int j = 0; j < 6; j++) ls[i][j] = sc.nextInt(); //交点列挙してソート Vector rps = new Vector(); for(int i = 0; i < n; i++){ Point2D.Double cp = crossPt(Ax, Ay, Bx, By, ls[i][0], ls[i][1], ls[i][2], ls[i][3]); if(cp != null) rps.add(new RailPoint(cp.x, cp.y, ls[i][4] ^ ls[i][5])); } Collections.sort(rps); //高さが変わるところを数える int ans = 0, h = -1; for(RailPoint p: rps){ if(h != -1 && h != p.h) ans++; h = p.h; } System.out.println(ans); } } }