#include <stdio.h>
#include <string.h>
struct stone
{
int w;
int p;
};
struct stone a[10005];
int main()
{
int n, m;
while(scanf("%d%d", &n, &m)!=EOF)
{
int i, j;
struct stone temp;
for(i = 0; i < n; i++)
scanf("%d%d", &a[i].w, &a[i].p);
for(i = 0; i < m-1; i++)
for(j = i+1; j < m; j++)
if(a[i].w > a[j].w)
{
temp = a[j];
a[j] = a[i];
a[i] = temp;
}
for(i = 0; i < m-1; i++)
for(j = i+1; j < m; j++)
if(a[i].w == a[j].w&&a[i].p < a[j].p)
{
temp = a[j];
a[j] = a[i];
a[i] = temp;
}
for(i=0;i<n;i++)
printf("%d ", a[i].p);
printf("\n");
for(i=0;i<n;i++)
printf("%d ",a[i].w);
/*int sum = 0;
for(i = 0; i < m; i++)
sum += a[i].p;
printf("%d\n", sum);*/
}
return 0;
}