#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <queue>
#include <bitset>
#include <string>
using namespace std;

#define REP(i,n) for(int i=0; i<n; i++)
#define RPA(i,s,e) for(int i=s; i<=e; i++)
#define RPD(i,s,e) for(int i=s; i>=e; i--)
#define PB(a) push_back(a)
#define MP(i,s) make_pair(i,s)
#define ALL(a) (a).begin(), (a).end()
#define PRT(a) cout << (a) << endl

const double EPS = 1.0e-10;

class P {
public:
	double x, y;
	P() {}
	P(int x_, int y_) : x(x_), y(y_) {}
	P(double x_, double y_) : x(x_), y(y_) {}
	P(const P& p) : x(p.x), y(p.y) {}
	P operator+(const P& p) const { return P(x+p.x, y+p.y); } 
	P operator-(const P& p) const { return P(x-p.x, y-p.y); }
	P operator-() const { return P(-x, -y); }
	P operator*(double s) const { return P(x*s, y*s); }
	P operator/(double s) const { return P(x/s, y/s); }
	P& operator=(const P& p) { x=p.x; y=p.y; return (*this); }
	double dot(const P& p) const { return x*p.x + y*p.y; }
	double det(const P& p) const { return x*p.y - y*p.x; }
	double norm() const { return sqrt(x*x + y*y); }
	double norm2() const { return x*x + y*y; }
	P proj(const P& p) const { double k=det(p)/norm2(); return P(x*k, y*k); }
};

double tri(P a, P b, P c) { return (b - a).det(c - a); }
int sign(double r) { return r < -EPS ? -1 : r > EPS ? 1 : 0; }

class L {
public:
	P s, t;
	L() {}
	L(P s_, P t_) : s(s_), t(t_) {}
	L(const L& l) : s(l.s), t(l.t) {}
	L& operator=(const L& l) { s=l.s; t=l.t; return (*this); }
	double length() { return (s - t).norm(); }
	P vec() const { return t - s; }
	P proj(const P& p) { return s + vec().proj(p - s); }
	int iLL(const L& l) const {
		if(sign(vec().det(l.vec()))) return 1;
		if(sign(vec().det(l.s - s))) return 0;
		return -1;
	}
	bool iLS(const L& l) const { return sign(tri(s, t, l.s)) * sign(tri(s, t, l.t)) <= 0; } 
	bool iSS(const L& l) const { return iLS(l) && l.iLS(*this); }
	P pLL(const L& l) const { return s + vec() * (l.s - s).det(l.vec()) / vec().det(l.vec()); }
	double dLP(const P& p) const { return abs(tri(s, t, p)) / vec().norm(); }
	double dSP(const P& p) const {
		if(sign(vec().dot(p - s)) <= 0) return (p - s).norm();
		if(sign(vec().dot(p - t)) >= 0) return (p - t).norm();
		return dLP(p);
	}
};

const int MAX_N = 101;
vector<L> rects[MAX_N];

int M, N;
P p1, p2, p3, p4;
bool G[MAX_N][MAX_N];
bool used[MAX_N];

bool overlap(vector<L>& r1, vector<L>& r2) {
	REP(i,r1.size()) {
		REP(j,r2.size()) {
			if(r1[i].iSS(r2[j])) return true;
		}
	}
	return false;
}

bool contain(vector<L>& r1, vector<L>& r2) {
	REP(i,r2.size()) {
		REP(j,r1.size()) {
			P p1 = r1[j].s - r2[i].s;
			P p2 = r1[j].t - r2[i].s;
			if(sign(p1.det(p2)) != -1) {
				return false;
			}
		}
	}
	return true;
}

void bfs(int v) {
	queue<int> que;
	used[v] = true;
	que.push(v);
	while(!que.empty()) {
		int v = que.front();
		que.pop();
		REP(i,N) {
			if(G[v][i] && !used[i]) {
				used[i] = true;
				que.push(i);
			}
		}
	}
}

void solve() {
	memset(G, 0, sizeof(G));
	REP(i,N) {
		RPA(j,i+1,N-1) {
			if(overlap(rects[i], rects[j]) ||
				(contain(rects[i], rects[j]) || contain(rects[j], rects[i]))) {
				G[i][j] = true;
				G[j][i] = true;
			}
		}
	}

	int cnt = 0;
	memset(used, 0, sizeof(used));
	REP(i,N) {
		if(!used[i]) {
			cnt++;
			bfs(i);
		}
	}
	printf("%d\n", cnt);
}

void coding() {
	while(scanf("%d", &M) != EOF) {
		if(M == 0) break;

		REP(k,M) {
			scanf("%d", &N);
			REP(i,N) rects[i].clear();

			REP(i,N) {
				scanf("%lf%lf", &p1.x, &p1.y);
				scanf("%lf%lf", &p2.x, &p2.y);
				scanf("%lf%lf", &p3.x, &p3.y);
				scanf("%lf%lf", &p4.x, &p4.y);
				rects[i].PB(L(p1, p2));
				rects[i].PB(L(p2, p3));
				rects[i].PB(L(p3, p4));
				rects[i].PB(L(p4, p1));
			}
			solve();
		}
	}
}

// #define _LOCAL_TEST

int main() {
#ifdef _LOCAL_TEST
	freopen("a.in", "r", stdin);
#endif

	coding();

#ifdef _LOCAL_TEST
	cout << endl;
	cout << "This is local test" << endl;
	cout << "Do not forget to comment out _LOCAL_TEST" << endl;
#endif
}