def build_graph(lines): graph = {i: set() for i in range(1, n+1)} for i, line in enumerate(lines, 1): for j in range(len(line)-1): u, v = line[j], line[j+1] graph[u].add((i, v)) graph[v].add((i, u)) return graph def dfs(graph, visited, node): visited.add(node) for edge in graph[node]: if edge[0] not in visited: dfs(graph, visited, edge[1]) def count_connected_components(graph): visited = set() count = 0 for node in graph: if node not in visited: dfs(graph, visited, node) count += 1 return count t = int(input()) for i in range(1, t+1): n, l = map(int, input().split()) lines = [list(map(int, input().split()))[1:] for _ in range(l)] graph = build_graph(lines) num_components = count_connected_components(graph) num_essential = 0 for i, line in enumerate(lines, 1): for j in range(len(line)-1): u, v = line[j], line[j+1] graph[u].discard((i, v)) graph[v].discard((i, u)) new_num_components = count_connected_components(graph) if new_num_components > num_components: num_essential += 1 graph[u].add((i, v)) graph[v].add((i, u)) print("Case #{}: {}".format(i, num_essential))