import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = Integer.valueOf(scan.nextLine().trim());
        int sign = Integer.valueOf(scan.nextLine().trim());
        String[][] nameList = new String[n][2];
        for (int i = 0; i < n; i++) {
            String[] ns =  scan.nextLine().split(" ");
            nameList[i][0] = ns[0];
            nameList[i][1] = ns[1];
        }
        mergeSort(nameList, sign);
        for (String[] ns : nameList) {
            System.out.println(ns[0] + " " + ns[1]);
        }
    }
    public static void mergeSort(String[][] strs, int sign) {
        if (null == strs || strs.length < 2) {
            return;
        }
        process(strs, 0, strs.length - 1, sign);
    }
    public static void process(String[][] strs, int start, int end, int sign) {
        if (start >= end) {
            return;
        }
        int mid = start + ((end - start) >> 1);
        process(strs, start, mid, sign);
        process(strs, mid + 1, end, sign);
        if (sign == 1) {
            merge1(strs, start, mid, end);
        } else {
            merge2(strs, start, mid, end);
        }
    }
    public static void merge1(String[][] strs, int start, int mid, int end) {
        String[][] helper = new String[end - start + 1][2];
        int index = 0;
        int p1 = start;
        int p2 = mid + 1;
        while (p1 <= mid && p2 <= end) {
            if (Integer.valueOf(strs[p1][1]) <= Integer.valueOf(strs[p2][1])) {
                helper[index][0] = strs[p1][0];
                helper[index][1] = strs[p1][1];
                index++;
                p1++;
            } else {
                helper[index][0] = strs[p2][0];
                helper[index][1] = strs[p2][1];
                index++;
                p2++;
            }
        }
        while (p1 <= mid) {
            helper[index][0] = strs[p1][0];
            helper[index][1] = strs[p1][1];
            index++;
            p1++;
        }
        while (p2 <= end) {
            helper[index][0] = strs[p2][0];
            helper[index][1] = strs[p2][1];
            index++;
            p2++;
        }
        for (int i = 0; i < helper.length; i++) {
            strs[start + i][0] = helper[i][0];
            strs[start + i][1] = helper[i][1];
        }
    }
    public static void merge2(String[][] strs, int start, int mid, int end) {
        String[][] helper = new String[end - start + 1][2];
        int index = 0;
        int p1 = start;
        int p2 = mid + 1;
        while (p1 <= mid && p2 <= end) {
            if (Integer.valueOf(strs[p1][1]) >= Integer.valueOf(strs[p2][1])) {
                helper[index][0] = strs[p1][0];
                helper[index][1] = strs[p1][1];
                index++;
                p1++;
            } else {
                helper[index][0] = strs[p2][0];
                helper[index][1] = strs[p2][1];
                index++;
                p2++;
            }
        }
        while (p1 <= mid) {
            helper[index][0] = strs[p1][0];
            helper[index][1] = strs[p1][1];
            index++;
            p1++;
        }
        while (p2 <= end) {
            helper[index][0] = strs[p2][0];
            helper[index][1] = strs[p2][1];
            index++;
            p2++;
        }
        for (int i = 0; i < helper.length; i++) {
            strs[start + i][0] = helper[i][0];
            strs[start + i][1] = helper[i][1];
        }
    }
}