#include<bits/stdc++.h>
using namespace std;
struct Node{
int height;
string name;
};
bool cmp(Node a,Node b){
if(a.height!=b.height) return a.height>b.height;
else if(a.name!=b.name) return a.name<b.name;
}
int main()
{
freopen("in.txt","r",stdin);
int n,k;cin>>n>>k;
vector<Node> ppp;ppp.resize(n);
for(int i=0;i<n;i++){
cin>>ppp[i].name>>ppp[i].height;
}
sort(ppp.begin(),ppp.end(),cmp);
int t1,t2;
if(n%k==0){
t1=t2=n/k;
}else{
t2=n/k;
t1=n-(k-1)*t2;
}
int mid=t1/2+1;
string a[t1+1];
a[mid]=ppp[0].name;
int x=1;
for(int i=1;i<t1;i++){
if(i%2!=0){
a[mid+0-x]=ppp[i].name;
}else{
a[mid+x]=ppp[i].name;x++;
}
}
for(int i=1;i<=t1;i++){
if(i==1) cout<<a[i];
else cout<<' '<<a[i];
}
cout<<endl;
for(int i=0;i<k-1;i++){
vector<Node> ppp1;ppp1.resize(t2);
for(int j=0;j<t2;j++){
ppp1[j]=ppp[t1+j+t2*i];
}
int midd=t2/2+1;
string aa[t2+1];
aa[midd]=ppp1[0].name;
int xx=1;
for(int j=1;j<t2;j++){
if(j%2!=0){
aa[midd+0-xx]=ppp1[j].name;
}else{
aa[midd+xx]=ppp1[j].name;xx++;
}
}
for(int j=1;j<=t2;j++){
if(j==1) cout<<aa[j];
else cout<<' '<<aa[j];
}
cout<<endl;
}
return 0;
}