using System;
using System.Collections.Generic;
using System.Linq;
namespace HJ68
{
internal class Student
{
public string Name { get; set; }
public int Sorce { get; set; }
public Student()
{
}
public Student(string name)
{
Name = name;
}
public Student(string name, int sorce)
{
Name = name;
Sorce = sorce;
}
public override string ToString()
{
return $"{Name} {Sorce}";
}
}
internal class Program
{
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
int orderWay = int.Parse(Console.ReadLine());
List<Student> list = new List<Student>();
for (int i = 0; i < n; i++)
{
string[] strings = Console.ReadLine().Split();
string name = strings[0];
int sorce = int.Parse(strings[1]);
list.Add(new Student(name, sorce));
}
List<Student> orderList;
if(orderWay==0)
{
orderList = list.OrderByDescending(x => x.Sorce).ToList();
}
else
{
orderList = list.OrderBy(x => x.Sorce).ToList();
}
foreach (Student student in orderList)
{
Console.WriteLine(student);
}
}
}
}