import bisect import sys input = sys.stdin.readline T = int(input()) for _ in range(T): N, L = map(int, input().split()) DIV = int(L/(N**0.5)) XY = [list(map(int, input().split())) for _ in range(N)] Z = [[[] for _ in range(DIV+1)] for _ in range(DIV+1)] D = [0 for _ in range(DIV+1)] D[DIV] = L+1 for i in range(1, DIV): D[i] = int(L/DIV*i) for i in range(N): x, y = XY[i] xpos = bisect.bisect_left(D, x) ypos = bisect.bisect_left(D, y) Z[xpos][ypos].append(i) ans = [] for i in range(DIV+1): for j in range(DIV+1): if i%2==0: k = j else: k = DIV-j for l in Z[i][k]: ans.append(XY[l]) print(len(ans)) for a in ans: print(*a)