#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define reps(i,f,n) for(int i=f; i<int(n); ++i)
#define rep(i,n) reps(i,0,n)

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1001001001;

struct UF
{
	vi par, rank;
	UF(int n) : par(n), rank(n, 0) {
		rep(i, n) par[i] = i;
	}
	
	int find(int x) {
		return par[x]==x ? x : (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;
		rank[x] += rank[x] == rank[y];
	}
	bool same(int x, int y) {
		return find(x) == find(y);
	}
};

namespace std{
	pii operator- (const pii& a, const pii& b){
		return pii(a.first-b.first, a.second-b.second);
	}
	int operator* (const pii& a, const pii& b){
		int tmp = a.first*b.second - a.second*b.first;
		if(tmp<0) return -1;
		if(tmp>0) return 1;
		return 0;
	}
}

int ccw(pii a, pii b, pii c) {
	return (b-a)*(c-a);
}

bool inner(vector<pii>& ps, pii p)
{
	rep(i, 4){
		if(ccw(ps[(i+1)%4], ps[i], p) < 0)
			return false;
	}
	return true;
}

bool cross(vector<pii> A, vector<pii> B)
{
	rep(i, 4) rep(j, 4){
		pii a = B[(i+1)%4] - B[i];
		pii b = A[j] - B[i];
		pii c = A[(j+1)%4] - B[i];
		pii d = A[(j+1)%4] - A[j];
		pii e = B[i] - A[j];
		pii f = B[(i+1)%4] - A[j];
		if((a*b)*(a*c) < 0 && (d*e)*(d*f) < 0){
			return true;
		}
	}
	return inner(A, B[0]) || inner(B, A[0]);
}

int main()
{
	int n;
	while(scanf("%d", &n), n){
		rep(i, n){
			int m;
			scanf("%d", &m);

			vector<pii> rect[100];
			rep(j, m){
				rect[j].resize(4);
				rep(k, 4)
					scanf("%d%d", &rect[j][k].first, &rect[j][k].second);
			}

			UF uf(m);
			int ans = m;
			rep(j, m) reps(k, j+1, m){
				if(!uf.same(j, k) && cross(rect[j], rect[k])){
					uf.unite(j, k);
					--ans;
				}
			}
			printf("%d\n", ans);
		}
	}
	return 0;
}