import sys input = sys.stdin.readline t=int(input()) for _ in range(t): n,l=map(int,input().split()) M=int(n**0.5)+1 bucket=[[] for i in range(M)] xy=[] X=[] for i in range(n): x,y=map(int,input().split()) xy.append((x,y)) xy.sort() for i in range(n): x,y=xy[i] bucket[i*M//n].append((x,y)) for i in range(M): if i&1: bucket[i].sort(key=lambda x:-x[1]) else: bucket[i].sort(key=lambda x:x[1]) ans=[] for i in range(M): for j in bucket[i]: ans.append(j) print(n) for i in ans: print(*i)