// Arup Guha
// 11/16/2015
// Solution to 2015 SER D1/D2 Problem: Hilbert Sort

import java.util.*;

public class hilbert {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int n = stdin.nextInt();
		int s = stdin.nextInt();

		// Read in and create points.
		pt[] list = new pt[n];
		for (int i=0; i<n; i++) {
			int x = stdin.nextInt();
			int y = stdin.nextInt();
			list[i] = new pt(x, y, s, 0);
		}

		// Sort it.
		Arrays.sort(list);

		// Print it.
		for (int i=0; i<n; i++)
			System.out.println(list[i]);
	}
}

class pt implements Comparable<pt> {

	// ORDERINGS BASED ON YOUR INCOMING DIRECTION
	final public static int[][] PATTERN = { {0, 1, 2, 3},	// UP
											{0, 3, 2, 1},	// FACING RIGHT
											{2, 3, 0, 1},	// DOWN
											{2, 1, 0, 3} };	// FACING DOWN
	public double x;
	public double y;
	public double s;
	public int dir;

	public pt(double myx, double myy, double mys, int myd) {
		x = myx;
		y = myy;
		s = mys;
		dir = myd;
	}

	public int compareTo(pt other) {

		// Figure out which "real" quadrant each item is in.
		int thisQ = this.getQuadrant();
		int otherQ = other.getQuadrant();

		// Easy case - we break the tie now.
		if (thisQ != otherQ) {
			for (int i=0; i<4; i++) {
				if (PATTERN[dir][i] == thisQ) return -1;
				if (PATTERN[dir][i] == otherQ) return 1;
			}
		}

		// Recursive case.
		return this.div2().compareTo(other.div2());
	}

	// Return the quadrant of this point.
	public int getQuadrant() {
		if (x < s/2 && y < s/2) return 0;
		if (x < s/2 && y > s/2) return 1;
		if (x > s/2 && y > s/2) return 2;
		return 3;
	}

	// Returns this point represented in within the smaller sphere of its current quadrant.
	public pt div2() {

		// Set up recursive quadrant.
		double newx = x > s/2 ? x - s/2 : x;
		double newy = y > s/2 ? y - s/2 : y;
		double news = s/2;
		int index = -1;
		int newd = dir;
		int q = getQuadrant();

		// Find index in pattern.
		for (int i=0; i<4; i++)
			if (PATTERN[dir][i] == q)
				index = i;

		// Only times we change our direction, either adding or subtracting 1.
		if (index == 0) newd = dir%2 == 0 ? (dir+1)%4 : (dir+3)%4;
		if (index == 3) newd = dir%2 == 0 ? (dir+3)%4 : (dir+1)%4;

		// Our new point.
		return new pt(newx, newy, s/2, newd);
	}

	// So a point prints out as an integer.
	public String toString() {
		return (int)(x+1e-9)+" "+(int)(y+1e-9);
	}
}