Problem:


秋のイルミネーション

 四角形が大好きな人が多く住む四角町では、四角形の電光板を組み合わせたイルミネーションで町中を彩るお祭りが開催されます。この電光板は電気を流すと発光し、発光している板に接している板も発光します。したがって、電光板を何枚使っても、すべてがひとかたまりに接していれば電源がひとつで済むという優れものです。
 

お祭り実行委員会では、四角形を組み合わせたデザインを町民から募集し多数のイルミネーションを作成します。各イルミネーションは、デザインを構成する四角形の配置によって必要となる電源の個数が変わるため、必要な電源の個数を把握する作業に大きな手間がかかります。そこで、あなたは四角形の配置を調べ、必要な電源の個数を計算するプログラムを作成して実行委員会を手助けすることにしました。

イルミネーションの個数、各イルミネーションを構成する四角形の情報を入力とし、イルミネーションごとに必要な電源の個数を出力するプログラムを作成してください。重なっていたり接していたりする四角形を 1 つのまとまりと考え、1 つのまとまりに対して電源の個数は 1 つと数えてください。

Input

複数のデータセットの並びが入力として与えられます。入力の終わりはゼロひとつの行で示されます。各データセットは以下の形式で与えられます。

N
M1
xa1 ya1 xb1 yb1 xc1 yc1 xd1 yd1
xa2 ya2 xb2 yb2 xc2 yc2 xd2 yd2
:
xaM1 yaM1 xbM1 ybM1 xcM1 ycM1 xdM1 ydM1
M2
:
:
MN
:
:

1 行目にイルミネーションの個数 N (1 ≤ N ≤ 100) が与えられます。続いて、N 個のイルミネーションの情報が与えられます。第 i のイルミネーションの情報として、イルミネーションを構成する四角形の個数 Mi (1 ≤ Mi ≤ 100) が１行に与えられます。続く Mi 行に j 個目の四角形の頂点 xaj, yaj, xbj, ybj, xcj, ycj, xdj, ydj (-1000 以上 1000 以下の整数) が与えられます。

入力される四角形の頂点座標は、時計回りの順に従って入力されます。ただし、入力データの四角形はすべて凸な四角形とします。

  

データセットの数は 10 を超えません。

Output

入力データセットごとに、イルミネーションごとに必要な電源の個数を出力します。出力する電源の個数は各イルミネーションが入力された順番に従ってください。

Sample Input

3
1
0 0 0 1 1 1 1 0
2
0 0 0 1 1 1 1 0
100 100 100 200 200 200 200 100
2
0 0 0 100 100 100 100 0
10 10 10 20 20 20 20 10
2
1
30 40 40 50 40 20 20 10
1
30 40 40 50 40 20 20 10
0

Output for the Sample Input

1
2
1
1
1



Sample input:
3
1
0 0 0 1 1 1 1 0
2
0 0 0 1 1 1 1 0
100 100 100 200 200 200 200 100
2
0 0 0 100 100 100 100 0
10 10 10 20 20 20 20 10
2
1
30 40 40 50 40 20 20 10
1
30 40 40 50 40 20 20 10
0


Sample output:
1
2
1
1
1

Submission status: Runtime Error

Solution code:

# def chk_in_rect(xy1,xy2):
#     chk2in1 = True
#     for i in range(4):
#         for j in range(4):
#             a = (xy1[(2*i+2)&7]-xy1[(2*i  )&7]) * (xy2[(2*j+1)&7]-xy1[(2*i+1)&7])
#             b = (xy1[(2*i+3)&7]-xy1[(2*i+1)&7]) * (xy2[(2*j  )&7]-xy1[(2*i  )&7])
#             if a > b: chk2in1 = False; break
#     chk1in2 = True
#     for i in range(4):
#         for j in range(4):
#             a = (xy2[(2*i+2)&7]-xy2[(2*i  )&7]) * (xy1[(2*j+1)&7]-xy2[(2*i+1)&7])
#             b = (xy2[(2*i+3)&7]-xy2[(2*i+1)&7]) * (xy1[(2*j  )&7]-xy2[(2*i  )&7])
#             if a > b: chk2in1 = False; break
#     if chk2in1|chk1in2: return True
#     else: return False

# def chk_intersect(xy1,xy2):
#     for i in range(4):
#         for j in range(4):
#             a = (xy1[(2*i  )&7]-xy1[(2*i+2)&7]) * (xy2[(2*j+1)&7]-xy1[(2*i+3)&7])
#             a+= (xy1[(2*i+1)&7]-xy1[(2*i+3)&7]) * (xy2[(2*i  )&7]-xy1[(2*j  )&7])
#             b = (xy1[(2*i  )&7]-xy1[(2*i+2)&7]) * (xy2[(2*j+3)&7]-xy1[(2*i+3)&7])
#             b+= (xy1[(2*i+1)&7]-xy1[(2*i+3)&7]) * (xy2[(2*i  )&7]-xy1[(2*j+2)&7])
#             if a*b <= 0 : return True
#     else:
#         return False

