#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
typedef struct
{
char name[9]; int height;
}struction;
int cmp_height(const void *a, const void *b)
{
struction *aa = (struction*)a, *bb = (struction*)b;
return (aa->height > bb->height) ? -1 : 1;
}
int cmp_name(const void *a, const void *b)
{
struction *aa = (struction*)a, *bb = (struction*)b;
return strcmp(aa->name, bb->name);
}
int main()
{
int N, K, n, count = 0;
struction **b, *a;
scanf("%d %d", &N, &K);
n = N / K;
a = (struction*)malloc(sizeof(struction)*N);
for (int i = 0; i < N; i++)scanf("%s %d", a[i].name, &a[i].height);
qsort(a, N, sizeof(struction), cmp_height);
b = (struction**)malloc(sizeof(struction*)*K);
b[0] = (struction*)malloc(sizeof(struction)*(N - K * n + n));;
for (int i = 1; i < K; i++)b[i] = (struction*)malloc(sizeof(struction)*n);
for (int i = 0; i < N - 1; i++)
if (a[i].height == a[i + 1].height)
{
int cnt = 0, j;
for (j = i; a[j].height == a[i].height; j++)cnt++;
qsort(a + i, cnt, sizeof(struction), cmp_name);
i = j - 1;
}
for (int i = (N - K * n + n) / 2, cnt = 0, flag = 1; i >= 0 && i < N - K * n + n; i += cnt * flag)
b[0][i] = a[count++], cnt++, flag *= -1;
for (int i = 1; i < K; i++)
for (int j = n / 2, cnt = 0, flag = 1; j >= 0 && j < n; j += cnt * flag)
b[i][j] = a[count++], cnt++, flag *= -1;
for (int i = 0; i < N - K * n + n; i++)
{
printf("%s", b[0][i].name);
if (i != (N - K * n + n - 1))printf(" ");
else printf("\n");
}
for(int i=1;i<K;i++)
for (int j = 0; j < n; j++)
{
printf("%s", b[i][j].name);
if (j != n - 1)printf(" ");
else printf("\n");
}
return 0;
}