#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }


struct UnionFind
{
  vint data;
  UnionFind(int size) : data(size, -1) {}
  bool unite(int x, int y) {
    x = root(x); y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y]; data[y] = x;
    }
    return x != y;
  }
  bool same(int x, int y) {
    return root(x) == root(y);
  }
  int root(int x) {
    return data[x] < 0 ? x : data[x] = root(data[x]);
  }
  int size(int x) {
    return -data[root(x)];
  }
  int count() {
    int res = 0;
    rep(i, data.size()) res += data[i] < 0;
    return res;
  }
};


struct Point
{
  int x, y;
};

struct Tetragon
{
  Point p[4];
};

bool cont(Tetragon tet, Point p)
{
  rep(i, 4){
    int x1 = p.x - tet.p[i].x, x2 = tet.p[(i + 1) % 4].x - tet.p[i].x;
    int y1 = p.y - tet.p[i].y, y2 = tet.p[(i + 1) % 4].y - tet.p[i].y;

    double theta = (x1 * x2 + y1 * y2) / sqrt(x1 * x1 + y1 * y1) / sqrt(x2 * x2 + y2 * y2);

    if(theta < 0) return false;
  }

  return true;
}

bool __line_cross(Point s1, Point t1, Point s2, Point t2)
{
  int x1 = t1.x - s1.x, x2 = s2.x - s1.x;
  int y1 = t1.y - s1.y, y2 = s2.y - s1.y;

  double theta1 = (x1 * x2 + y1 * y2) / sqrt(x1 * x1 + y1 * y1) / sqrt(x2 * x2 + y2 * y2);

  x2 = t2.x - s1.x, y2 = t2.y - s1.y;

  double theta2 = (x1 * x2 + y1 * y2) / sqrt(x1 * x1 + y1 * y1) / sqrt(x2 * x2 + y2 * y2);

  return theta1 * theta2 <= 0;
}

bool line_cross(Point s1, Point t1, Point s2, Point t2)
{
  return __line_cross(s1, t1, s2, t2) && __line_cross(s2, t2, s1, t1);
}

bool col(Tetragon tet1, Tetragon tet2)
{
  rep(i, 4) if(cont(tet1, tet2.p[i])) return true;
  rep(i, 4) if(cont(tet2, tet1.p[i])) return true;

  rep(i, 4) REP(j, i+1, 4) rep(k, 4) REP(l, k+1, 4) if(line_cross(tet1.p[i], tet1.p[j], tet2.p[k], tet2.p[l])) return true;
  
  return false;
}

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

      vector<Tetragon> tetras;

      rep(i, m){
	Tetragon tmp;

	rep(i, 4) cin >> tmp.p[i].x >> tmp.p[i].y;
	
	tetras.push_back(tmp);
      }

      UnionFind uft(m);
      rep(i, m) rep(j, m) if(col(tetras[i], tetras[j])) uft.unite(i, j);

      cout << uft.count() << endl;
    }
  }
}