### subroutines

def cross_y(y0, x1, y1, x2, y2)
  return nil if y1 == y2

  t = (y0 - y1).to_f / (y2 - y1)
  return nil if t < 0.0 || t > 1.0

  x0 = t * (x2 - x1) + x1
end

def cross_quad?(q1, q2)
  min_y1, max_y1 = [q1[1], q1[3], q1[5], q1[7]].minmax
  min_y2, max_y2 = [q2[1], q2[3], q2[5], q2[7]].minmax

  if max_y1 - min_y1 <= max_y2 - min_y2
    min_y = min_y1
    max_y = max_y1
  else
    min_y = min_y2
    max_y = max_y2
  end

  for y0 in (min_y..max_y)
    xs1 = [
      cross_y(y0, q1[0], q1[1], q1[2], q1[3]),
      cross_y(y0, q1[2], q1[3], q1[4], q1[5]),
      cross_y(y0, q1[4], q1[5], q1[6], q1[7]),
      cross_y(y0, q1[6], q1[7], q1[0], q1[1])
    ].select{|x| ! x.nil?}.sort

    xs2 = [
      cross_y(y0, q2[0], q2[1], q2[2], q2[3]),
      cross_y(y0, q2[2], q2[3], q2[4], q2[5]),
      cross_y(y0, q2[4], q2[5], q2[6], q2[7]),
      cross_y(y0, q2[6], q2[7], q2[0], q2[1])
    ].select{|x| ! x.nil?}.sort

    next if xs1.length != 2 || xs2.length != 2

    if ! (xs1[1] < xs2[0] || xs2[1] < xs1[0])
      return true
    end
  end

  false
end

### main

while true
  n = gets.strip.to_i
  break if n == 0

  n.times.each do
    m = gets.strip.to_i

    quads = []

    m.times.each do
      q0 = gets.strip.split(' ').map{|s| s.to_i}
      id = 0

      while id < quads.length
        cross = false
        for q1 in quads[id]
          if cross_quad?(q0, q1)
            cross = true
            break
          end
        end
        break if cross
        id += 1
      end

      if id >= quads.length
        quads[id] = [q0]
      else
        quads[id] << q0
      end
    end

    puts quads.length
  end
end