const readline = require('readline')
const rl = readline.createInterface({
input:process.stdin,
output:process.stdout
})
const lines = []
rl.on('line',(line)=>{
lines.push(line)
})
rl.on('close',()=>{
let c = parseInt(lines[0])
let flag = parseInt(lines[1])
lines.splice(0,2)
let arr = []
for(item of lines) {
let obj ={}
let [j,k] = item.split(' ')
obj[j] = parseInt(k)
arr.push(obj)
}
arr.sort((a,b)=>{
if(flag == 0) {
return Object.values(b)[0]-Object.values(a)[0]
}else {
return Object.values(a)[0]-Object.values(b)[0]
}
})
for(i of arr) {
console.log(Object.keys(i)[0] + ' ' + Object.values(i)[0])
}
})