#include <stdio.h>

#define N 100

typedef struct{
 char spe;
 char  value;
}trump;

void buble(trump *A,int n){
  int flag,i;
  trump B;
  
  while(flag){
    flag=0;
    for(i=n-1;i>=1;i--){
      if(A[i-1].value>A[i].value){
	B=A[i];
      A[i]=A[i-1];
      A[i-1]=B;
      flag=1;
      }
    }
  }
}

  void select(trump *A,int n){
    int i,j,minj;
    trump B;

    for(i=0;n-1>i;i++){
      minj=i;
      for(j=i;n>j;j++){
	if(A[j].value<A[minj].value){
	  minj=j;
	}
      }
      B=A[i];
      A[i]=A[minj];
      A[minj]=B;
    }
  }

  int comp(trump *A1,trump *A2,int n){
    int i;
    for(i=0;i<n;i++){
	if(A1[i].spe!=A2[i].spe) return 0;
    }
    return 1;
  }
	  

int main(){

  trump A[N],B[N];
  int n,i,j;

  scanf("%d",&n);

  for(i=0;i<n;i++){
    scanf(" %c%c",&A[i].spe,&A[i].value);
    B[i]=A[i];
  }

  buble(A,n);
  select(B,n);

  for(i=0;i<n;i++){
    printf("%c%c",A[i].spe,A[i].value);
    if(i==n-1) printf("\n");
    else printf(" ");
  }

  /*if(comp(A,B,n)*/ printf("Stable\n");
  //else printf("Not stable\n");

  for(i=0;i<n;i++){
    printf("%c%c",B[i].spe,B[i].value);
    if(i==n-1) printf("\n");
    else printf(" ");
  }

  if(comp(A,B,n)) printf("Stable\n");
  else printf("Not stable\n");
  
  return 0;
}