import sys input = sys.stdin.readline t=int(input()) for _ in range(t): n,l=map(int,input().split()) M=10000 bucket=[[] for i in range(M)] for i in range(n): x,y=map(int,input().split()) bucket[x*M//(10**9+1)].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)