#include
#include
#include
using namespace std;
const int maxn = 1e4 + 10;
struct stu{
string name;
int h;
friend bool operator < (stu &s1, stu &s2){
if(s1.h != s2.h) return s1.h > s2.h;
return s1.name < s2.name;
}
}stus[maxn];
int main(){
int n, k, now, cnt = 1, mid, c, flag;
string ans[maxn];
cin >> n >> k;
for(int i = 1; i <= n; i++){
cin >> stus[i].name >> stus[i].h;
}
sort(stus + 1, stus + n + 1);
for(int i = 1; i <= k; i++){//i排数
if(i == 1) now = n / k + n % k;//now每行的人数
else now = n / k;
mid = now / 2 + 1;//mid中间位置
flag = c = 1;//flag本排是否需要继续,c距离中间的距离
ans[mid] = stus[cnt++].name;
while(flag){
if(mid - c > 0) ans[mid - c] = stus[cnt++].name;
if(mid + c < now + 1) ans[mid + c] = stus[cnt++].name;
else if(mid - c < 1) flag = 0;
c++;
}
for(int j = 1; j <= now; j++){
if(j != 1) cout << " ";
cout << ans[j];
}
cout << endl;
}
return 0;
}