import sys
for line in sys.stdin:
    N=int(line.strip())
    flg=int(input().strip())
    alt=[]
    for i in range(N):
        temp=input().strip().split()
        temp[1]=int(temp[1])#陷阱在这里,比如jim 90,只能单独将90转成数字
        alt+=[temp]
        # print(alt)#测试
    alt.sort(key=lambda item:(item[1]),reverse=not flg)
    for i in alt:
        print(i[0],i[1])