while(True):
    n = int(input())
    if not n: break
    for _ in range(n):
        m = int(input())
        # p = list(range(m))
        xys = [ input() for i in range(m) ]
        print(1)
        continue
        # # print(n,m,xys)
        # for i in range(m):
        #     if len(set(p)) == 1: print(1); break
        #     for j in range(i+1,m):
        #         if p[i] == p[j]:
        #             continue
        #         if chk_in_rect(xys[i],xys[j]):
        #             # print("in_rect")
        #             p = [i if e==j else e for e in p]
        #             continue
        #         if chk_intersect(xys[i],xys[j]):
        #             # print("intersect")
        #             p = [i if e==j else e for e in p]
        #             continue
        # else: print(p,len(set(p)))

Submission status: Runtime Error

Solution code:
while(True):
    n = int(input())
    if not n: break
    for _ in range(n):
        m = int(input())
        # p = list(range(m))
        xys = [ map(int,input().split()) for i in range(m) ]
        print(1)
        continue

Submission status: Runtime Error

Solution code:
while(True):
    n = int(input())
    if not n: break
    for _ in range(n):
        m = int(input())
        xys = [ map(int,input().split()) for i in range(m) ]
        print(1)

Submission status: Runtime Error

Solution code:
while(True):
    n = int(input())
    if n == 0: break
    for _ in range(n):
        m = int(input())
        xys = [ map(int,input().split()) for i in range(m) ]
        print(1)

Submission status: Runtime Error

Solution code:
while(True):
    n = int(input())
    if n == 0: break
    for _ in range(n):
        m = int(input())
        xys = [ map(str,input().split()) for i in range(m) ]
        print(1)

Submission status: Runtime Error

Solution code:
while(True):
    n = int(input())
    if n == 0: break
    for i in range(n):
        m = int(input())
        xys = [ map(str,input().split()) for i in range(m) ]
        print(1)

Submission status: Runtime Error

Solution code:
while(True):
    n = int(input())
    if n == 0: break
    for i in range(n):
        m = int(input())
        xys = [ input() for i in range(m) ]
        print(1)

Submission status: Runtime Error

Solution code:
while(True):
    n = int(input())
    if n == 0: break
    for j in range(n):
        m = int(input())
        xys = [ input() for i in range(m) ]
        print(1)

Submission status: Runtime Error

Solution code:
while(True):
    n = int(input())
    if n == 0: break
    for j in range(n):
        m = int(input())
        xys = [ input() for i in range(m) ]

Submission status: Runtime Error

Solution code:
while(True):
    n = int(input())
    if n == 0: break
    for j in range(n):
        m = int(input())
        xys = [ [input() for k in range(4)] for i in range(m) ]

Submission status: Runtime Error

Solution code:
while(True):
    n = int(input())
    if n == 0: break
    for j in range(n):
        m = int(input())
        xys = [ [input() for k in range(8)] for i in range(m) ]

Submission status: Runtime Error

Solution code:
while(True):
    n = int(input())
    if n == 0: break
    for j in range(n):
        m = int(input())
        xys = [ [input() for k in range(2)] for i in range(m) ]

Submission status: Runtime Error

Solution code:
while(True):
    n = int(input())
    if n == 0: break
    for j in range(n):
        m = int(input())
        xys = [ [input() for k in range(4)] for i in range(m) ]
        print(xys)

Submission status: Runtime Error

Solution code:
while(True):
    n = int(input())
    if n == 0: break
    for j in range(n):
        m = int(input())
        xys = [ [input() for k in range(4)] for i in range(m) ]
        print("1")

Submission status: Runtime Error

Solution code:
while(True):
    n = int(input())
    if not n: break
    for _ in range(n):
        m = int(input())
        xys = [ list(map(int, input().split())) for i in range(m) ]
        print(1)

Submission status: Runtime Error

Solution code:
while(True):
    n = int(input())
    if not n: break
    for _ in range(n):
        m = int(input())
        for i in range(m):
            hoge = list(map(int, input().split()))
        print(1)

Submission status: Runtime Error

Solution code:
while(True):
    try:
        n = int(input())
    except:
        break
    if not n: break
    for _ in range(n):
        m = int(input())
        for i in range(m):
            hoge = list(map(int, input().split()))
        print(1)

Submission status: Runtime Error

