#include <iostream>
#include <complex>
#include <vector>
#include <set>
#include <cstring>
using namespace std;

#define REP(i,n,m) for(int i=n;i<m;i++)
#define rep(i,n) REP(i,0,n)
#define EREP(i,n,m) for(int i=n;i<=m;i++)
#define erep(i,n) EREP(i,0,n)

#define EPS (1e-8)

#define COUNTER_CLOCKWISE 1
#define CLOCKWISE -1
#define ONLINE_BACK 2
#define ONLINE_FRONT -2
#define ON_SEGMENT 0

typedef complex<double> P;

int par[100],rank[100]; //Union-Find

void init(int n){
	rep(i,n){
		par[i] = i;
		rank[i] = 0;
	}
}

int find(int x){
	if(par[x] == x)
		return x;
	else
		return par[x] = find(par[x]);
}

void unite(int x,int y){
	x = find(x);
	y = find(y);
	if(x==y) return;

	if(rank[x] < rank[y])
		par[x] = y;
	else{
		par[y] = x;
		if(rank[x] == rank[y]) rank[x]++;
	}
}

double dot(P a,P b){
	return (a.real() * b.real() + a.imag() * b.imag());
}

double cross(P a,P b){
	return (a.real() * b.imag() - a.imag() * b.real());
}

int ccw(P p0,P p1,P p2){
	P a = p1 - p0;
	P b = p2 - p0;
	if(cross(a,b) > EPS) return COUNTER_CLOCKWISE;
	if(cross(a,b) < -EPS) return CLOCKWISE;
	if(dot(a,b) < -EPS) return ONLINE_BACK;
	if(abs(a) < abs(b)) return ONLINE_FRONT;
	return ON_SEGMENT;
}

bool intersect_ls_ls(P p1,P p2,P p3,P p4){
	return (ccw(p1,p2,p3) * ccw(p1,p2,p4) <= 0 &&
					ccw(p3,p4,p1) * ccw(p3,p4,p2) <= 0);
}

bool inside_p_pol(vector<P> pol,P p){
	rep(i,4){
		if(ccw(pol[i],pol[(i+1)%4],p) == COUNTER_CLOCKWISE) return false;
	}
	return true;
}

bool intersect_pol_pol(vector<P> p1,vector<P> p2){
	rep(i,3) rep(j,3) {
		if(intersect_ls_ls(p1[i],p1[i+1],p2[j],p2[j+1])) return true;
	}
	rep(i,4) if(inside_p_pol(p1,p2[i])) return true;
	rep(i,4) if(inside_p_pol(p2,p1[i])) return true;
	return false;
}

int main(void){
	int n;

	while(cin>>n,n) while(n--) {
		int m; cin>>m;
		vector<P> pol[m];
		init(m);

		rep(i,m){
			rep(j,4){
				P p; cin>>p.real()>>p.imag();
				pol[i].push_back(p);
			}
			rep(j,i) if(intersect_pol_pol(pol[i],pol[j])) unite(i,j);
		}

		set<int> ans;
		rep(i,m) ans.insert(find(i));
		cout<<ans.size()<<endl;
	}

	return 0;
}