#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXNAME 30

typedef struct Grade
{
    char s[MAXNAME];
    int p;
} Grade;
// http://c.biancheng.net/view/232.html
// https://www.cnblogs.com/flipped/p/5005627.html
// https://blog.csdn.net/u010968153/article/details/52807435

int cmp1(const void *a, const void *b)
{
    return (*(Grade *)a).p > (*(Grade *)b).p;
}

int cmp0(const void *a, const void *b)
{
    return (*(Grade *)a).p < (*(Grade *)b).p;
}

int main()
{
    int Num, Flag;
    while (scanf("%d%d", &Num, &Flag) != EOF)
    {
        Grade G[Num];
        for (int i = 0; i < Num; i++)
            scanf("%s%d", G[i].s, &G[i].p);
        //以上即将所有数据录入

        if (Flag)
            qsort(G, Num, sizeof(Grade), cmp1);
        else
            qsort(G, Num, sizeof(Grade), cmp0);
        //排序过程

        for (int i = 0; i < Num; i++)
        {
            printf("%s %d\n", G[i].s, G[i].p);
        }
    }
    return 0;
}