#include<stdio.h>

struct Card{
  char suit;
  char value;
};


int main(){
  struct Card C1[101], C2[101],tmp[1];
  int n, i, j,mini,flag=0;
  char ch;

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

  for ( i = 1; i <= n; i++ ){
    printf("%c%c", C1[i].suit, C1[i].value);
    if(i == n){
      break;
    }
    printf(" ");
  }

  printf("\n");
  printf("Stable\n");

  for(i = 1; i <= n; i++){
    mini = i;
    for(j = i; j <= n; j++){
      if(C2[j].value < C2[mini].value){
	mini = j;
      }
    }
    tmp[0] = C2[i];
    C2[i] = C2[mini];
    C2[mini] = tmp[0];
  }
  
  for ( i = 1; i <= n; i++ ){
    printf("%c%c", C2[i].suit, C2[i].value);
    if(i == n){
      break;
    }
    printf(" ");
  }
  printf("\n");

  for ( i = 1; i <= n; i++ ){
    if(C1[i].suit != C2[i].suit || C1[i].value != C2[i].value){
      flag = 1;
    }
  }
  if(flag == 1){
    printf("Not stable\n");
  }else{
    printf("Stable\n");
  }
  return 0;
}
  