/*
 * 0214.cc: Autumnal Illumination
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100;
const int MAX_M = 100;

/* typedef */

template <typename T>
struct Pt {
  T x, y;

  Pt() {}
  Pt(T _x, T _y) : x(_x), y(_y) {}
  Pt(const Pt& pt) : x(pt.x), y(pt.y) {}

  bool operator==(const Pt pt) const { return x == pt.x && y == pt.y; }
  Pt<T> operator+(const Pt pt) const { return Pt<T>(x + pt.x, y + pt.y); }
  Pt<T> operator-() const { return Pt<T>(-x, -y); }
  Pt<T> operator-(const Pt pt) const { return Pt<T>(x - pt.x, y - pt.y); }
  Pt<T> operator*(T t) const { return Pt<T>(x * t, y * t); }
  Pt<T> operator/(T t) const { return Pt<T>(x / t, y / t); }
  T dot(Pt v) const { return x * v.x + y * v.y; }
  T cross(Pt v) const { return x * v.y - y * v.x; }
  Pt<T> mid(const Pt pt) { return Pt<T>((x + pt.x) / 2, (y + pt.y) / 2); }
  T d2() { return x * x + y * y; }
  double d() { return sqrt(d2()); }

  Pt<T> rot(double th) {
    double c = cos(th), s = sin(th);
    return Pt<T>(c * x - s * y, s * x + c * y);
  }

  Pt<T> rot90() { return Pt<T>(-y, x); }

  bool operator<(const Pt& pt) const {
    return x < pt.x || (x == pt.x && y < pt.y);
  }

  void print(string format) {
    printf(("(" + format + ", " + format + ")\n").c_str(), x, y);
  }
  void print() { print("%.6lf"); }
};

typedef Pt<double> pt;

struct CL {
  pt p;
  double t0, t1;
  CL() {}
  CL(const pt& _p, double _t0, double _t1) : p(_p), t0(_t0), t1(_t1) {}
};

struct UFT {
  int links[MAX_M], ranks[MAX_M], sizes[MAX_M];

  void clear(int n) {
    for (int i = 0; i < n; i++) links[i] = i, ranks[i] = sizes[i] = 1;
  }

  UFT() {}
  UFT(int n) { clear(n); }

  int root(int i) {
    int i0 = i;
    while (links[i0] != i0) i0 = links[i0];
    return (links[i] = i0);
  }

  int rank(int i) { return ranks[root(i)]; }
  int size(int i) { return sizes[root(i)]; }
  bool same(int i, int j) { return root(i) == root(j); }

  int merge(int i0, int i1) {
    int r0 = root(i0), r1 = root(i1), mr;
    if (ranks[r0] == ranks[r1]) {
      links[r1] = r0;
      sizes[r0] += sizes[r1];
      ranks[r0]++;
      mr = r0;
    }
    else if (ranks[r0] > ranks[r1]) {
      links[r1] = r0;
      sizes[r0] += sizes[r1];
      mr = r0;
    }
    else {
      links[r0] = r1;
      sizes[r1] += sizes[r0];
      mr = r1;
    }
    return mr;
  }
};

/* global variables */

pt rcts[MAX_M][4];
UFT uft;

/* subroutines */

bool cross_lines(const pt& ap, const pt av, const pt& bp, const pt bv, CL& cl) {
  double op01 = av.cross(bv);
  //if (op01 == 0.0) return false; /* need to handle parallel?? */

  if (op01 == 0.0) {
    pt v = bp - ap;
    if (v.cross(av) != 0.0) return false;

    pt a1 = ap + av;
    pt b1 = bp + bv;

    return
      ((bp - ap).dot(b1 - ap) <= 0.0 ||
       (bp - a1).dot(b1 - a1) <= 0.0 ||
       (ap - bp).dot(a1 - bp) <= 0.0 ||
       (ap - b1).dot(a1 - b1) <= 0.0);
  }

  pt v = bp - ap;
  double op0 = v.cross(av);
  double op1 = v.cross(bv);

  double t0 = op1 / op01;
  double t1 = op0 / op01;

  cl.p = bv * t1 + bp;
  cl.t0 = t0;
  cl.t1 = t1;

  return true;
  //return (0.0 <= cl.t0 && cl.t0 <= 1.0 && 0.0 <= cl.t1 && cl.t1 <= 1.0);
}

bool cross_segs(const pt& ap, const pt av, const pt& bp, const pt bv) {
  CL cl;
  if (! cross_lines(ap, av, bp, bv, cl)) return false;
  return (0.0 <= cl.t0 && cl.t0 <= 1.0 && 0.0 <= cl.t1 && cl.t1 <= 1.0);
}

bool pt_trg(pt p, pt trg[]) {
  for (int i = 0; i < 3; i++)
    if ((trg[(i + 1) % 3] - trg[i]).cross(p - trg[i]) < 0.0) return false;
  return true;
}

bool col_trgs(pt t0[], pt t1[]) {
  for (int i = 0; i < 3; i++) {
    if (pt_trg(t0[i], t1)) return true;
    if (pt_trg(t1[i], t0)) return true;
  }

  for (int i = 0; i < 3; i++) {
    pt v0 = t0[(i + 1) % 3] - t0[i];
    for (int j = 0; j < 3; j++) {
      pt v1 = t1[(j + 1) % 3] - t1[j];
      if (cross_segs(t0[i], v0, t1[j], v1)) return true;
    }
  }
  
  return false;
}

/* main */

int main() {
  for (;;) {
    int n;
    cin >> n;
    if (n == 0) break;

    while (n--) {
      int m;
      cin >> m;

      for (int i = 0; i < m; i++)
	for (int j = 0; j < 4; j++) cin >> rcts[i][j].x >> rcts[i][j].y;

      uft.clear(m);
      int ans = m;

      for (int i = 0; i < m; i++) {
	pt t00[3] = {rcts[i][0], rcts[i][1], rcts[i][2]};
	pt t01[3] = {rcts[i][2], rcts[i][3], rcts[i][0]};
	for (int j = i + 1; j < m; j++) {
	  int ri = uft.root(i), rj = uft.root(j);
	  if (ri != rj) {
	    pt t10[3] = {rcts[j][0], rcts[j][1], rcts[j][2]};
	    pt t11[3] = {rcts[j][2], rcts[j][3], rcts[j][0]};
	    if (col_trgs(t00, t10) || col_trgs(t00, t11) ||
		col_trgs(t01, t10) || col_trgs(t01, t11)) {
	      uft.merge(ri, rj);
	      ans--;
	    }
	  }
	}
      }

      printf("%d\n", ans);
    }
  }
  return 0;
}