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

void swap(char* a,char* b){
  char tmp=*a;
  *a=*b;
  *b=tmp;
}

void PrintStability(char Array1[],char Array2[], int Num){
  int i;
  for(i=0;i<Num;i++){
    if(Array1[i]!=Array2[i]){
      printf("Not stable\n");
      return;
    }
  }
  printf("Stable\n");
}


void BubbleSort(char list[],int Num){
  if(Num<=4) return;
  int i,j;
  for(i=1;i<2*Num;i+=2){
    for(j=2*Num-1;j>i;j-=2){

      if(list[j]<list[j-2]){
        swap(&list[j],&list[j-2]);
        swap(&list[j-1],&list[j-3]);
      }

    }
  }
}

void SelectionSort(char *list,int Num){
  int i,j,key;
  for(i=0;i<2*Num;i+=2){
    key=i+1;
    for(j=2*Num-1;j>i;j-=2){
      if(list[j]<list[key])key=j;
    }
    swap(&list[key],&list[i+1]);
    swap(&list[key-1],&list[i]);
  }
}

int main(void){
  int N,i;
  scanf("%d",&N);

  char* List=(char*)malloc(sizeof(char)*2*N);
  if(List==NULL)exit(0);
  char* DefaultList=(char*)malloc(sizeof(char)*2*N);
  if(List==NULL)exit(0);

  for(i=0;i<2*N;i++){
    scanf("%c",&List[i]);
    DefaultList[i]=List[i];
  }

  BubbleSort(List,N);
  PrintStability(DefaultList,List,N);

  SelectionSort(List,N);
  PrintStability(DefaultList,List,N);

  free(DefaultList);
  free(List);
  return 0;
}