#include <algorithm> //sort
#include<cstring> //定义数组,以及 strcmp
#include<iostream>
using namespace std;
struct people{
char name[10];
int high;
}person[10000];
bool compare(const struct people &a,const struct people &b){
if(a.high!=b.high) return a.high>b.high;
return strcmp(a.name,b.name)<0; //如果name定义为string,则可直接a.name<b.name
}
void print(int m,int n){
int l,r,i;
int * I = new int[n];
I[n/2]=m;l=n/2-1,r=n/2+1; m=m+1;
while(l>=0 || r<n){
if(l>=0) I[l--]=m++;
if(r<n) I[r++]=m++;
}
for(i=0;i<n;i++){
cout<<person[I[i]].name;
if(i < n-1) cout<<" "; //每行的最后一位没有空格,直接回车
}
cout<<endl;
}
int main(){
int N,K=0,i;
cin>>N>>K;
for(i=0;i<N;i++){
cin>>person[i].name>>person[i].high;
}
sort(person,person+N,compare);
print(0,N/K+N%K);
for(i=N/K+N%K;i<N;i+=N/K) print(i,N/K);
return 0;
}