#include<stdio.h>
  
int bubble(int kosuu, int mark[], int num[])
{
 int i, j, box1 ;
 int box2; 
 

 for(i = 1;i <= kosuu;i++) {
   for(j = kosuu;j > i;j--) {
     if (num[j] < num[j-1]) {
       box1      = num[j-1];
       box2      = mark[j-1];      
       num[j-1] = num[j];
       mark[j-1] = mark[j];
       num[j]    = box1;
       mark[j]    = box2;
     }
   }
 }
 

 for(i = 1;i < kosuu;i++){
   printf("%c%d ", mark[i], num[i]);
 }
 printf("%c%d\n", mark[kosuu], num[kosuu]);
 return 0;
}
 
int select(int kosuu, int mark[], int num[])
{
 int i, j,  min, minnum, box1;
 int box2;
  

 for(i = 1;i <= kosuu;i++){
   min = num[i];
   for(j = i + 1;j <= kosuu;j++){
     if(num[j] < min){
       min = num[j];
       minnum = j;
     }
   }
   if(min < num[i]){
      box1  = num[i];
      num[i] = min;
      num[minnum]  = box1;
   }
 }

 for(i = 1;i < kosuu;i++){
   printf("%c%d ", num[i]);
 }
 printf("%d\n\n", num[kosuu]);
 return 0;
}
int main()
{
 int i, kosuu, cardnum[40];
 char cardmark[40];
  
 scanf("%d", &kosuu);
  
 for(i = 1;i <=kosuu; i++){ 
   scanf("%c%d ",&cardmark[i], &cardnum[i]);
 }
  
 bubble(kosuu, cardmark, cardnum);
 select(kosuu, cardmark, cardnum);
 return 0;
} 