T = int(input()) for _ in range(T): N, L = map(int, input().split()) XY = [list(map(int, input().split())) for _ in range(N)] XY.sort(key=lambda x:x[1]) RANGE = int(N**0.5)+1 qS = [[] for _ in range(N//RANGE+1)] for i in range(N): X, Y = XY[i] qS[i//RANGE].append((X, Y)) for i in range(N//RANGE+1): qS[i].sort(key=lambda x:x[0], reverse=i%2) ans = [] for i in range(N//RANGE+1): for x, y in qS[i]: ans.append((x, y)) print(len(ans)) for x, y in ans: print(x, y)