for _ in range(int(input())): n,l = map(int,input().split()) xy = sorted(tuple(map(int,input().split())) for _ in range(n)) a = []; m = int(n**0.5)+1; print(n) for i in range(m): a += sorted(xy[i*m:(i+1)*m],key=lambda x:x[1]) for x,y in a: print(x,y)