#include<bits/stdc++.h>
using namespace std;
#define mst(x,y) memset(x,y,sizeof(x))
#define rep(x,y,z) for(int x=(y);(x)<(z);x++)
#define fi first
#define se second
#define mk(x,y) make_pair(x,y)
#define pii pair<int,string>
const int maxn=1e4+10;
int n,k;
pair<int,string> p[maxn];
void output(int x){
if(x>=n/k) return;
output(x+1);
int ret[maxn];
if(x==n/k-1){
int l=x*k,r=n-1;
int len=r-l+1,pos=len/2+1,x=pos-1,y=pos;
for(int i=r;i>=l;i--){
if((r-i)&1) ret[x--]=i;
else ret[y++]=i;
}
rep(i,1,len+1){
cout<<p[ret[i]].se;
if(i==len) cout<<"\n";
else cout<<" ";
}
return ;
}else{
int l=x*k,r=l+k-1;
int y=k/2+1,x=y-1;
for(int i=r;i>=l;i--){
if((r-i)&1) ret[x--]=i;
else ret[y++]=i;
}
rep(i,1,k+1){
cout<<p[ret[i]].se;
if(i==k) cout<<"\n";
else cout<<" ";
}
}
}
bool cmp(pii x,pii y){
if(x.fi==y.fi) return x.se>y.se;
return x.fi<y.fi;
}
int main(){
cin>>n>>k;k=n/k;
rep(i,0,n){
string s;
int x;
cin>>s>>x;
p[i]=mk(x,s);
}
sort(p,p+n,cmp);
///rep(i,0,n)cout<<p[i].fi<<" "<<p[i].se<<endl;
output(0);
return 0;
}