#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int n,k;
struct node{
string name;
int height;
};
bool cmp(struct node a,struct node b){
return a.height!=b.height?a.height>b.height:a.name<b.name;
}
int main(){
cin>>n>>k;
int m;
vector<node>stu(n);
for(int i=0;i<n;i++){
cin>>stui.name;
cin>>stui.height;
}
sort(stu.begin(),stu.end(),cmp);
int t=0,row=k;//t是已处理的学生索引
//计算每行人数
while(row){
if(row==k){
m=n-n/k*(k-1);
}else{
m=n/k;
}
vector<string>ans(m);
ansm/2=stut.name;
//左边一列
int j=m/2-1;
for(int i=t+1;i<t+m;i=i+2){
ansj--=stui.name;
}
//右边一列
j=m/2+1;
for(int i=t+2;i<t+m;i=i+2){
ansj++=stui.name;
}
cout<<ans0;
for(int i=1;i<m;i++){
cout<<" "<<ansi;
}
cout<<endl;
t=t+m;//移动到下一批学生
row--;
}
return 0;
}