#include <set>
#include <cstdio>
#include <queue>
#include <iostream>
#include <complex>
#include <vector>
#include <climits>
#include <cfloat>

#define REP(i,n) for(int i=0; i<(int)(n); i++)

using namespace std;

typedef complex<double> P;
typedef vector<P> Polygon;
const double EPS = 1e-9;

struct S{
  P p1;
  P p2;
  S(P p,P q) : p1(p), p2(q) {}
};

bool intersect(const S &s, const P &p){
  return std::abs(abs(s.p1 - p) + abs(s.p2 - p) - abs(s.p2 - s.p1)) < EPS;
}

bool contain(const Polygon &g, const P &p){
  double sum = 0.0;
  int n = g.size();
  for(int i = 0; i < n; i++){
    int j = (i + 1) % n;
    if(intersect(S(g[i], g[j]), p))
      return true;
    sum += arg((g[j] - p) / (g[i] - p));
  }
  return std::abs(sum) > 1.0; // 0 or 2 PI
}

bool intersect(const Polygon &p1, const Polygon &p2){
  int n = p2.size();
  int m = p1.size();
  for(int i = 0; i < n; i++)
    if(contain(p1, p2[i])) return true;
  for(int i = 0; i < m; i++)
    if(contain(p2, p1[i])) return true;
  return false;
}

class UnionFind{
private:
  vector<int> id;
  int cnt;

  int getId(int i){
    if(i == id[i]) return i;
    return id[i] = getId(id[i]);
  }
public:
  UnionFind(int size = 0){ init(size); }
  ~UnionFind(){}
  void init(int size){
    id = vector<int>(size);
    cnt = size;
    for(int i = 0; i < size; i++)
      id[i] = i;
  }
  void unite(int i, int j){
    int next = min( getId(i), getId(j) );
    if(getId(i) != getId(j)) cnt--;
    id[getId(i)] = id[getId(j)] = next;
  }
  int operator [](int i){ return getId(i); }
  int count(){
    set<int> s;
    for(int i = 0; i < (int)id.size(); i++)
      s.insert(getId(i));
    while(cnt != s.size());
    return s.size();
  }
};

int main(){
  int t;
  while(scanf("%d", &t), t){
    while(t --> 0){
      int n; scanf("%d", &n);
      UnionFind uf(n);
      vector<Polygon> ps(n);

      REP(i,n){
        vector<P> pos(4);
        REP(j,4) scanf("%lf%lf", &pos[j].real(), &pos[j].imag());
        ps[i] = pos;

        REP(j,i) if(intersect(ps[i], ps[j])){
          //printf("cross: %d %d\n", i, j);
          uf.unite(i, j);
        }
      }

      printf("%d\n", uf.count());
    }
  }
  return 0;
}