import random # Find a cycle in the graph using depth-first search def find_cycle(graph, visited, start, prev): visited.add(start) for neighbor in graph[start]: if neighbor == prev: continue if neighbor in visited: return [neighbor, start] cycle = find_cycle(graph, visited, neighbor, start) if cycle: return cycle + [start] return None # Generate a ring-preserving network and find a ring structure def solve_case(C, L): # Create a ring structure links = [(i, i % C + 1) for i in range(1, C + 1)] # Add random links while len(links) < L: a, b = random.sample(range(1, C + 1), 2) if (a, b) not in links and (b, a) not in links: links.append((a, b)) # Build graph graph = {i: set() for i in range(1, C + 1)} for a, b in links: graph[a].add(b) graph[b].add(a) # Permute IDs perm = list(range(1, C + 1)) random.shuffle(perm) links = [(perm[a - 1], perm[b - 1]) for a, b in links] links.sort() # Find cycle in permuted graph cycle = find_cycle(graph, set(), perm[0], None) if cycle[0] != cycle[-1]: cycle += cycle[1:] # Output result return ' '.join(str(perm[i - 1]) for i in cycle) # Read number of test cases T = int(input()) # Solve each test case for t in range(T): C, L = map(int, input().split()) result = solve_case(C, L) print(result)