def solveans(dat): last=dat[0][1] ans=L for i in range(1,N): ans+=abs(last-dat[i][1]) ans+=dat[i][-1] return ans from collections import defaultdict import math T=int(input()) for _ in range(T): N,L=map(int,input().split()) dat=defaultdict(list) sl=(L//math.sqrt(N)) for _ in range(N): x,y=map(int,input().split()) dat[(y//sl)].append([x,y]) count=0 keys=sorted(dat.keys()) for k in keys: if count%2==0: dat[k].sort() else: dat[k].sort(reverse=True) count+=1 ans=[] for k in keys: for d in dat[k]: ans.append(d) print(len(ans)) for a,b in ans: print(a,b)