from math import sqrt def solve(): N, L = map(int, input().split()) C = max(1, (int(sqrt(2 * (L * L) // N)))) ls = [[] for _ in range(L // C + 2)] for _ in range(N): x, y = map(int, input().split()) ls[y // C].append((x, y)) for i in range(L // C + 2): if len(ls[i]) == 0: continue ls[i].sort() for x, y in ls[i]: print(x, y) T = int(input()) for _ in range(T): solve()