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

//???????????¨???
 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]    = box;
       mark[j]    = box;
     }
   }
 }

//??¨???
 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 i, j, kosuu, num[101],  min, minnum, box1;
 char mark[40], 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]){
      box  = num[i];
      num[i] = min;
      num[minnum]  = box;
   }
 }

//??¨???
 for(i = 1;i < kosuu;i++){
   printf("%c%d ", a[i]);
 }
 printf("%d\n%d\n", a[kosuu], CGcount);

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[40], cardnum[40]);
 select(kosuu, cardmark[40], cardnum[40]);
 return 0;
} 