Solution code:
while(True):
    try:
        n = int(input())
    except:
        break
    if not n: break
    for _ in range(n):
        m = int(input())
        hoge = [list(map(int, input().split())) for j in range(m)]
        print(1)

Submission status: Runtime Error

Solution code:
while(True):
    try:
        n = int(input())
    except:
        break
    if not n: break
    for _ in range(n):
        m = int(input())
        hoge = [list(map(int, input().split())) for j in range(m)]
        print("1")

Submission status: Runtime Error

Solution code:
from sys import stdin
while(True):
    try:
        n = int(stdin.readline())
    except:
        break
    if not n: break
    for _ in range(n):
        m = int(stdin.readline())
        hoge = [list(map(int, stdin.readline().split())) for j in range(m)]
        print("1")

Submission status: Runtime Error

Solution code:
from sys import stdin
while(True):
    try:
        n = int(stdin.readline())
    except:
        break
    if not n: break
    for _ in range(n):
        m = int(stdin.readline())
        try: hoge = [list(map(int, stdin.readline().split())) for j in range(m)]
        except: break
        print("1")

Submission status: Runtime Error

Solution code:
from sys import stdin
while(True):
    try:
        n = int(stdin.readline())
    except:
        break
    if not n: break
    for _ in range(n):
        m = int(stdin.readline())
        try: hoge = [list(map(int, stdin.readline().split())) for j in range(m)]
        except: excit()
        print("1")

Submission status: Runtime Error

Solution code:
from sys import stdin
stdin.readline()
while(True):
    try:
        n = int(stdin.readline())
    except:
        break
    if not n: break
    for _ in range(n):
        m = int(stdin.readline())
        try: hoge = [list(map(int, stdin.readline().split())) for j in range(m)]
        except: excit()
        print("1")

Submission status: Runtime Error

Solution code:
from sys import stdin
stdin.readline()
while(True):
    try:
        n = int(stdin.readline())
    except:
        break
    if not n: break
    for _ in range(n):
        m = int(stdin.readline())
        try: hoge = [list(map(int, stdin.readline().split())) for j in range(m)]
        except: exit()
        print("1")

Submission status: Runtime Error

Solution code:
from sys import stdin
while(True):
    try:
        n = int(stdin.readline())
    except:
        break
    if not n: break
    for _ in range(n):
        m = int(stdin.readline())
        try: hoge = [list(map(int, stdin.readline().split())) for j in range(m)]
        except: exit()
        print("1")

Submission status: Runtime Error

Solution code:

from sys import stdin
while(True):
    try:
        n = int(stdin.readline())
    except:
        exit()
    if not n: exit()
    for _ in range(n):
        m = int(stdin.readline())
        try: hoge = [list(map(int, stdin.readline().split())) for j in range(m)]
        except: exit()
        print("1\n")

Submission status: Runtime Error

Solution code:
from sys import stdin
readline = stdin.readline



class union_find:
    def __init__(self, size):
        self.table = [-1 for _ in range(size)]

    def union(self, x, y):
        x, y = self.find(x), self.find(y)
        if x == y:
            return True
        if self.table[y] < self.table[x]:
            x, y = y, x

        self.table[x] += self.table[y]
        self.table[y] = x
        return False

    def find(self, x):
        if self.table[x] < 0:
            return x
        self.table[x] = self.find(self.table[x])
        return self.table[x]

    def size(self):
        return sum(1 for i in self.table if i < 0)


# p???????????´????????????????????????????¨??????°???
def in_polygon(xy, p):
    wn = 0
    for i in range(len(p)):
        j = i - 1
        if 0 == (p[i] - p[j]).imag:
            continue
        vt = (xy - p[j]).imag / (p[i] - p[j]).imag
        tmp = p[j] + vt * (p[i] - p[j])
        if xy.real < tmp.real:
            wn += 1 if p[j].imag <= xy.imag < p[i].imag else\
                 -1 if p[i].imag <= xy.imag < p[j].imag else 0
    return wn


from itertools import combinations


def solve(cwp):
    if len(cwp) == 1:
        return 1
    ccwp = [pi[::-1] for pi in cwp]

    uf = union_find(len(ccwp))

    for i, j in combinations(range(len(ccwp)), 2):
        if uf.find(i) == uf.find(j):
            continue
        for ji in ccwp[j]:
            if in_polygon(ji, ccwp[i]):
                uf.union(i, j)

    return uf.size()


def main():
    while True:
        n = int(readline())
        if n == 0:
            break
        for _ in range(n):
            m = int(readline())
            cw_polygons = []
            for _ in range(m):
                l = map(int, readline().split())
                cw_p = [complex(real, imag) for real, imag in zip(*[l] * 2)]
                cw_polygons.append(cw_p)
            print(solve(cw_polygons))
main()