#include<stdio.h>
#include<stdlib.h>

#define CARD_NUM 36

typedef struct {

  int value;
  char mark;

} card ;

void print_card(int N,card *card){

  int i;

  for(i = 0;i < N-1;i++){
    printf("%c",card[i].mark);
    printf("%d ",card[i].value);
  }
  printf("%c",card[N-1].mark);
  printf("%d\n",card[N-1].value);

}

void print_flag(int flag){

  if(flag == 0){
    printf("Stable\n");
  }
  else {
    printf("Not stable\n");
  }
}

void Bubble(int N,card *C){

  int i;
  int j;
  int flag = 0;
  card temp;

  for(i = 0;i < N;i++){
    for(j = N-1;j > i;j--){
      if(C[j].value < C[j-1].value){
	if(C[j].value == C[j-1].value){
	  flag = 1;
	}
	temp = C[j];
	C[j] = C[j-1];
	C[j-1] = temp;
      }
    }
  }

  print_card(N,C);
  print_flag(flag);
}

void Selection(int N,card *C){

  int i;
  int j;
  int min;
  int flag = 0;
  card temp;

  for(i = 0;i < N;i++){
    min = i;
    for(j = i;j < N;j++){
      if(C[j].value < C[min].value){
	min = j;
      }
    }
    if(C[i].value == C[min].value){
      flag = 1;
    }
    temp = C[i];
    C[i] = C[min];
    C[min] = temp;
  }
  print_card(N,C);
  print_flag(flag);
}

int main(){

  int i;
  int num;
  char temp[4];
  card card[CARD_NUM];

  scanf("%d",&num);

  for(i = 0;i < num;i++){
    scanf("%s",temp);
    card[i].mark = *temp;
    card[i].value = atoi(&temp[1]);
  }

  Bubble(num,card);
  Selection(num,card);

  return 0;
}