#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
using namespace std;
static const double EPS = 1e-8;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define sz(a) a.size()
#define all(a) a.begin(),a.end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define SS stringstream
#define DBG1(a) rep(_X,sz(a)){printf("%d ",a[_X]);}puts("");
#define DBG2(a) rep(_X,sz(a)){rep(_Y,sz(a[_X]))printf("%d ",a[_X][_Y]);puts("");}
#define bitcount(b) __builtin_popcount(b)
#define REP(i, s, e) for ( int i = s; i <= e; i++ )  

const double INF = 1e12;
typedef complex<double> P,point;
typedef vector<P> G,polygon;
namespace std {bool operator < (const P& a, const P& b) {return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);}}
double cross(const P& a, const P& b) {return imag(conj(a)*b);}
double dot(const P& a, const P& b) {return real(conj(a)*b);}
struct L : public vector<P> {L(const P &a, const P &b) {push_back(a); push_back(b);}};
int ccw(P a, P b, P c) {b -= a; c -= a;if (cross(b, c) > 0)   return +1;if (cross(b, c) < 0)   return -1;if (dot(b, c) < 0) return +2;if (norm(b) < norm(c)) return -2;return 0;}

// Check Funcs //
bool intersectSS(const L &s, const L &t) {return ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= 0 && ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= 0;}

// Where is Point in Polygon? //
#define curr(P, i) P[i]
#define next(P, i) P[(i+1)%P.size()]
enum { OUT, ON, IN };
int contains(const polygon& P, const point& p) {bool in = false;for (int i = 0; i < P.size(); ++i) {point a = curr(P,i) - p, b = next(P,i) - p;if (imag(a) > imag(b)) swap(a, b);if (imag(a) <= 0 && 0 < imag(b))if (cross(a, b) < 0) in = !in;if (cross(a, b) == 0 && dot(a, b) <= 0) return ON;}return in ? IN : OUT;}

/* graph check*/
int n,m;
int done[100];
bool info[100][100];

void dfs(int s){
	if(done[s])return;
	else done[s] = true;
	rep(i,m){
		if(info[s][i])dfs(i);
	}
}

int main(){
	while(cin >> n,n){
		rep(z,n){
			rep(i,100)rep(j,100)info[i][j]=false;
			cin >> m;
			vector<G> g(m); 
			rep(i,m){
				rep(j,4){
					double x,y;
					cin >> x >> y;
					g[i].push_back(P(x,y));
				}
			}
			/* cross check */
			rep(i,m){
				rep(j,m){
					rep(X,4)rep(Y,4){
						if( intersectSS(
							 L(g[i][X],g[i][(X+1)%4]),
							 L(g[j][Y],g[j][(Y+1)%4])
						    )
						){
							info[i][j] = info[j][i] = true;
						}
					}
				}
			}
			/* contain check */
			rep(i,m){
				rep(j,m){
					rep(k,4){
						if(contains(g[i],g[j][k]))
							info[i][j] = info[j][i] = true;	
					}
				}
			}
			rep(i,m)done[i] = false;
			int cnt = 0;
			rep(i,m)if(!done[i])cnt++,dfs(i);
			cout << cnt << endl;
		}
	}
}