from __future__ import print_function import time import sys from math import pi sys.setrecursionlimit(100002) #INFILE = open('1.in') INFILE=sys.stdin class Tarjan: # 求无向连通图的桥 @staticmethod def getCuttingPointAndCuttingEdge(edges): link, dfn, low = {}, {}, {}# link为字典邻接表 global_time = [0] for a, b in edges: if a not in link: link[a] = [] if b not in link: link[b] = [] link[a].append(b)#无向图 link[b].append(a)#无向图 dfn[a], dfn[b] = 0x7fffffff, 0x7fffffff low[a], low[b] = 0x7fffffff, 0x7fffffff cutting_points, cutting_edges = [], [] def dfs(cur, prev, root): global_time[0] += 1 dfn[cur], low[cur] = global_time[0], global_time[0] children_cnt = 0 flag = False for next in link[cur]: if next != prev: if dfn[next] == 0x7fffffff: children_cnt += 1 dfs(next, cur, root) if cur != root and low[next] >= dfn[cur]: flag = True low[cur] = min(low[cur], low[next]) if low[next] > dfn[cur]: cutting_edges.append([cur, next] if cur < next else [next, cur]) else: low[cur] = min(low[cur], dfn[next]) if flag or (cur == root and children_cnt >= 2): cutting_points.append(cur) dfs(edges[0][0], None, edges[0][0]) return cutting_points, cutting_edges # class Solution: # def criticalConnections(self, n: int, connections: List[List[int]]) -> List[List[int]]: # edges = [(a, b) for a, b in connections] # cutting_dots, cutting_edges = Tarjan.getCuttingPointAndCuttingEdge(edges) # return [[a, b] for a, b in cutting_edges] def solve(n,l,lx): # print(n,l,lx) if l==1: return 1 sta=[[] for _ in range(n+1)] for i,li in enumerate(lx): for a in li: sta[a].append(i) used=[0]*l edges=set() for i in range(1,n+1): if len(sta[i])==1: used[sta[i][0]]=1 else: sta[i].sort() for j in range(len(sta[i])): for k in range(j+1,len(sta[i])): edges.add((sta[i][j],sta[i][k])) led=list(edges) cutdot,_=Tarjan.getCuttingPointAndCuttingEdge(led) for a in cutdot: used[a]=1 return sum(used) def read_input(fi): read = lambda type: type(fi.readline().rstrip('\n').rstrip('\r')) readArray = lambda type: list(map(type, fi.readline().split())) readMatrix = lambda type, x: [list(map(type, fi.readline().split())) for i in range(x)] readLines = lambda type, x: [type(fi.readline().rstrip('\n').rstrip('\r')) for i in range(x)] n,l=readArray(int) lx=[] for i in range(l): _ = read(str) lx.append(readArray(int)) return (n,l,lx) def main(): time0 = time.time() t = int(INFILE.readline()) # _ = int(INFILE.readline()) for ti in range(t): time1 = time.time() ans = "Case #%d: %s" % (ti + 1, solve(*read_input(INFILE))) sys.stderr.write("%.3f\t" % (time.time() - time1)) print(ans) sys.stderr.write("%.3f\n" % (time.time() - time0)) if INFILE != sys.stdin: INFILE.close() if __name__ == '__main__': main() # print (check('RSP'*20)) # print(check('R'*20+'S'*20+'P'*20)) # print(check('R'*20+'S'*30+'P'*10)) # print(test(16)) # print(test1())