#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <utility>
using namespace std;

#define x first
#define y second

typedef pair<double,double> pdd;
const double EPS = 1e-9;

int uf[120];
int Rank[120];

void uf_init() {
	for(int i = 0; i < 120; i++) {
		uf[i] = i;
		Rank[i] = 0;
	}
}
int find(int a) {
	if(uf[a] == a) return a;
	return find(uf[a]);
}
bool same(int a, int b) {
	int aa, bb;
	aa = find(a);
	bb = find(b);
	if(aa == bb)
		return true;
	return false;
}
void merge(int a, int b) {
	if(same(a,b))
		return;
	int aa, bb;
	aa = find(a);
	bb = find(b);
	if(Rank[aa] > Rank[bb]) {
		uf[bb]=aa;
	}
	else {
		if(Rank[aa]==Rank[bb])
			Rank[bb]++;
		uf[aa]=bb;
	}
}

template<typename T>
T sqr(T x) { return x * x; }

double cross(pdd a, pdd b) {
	return a.x * b.y - a.y * b.x;
}
double dot(pdd a, pdd b) {
	return a.x*b.x + a.y*b.y;
}

bool intersect(pdd a0, pdd a1, pdd b0, pdd b1) {
	pdd a, b, c;
	a = pdd(a1.x-a0.x, a1.y-a0.y);
	b = pdd(b0.x-a0.x, b0.y-a0.y);
	c = pdd(b1.x-a0.x, b1.y-a0.y);
	if(fabs(cross(b,c)) < EPS) {
		return dot(b,c) < EPS;
	}
	double s, t;
	s = cross(a,b) / cross(c,b);
	t = cross(c,a) / cross(c,b);
	if(s+t+EPS > 1.0 && s > -EPS && t > -EPS) {
		return true;
	}
	return false;
}

bool in(vector<pdd> rec, pdd pt) {
	int neg = 0;
	int pos = 0;
	int sz = rec.size();
	for(int i = 0; i < sz; i++) {
		pdd a, b;
		a = pdd(rec[i].x-pt.x, rec[i].y-pt.y);
		b = pdd(rec[(i+1)%sz].x-pt.x, rec[(i+1)%sz].y-pt.y);
		neg += cross(a,b) < EPS;
		pos += cross(a,b) > -EPS;
	}
	if(neg == sz || pos == sz) {
		return true;
	}
	return false;
}

int main() {
	int N;
	while(cin >> N, N) {
		for(int i = 0; i < N; i++) {
			int M;
			cin >> M;
			vector<vector<pdd> > v;
			v.resize(M);
			for(int j = 0; j < M; j++) {
				v[j].resize(4);
				for(int k = 0; k < 4; k++) {
					cin >> v[j][k].x >> v[j][k].y;
				}
			}
			uf_init();

			for(int j = 0; j < v.size(); j++) {
				for(int k = 0; k < v.size(); k++) {
					if(j!=k){
						bool flg = false;
						for(int l = 0; l < v[j].size(); l++) {
							flg |= in(v[k], v[j][l]);
							for(int m = 0; m < v[k].size(); m++) {
								flg |= intersect(v[j][l], v[j][(l+1)%v[j].size()],
										v[k][m], v[k][(m+1)%v[k].size()]);
							}
						}
						if(flg) {
							merge(j, k);
						}
					}
				}
			}

			int res = 0;
			for(int i = 0; i < M; i++) {
				res += i == find(i);
			}
			cout << res << endl;
		}
	}
}