#include<stdio.h>

struct Card{
   char suit;
   char value;
};

void bubbleSort(struct Card *A, int N){
   int i, j;
   struct Card temp;
   for (i=0; i<N-1; i++){
      for (i=N-1; j>=i+1; j--){
         if (A[j].value<A[j-1].value){
            temp = A[j];
            A[j] = A[j-1];
            A[j-1] = temp;
         }
      }
   }

   for (i=0; i<N; i++){
      if (i>0)
         printf(" ");
      printf("%c%c", A[i].suit, A[i].value);
   }
   printf("\n");
}

void selectionSort(struct Card *A, int N){
   int i, j, minj;
   struct Card temp;
   for (i=0; i<N; i++){
      minj = i;
      for (j=i; j<N; j++){
         if (A[j]<A[minj])
            minj = j;
      }
      temp = A[minj];
      A[minj] = A[i];
      A[i] = temp;
   }
   for (i=0; i<N; i++){
      if (i>0)
         printf(" ");
      printf("%c%c", A[i].suit, A[i].value);
   }
   printf("\n");
}

int main()
{
   struct Card C1[36], C2[36], C3[36], C4[36];
   int N, i, flag = 0;

   scanf("%d", &N);
   for (i=0; i<N; i++){
      scanf("%c%c", &C1[i].suit, &C1[i].value)
      C2[i].suit = C1[i].suit;
      C2[i].value = C1[i].value;
   }

   bubbleSort(C1, N);
   puts("Stable");

   selectionSort(C2, N);
   for (i=0; i<N; i++){
      if (C1[i].suit != C2[i].suit)
         puts("Not stable");
         flag = 1;
         break;
   }
   if (flag==0)
      puts("Stable");

   return 0;
}