//01
#include<iostream>
#include<complex>
#include<algorithm>
#include<set>
#include<cmath>

using namespace std;

typedef complex<double> P;

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

struct R{
  P p[4];
  bool in(R a){
    for(int i=0;i<4;i++){
      bool sn[2]={};
      for(int j=0;j<4;j++){
	double c=cr(a.p[i]-p[j],p[(j+1)%4]-p[j]);
	if(fabs(c)>1e-9){
	  sn[c<0]=true;
	}
      }
      if(sn[0]&&sn[1])return false;
    }
    return true;
  }
};

int pr[100];

int find(int x){
  return (x==pr[x])?x:pr[x]=find(pr[x]);
}

void unite(int a,int b){
  pr[find(a)]=find(b);
}

bool ois(double a,double b,double c,double d){
  return d-a>1e-9&&b-c>1e-9;
}

int main(){
  for(int t;cin>>t,t;){
    while(t--){
      int n;
      cin>>n;
      R p[100];
      for(int i=0;i<n;i++){
	pr[i]=i;
      }
      for(int i=0;i<n;i++){
	for(int j=0;j<4;j++){
	  int x,y;
	  cin>>x>>y;
	  p[i].p[j]=P(x,y);
	}
	for(int j=0;j<i;j++){
	  if(p[i].in(p[j])||p[j].in(p[i]))goto con;
	  for(int k=0;k<4;k++){
	    P a=p[i].p[k],b=p[i].p[(k+1)%4];
	    for(int l=0;l<4;l++){
	      P c=p[j].p[l],d=p[j].p[l];
	      if(!ois(a.real(),b.real(),c.real(),d.real())
		 &&!ois(a.imag(),b.imag(),c.imag(),d.imag()))continue;
	      if(cr(a-b,c-b)*cr(a-b,d-b)<=1e-9&&cr(c-d,a-d)*cr(c-d,b-d)<=1e-9)goto con;
	    }
	  }
	  continue;
	con:
	  unite(i,j);
	}
      }
      set<int> s;
      for(int i=0;i<n;i++){
	s.insert(find(i));
      }
      cout<<s.size()<<endl;
    }
  }
  return 0;
}
    