#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int MAX_N = 1e4 + 5;
struct Person {
string name;
int height;
} ps[MAX_N];
bool cmp(Person p1, Person p2) {
return p1.height == p2.height ? p1.name<p2.name : p1.height>p2.height;
}
void form(vector<int>& v, int s, int d) {
if ((d - s) % 2 == 0) {
v.push_back(s);
for (int i = s + 1; i < d - 1; i += 2) {
v.insert(v.begin(), i);
v.push_back(i + 1);
}
v.insert(v.begin(), d - 1);
}
else {
v.push_back(s);
for (int i = s + 1; i < d; i += 2) {
v.insert(v.begin(), i);
v.push_back(i + 1);
}
}
}
int main() {
int N, K;
cin >> N >> K;
for (int n = 0; n < N; n++) {
cin >> ps[n].name >> ps[n].height;
}
sort(ps, ps + N, cmp);
int M = floor(N / K);
// cout << M << endl;
vector<vector<int>> ans;
vector<int> temp;
form(temp, 0, N - M * (K - 1));
ans.push_back(temp);
for (int i = 0; N - M * (K - 1) + (i + 1) * M <= N; i++) {
vector<int>().swap(temp);
form(temp, N - M * (K - 1) + i * M, N - M * (K - 1) + (i + 1) * M);
ans.push_back(temp);
}
for (auto v : ans) {
for (int i = 0; i < v.size();i++) {
cout << ps[v[i]].name;
if (i < v.size() - 1) {
cout << " ";
}
}
cout << endl;
}
return 0;
}
/*
Sample Input:
10 3
Tom 188
Mike 170
Eva 168
Tim 160
Joe 190
Ann 168
Bob 175
Nick 186
Amy 160
John 159
Sample Output:
Bob Tom Joe Nick
Ann Mike Eva
Tim Amy John
*/