#include<stdio.h>
#define N 36
struct Card{
  char S;
  char V;
};

struct Card C1[N], C2[N];
void swap(Card*,Card*);
void trace(struct Card *, int);
void Bubblesort(struct Card *, int);
void Selectionsort(struct Card *, int);
int main()
{
  int n, i,j;
  char ch;
  scanf("%d%c",&n,&ch);
  for (i=1;i<=n;i++)
    {
      scanf("%c%c%c",&C1[i].S,&C1[i].V,&ch);
      C2[i]=C1[i];
    }
  Bubblesort(C1,n);
  Selectionsort(C2,n);
  return 0;
}
void trace(struct Card C[], int n)
{
  int i;
  for(i=1;i<=n;i++)
    {
      if (i>1) printf(" ");
      printf("%c%c",C[i].S, C[i].V);
    }
  printf("\n");
}
void Bubble(struct Card C[], int n)
{
  struct Card C[101];
  int i,j,mini;
  for(i=0;i<n;i++)
    {
      for(j=n;j>=i+1;j--)
        {
          if(CB[j].V<CB[j-1].V)
            {
              CB2[j]=CB[j];
              CB[j]=CB[j-1];
              CB[j-1] = CB2[j];
            }
        }
    }
  trace(CB,n);
  printf("Stable\n");
}
void Selection(struct Card CS[], int n)
{
  struct Card CS2[101];
  int i,j,mini,count,flag=0;
  for(i=0;i<=n;i++)
    {
      mini=i;
      count=0;
      for(j=i;j<=n;j++)
        {
          if(CS[j].V<CS[mini].V)
            {
              mini=j;
              count=1;
            }
        }
      if(count==1)
        {
          CS2[i]=CS[i];
          CS[i]=CS[mini];
          CS[mini]=CS2[i];
        }
      if(CS[i].S!=C1[i].S)      flag=1;
    }
  trace(CS,n);
  if(flag==1)
    printf("Not stable\n");
  else printf("Stable\n");
}