#include<iostream>
#include<map>
#include<algorithm>
#include<vector>
using namespace std;

template<class T>
class DisjointSet {
public:
    map<T, T> parent;
    map<T, int> rank;
    void make(T node) {
        parent[node] = node;
        rank[node] = 0;
    }
    void unite(T node1, T node2) {
        if(rank[node1] < rank[node2]) swap(node1, node2);
        parent[node2] = node1;
        if(rank[node1] == rank[node2]) rank[node1]++;
    }
    T find(T node) {
        return node == parent[node] ? parent[node] : parent[node] = find(parent[node]);
    }
};

class Point {
public:
    int x, y;
    Point() {x = y = 0;}
    Point(int a, int b): x(a), y(b) {}
    int outer_product(Point p) {return x * p.y - y * p.x;}
    Point operator-(Point p) {return Point(x - p.x, y - p.y);}
    bool operator<(Point p) const {return x != p.x ? x < p.x : y < p.y;}
    void operator=(Point p) {x = p.x; y = p.y;}
};

class Quadrilateral {
public:
    vector<Point> point;
    Quadrilateral() {point = vector<Point>(4, Point());}
    Quadrilateral(vector<Point> p): point(p) {}
    bool contain(Point p) {
        int plus = 0, minus = 0;
        for(int i=0; i<4; i++) {
            Point edge = point[(i+1)%4] - point[i];
            Point target = p - point[i];
            int sign = edge.outer_product(target);
            if(!sign) {plus++; minus++;}
            else {sign > 0 ? plus++ : minus++;}
        }
        return (plus == 4) || (minus == 4);
    }
    bool contain(Quadrilateral q) {
        for(int i=0; i<4; i++) if(contain(q.point[i])) return true;
        return false;
    }
    bool operator<(Quadrilateral q) const {
        for(int i=0; i<4; i++) {
            if(point[i] < q.point[i]) return true;
            if(q.point[i] < point[i]) return false;
        }
        return false;
    }
};

int main()
{
    int N, M;
    while(cin>>N, N) {
        while(N-->0) {
            vector<Quadrilateral> quad;
            cin>>M;
            for(int i=0; i<M; i++) {
                vector<Point> point;
                for(int j=0; j<4; j++) {
                    int x, y;
                    cin>>x>>y;
                    point.push_back(Point(x, y));
                }
                quad.push_back(point);
            }
            DisjointSet<Quadrilateral> group;
            for(int j=0; j<M; j++) {
                group.make(quad[j]);
                for(int k=0; k<j; k++) {
                    if(quad[j].contain(quad[k]) || quad[k].contain(quad[j])) group.unite(quad[j], quad[k]);
                }
            }
            map<Quadrilateral, bool> root;
            for(int j=0; j<M; j++) root.insert(make_pair(group.parent[quad[j]], true));
            cout<<root.size()<<endl;
        }
    }
}