#include<stdio.h>
#define N 36

struct Card{
  char suit;
  int value;
};

Card C1[N],C2[N];

void BubbleSort(int);
void SelectSort(int);


int main(){
  int n, i,a;
  char ch;

  scanf("%d%c", &n, &ch);
  for ( i = 1; i <= n; i++ ){
    scanf("%c%d%c", &C1[i].suit, &C1[i].value, &ch);
  }

  BubbleSort(n);
  SelectSort(n);  

  return 0;
}

void BubbleSort(int n){
  int i,j;

  for(i=0; i<n; i++){
    for(j=n-1; j>i; j--){
      if(C1[j].value < C1[j-1].value){
	C2[j].value = C1[j-1].value;
	C2[j].suit = C1[j-1].suit;
	C2[j-1].value = C1[j].value;
	C2[j-1].suit = C[j].suit;
      }
    }
  }
  for(i=0; i<n; i++){
    printf("%c%d ",C2[i].suit,C2[i].value);
  }  

}

void SelectSort(int n){
  int i,j,mini;

  for(i=0; i<n; i++){
    mini = i;
    for(j=i; j<n; j++){
      if(C1[j].value < C1[mini].value){
        C2[j].value = C1[j-1].value;
        C2[j].suit = C1[j-1].suit;
        C2[j-1].value = C1[j].value;
        C2[j-1].suit = C[j].suit;
      }
    }
  }
  for(i=0; i<n; i++){
    printf("%c%d ",C2[i].suit,C2[i].value);
  }

}