#include<stdio.h>
#define N 36

struct Card{
  char s;
  char v;
};


int main(){
  struct Card Card1[N], Card2[N],p;
  int n,i=0,j,mini,l=0;
  char ch;

  scanf("%d%c", &n, &ch);
/*  for ( i = 0; i < n; i++ ){
    scanf("%c%c%c", &Card1[i].s, &Card1[i].v, &ch);
  }*/
while(0){
  scanf("%c%c%c", &Card1[i].s, &Card1[i].v, &ch);
  ++i;
  if(i==n-1) break;
}

  for(i=0;i<n;i++){
    Card2[i] = Card1[i];
  }
  /*Bubble*/
  for(i=0;i<n;i++){
      for(j=n-1;j>i;j--){
    if(Card1[j].v < Card1[j-1].v){
          p = Card1[j];
          Card1[j] = Card1[j-1];
          Card1[j-1] = p;
    }
      }
  }
  /*Selection*/
    for(i=0;i<n;i++){
      mini = i;
      for(j=i;j<n;j++){
    if(Card2[j].v < Card2[mini].v)
     mini = j;
       }
       p = Card2[i];
       Card2[i] = Card2[mini];
       Card2[mini] = p;
  }
    for(i=0;i<n;i++){
      printf("%c%c",Card1[i].s,Card1[i].v);
      if(i != n-1)
    printf(" ");
      if(i == n-1)
    printf("\n");
    }
    printf("Stable\n");

    for(i=0;i<n;i++){
      printf("%c%c",Card2[i].s,Card2[i].v);
      if(i != n-1)
    printf(" ");
      if(i == n-1)
    printf("\n");
    }
    for(i=0;i<n;i++){
      if(Card1[i].s != Card2[i].s)
    l = 1;
    }
    if(l==1)
      printf("Not stable\n");
    else
      printf("Stable\n");
    return 0;
}