#include <stdio.h>
#define TRUE 1
#define FALSE 0

typedef struct{
  char suit;
  char value;
} Card;

void bubble(Card *,int);
void seletion(Card *,int);
int isStable(Card *,Card *,int);

int main(){
  Card C1[100],C2[100];
  int i,n;
  char c;

  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%c%c",&C1[i].suit,&C1[i].value);
    C2[i] = C1[i];
  }
  bubble(C1,n);
  selection(C2,n);
  
  for(i=0;i<n;i++) printf("%c%c ",C1[i].suit,C1[i].value);
  printf("Stable\n");
  for(i=0;i<n;i++) printf("%c%c ",C2[i].suit,C2[i].value);
  if(isStable(C1,C2,n) == FALSE) printf("Not ");
  else printf("Stable\n");

  return 0;
}

void bubble(Card *A,int n){
  int i,j;
  Card tmp;
  for(i=0;i<n;i++){
    for(j=n-1;j>i;j--){
      if(A[j].value < A[j-1].value){
	tmp = A[j];
	A[j] = A[j-1];
	A[j-1] = tmp;
      }
    }
  }
}

void selection(Card *A,int n){
  int i,j,minj;
  Card tmp;
  for(i=0;i<n;i++){
    minj = i;
    for(j=i;j<n;j++){
      if(A[j].value < A[minj].value) minj = j;
    }
    tmp = A[i];
    A[i] = A[minj];
    A[minj] = tmp;
  }
}

int isStable(Card *c1,Card *c2,int m){
  int i,j,a,b;
  for(i=0;i<m;i++){
    for(j=i+1;j<=m;j++){
      for(a=1;a<m;a++){
	for(b=a+1;b<=m;b++){
	  if(c1[i] == c2[j] && c1[i] == c2[b] && c1[j] == c2[a]) return FALSE;
	}
      }
    }
  }
  return TRUE;
}
								  