#include <stdio.h>
//1 行目　バブルソート
//2 安定かどうか
//3　選択ソート
//4 安定かどうか

struct typedef{
  char mark;
  int nmber;
  int mark_number;
} card;

void bubblesort(card *);
void selectionsort(card*);
int main(){
  
  card A[36];
  int i,j,n;
  scanf("%d",&n);
  for(i=-0;i<36;i++){
    scanf("%c%d",A[i].mark,&A[i].number);
    
    if(A[i].mark == 'D')A[i].mark_number == 4;
    if(A[i].mark == 'C')A[i].mark_number == 3;
    if(A[i].mark == 'H')A[i].mark_number == 2;
    if(A[i].mark == 'S')A[i].mark_number == 1;
    }

  bubblesort(A);
  selectionsort(A);
  return 0;
}

void bubblesort(card A[]){
  int minj;
  int i,A[36];
  for(i=0;i<35;i++){
    if(A[i].number>A[i+1].number){
      card tmp = A[i];
      A[i] =A[i+1];
      A[i+1]=tmp;
    }
    
    else if(A[i].number == A[i+1].number){
      //同じ番号でマークが異なる場合の処理

      
      if((A[i].mark_number > A[i+1].mark_number){
	  tmp = A[i]; 
	  A[i] =A[i+1];
	  A[i+1]=tmp;}

	else continue;
    }

    
  }i=0;
    while(i<35)printf("%c%d "A[i].mark,A[i].number);
    printf("%c%d "A[35].mark,A[35].number);
    printf("\n");
  printf("Stable\n");

}


    
 void selectionsort(card A[]){
  int i,j;
  int minj;
  
  for(i=0;i<n;i++){
	 minj = i ;
	
	for(j=i;j<n;j++){
  	if(A[j] < A[minj])minj=j;
	}
	
	card tmp = A[i];
	A[i] = A[minj];
	A[minj] ==tmp;
  }i=0;
    while(i<35)printf("%c%d "A[i].mark,A[i].number);
    printf("%c%d "A[35].mark,A[35].number);
    printf("\n");
    
    i=0;
    while(i<35){if(A[i].mark_number <= A[i+1].mark_number) printf("Stable\n");
      else printf("Not stable\n");
    }

