#include<stdio.h>

struct Card{
  char suit;
  char value;
};

// my codes
#define N 36

int main(){
  struct Card C1[101], C2[101], C[101];
  int n, i, j, min, temp;
  char ch;

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

  for ( i = 1; i <= n; i++ ) C2[i] = C1[i];

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

  //selectionSort
  if(n != 1){
    for(i = 1; i < n; i++){
      min = i;
      for(j = (i + 1); j <= n; j++){
	if(C2[j].value < C2[min].value) min = j;
      }
      if(i != min){
	C[i] = C2[i];
	C2[i] = C2[min];
	C2[min] = C[i];
      }
    }
  }
  for ( i = 1; i <= n; i++ ){
    if(i > 1) printf(" ");
    printf("%c%c", C2[i].suit, C2[i].value);
  }
  printf("\n");

  return 0;
}