#include<stdio.h>

struct card
{
  char suit;
  char value;
};

void BubbleSort(struct card A[],int n)
{
  int i,j;
  struct card t;
  for( i = 0;i< n;i++)
    {
      for (j = n-1;j>=i+1;j--)
	{
	  if (A[j].value < A[j-1].value)
	    {
	      t=A[j];
	      A[j]=A[j-1];
	      A[j-1]=t;
	    }
	}
    }
}

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

void print(struct card A[],int n)
{
  int i;
  for(i=0;i<n;i++)
    {
      if(i) printf(" ");
      printf("%c%c",A[i].suit,A[i].value);
    }
  printf("\n");
}

int isStable(struct card c1[],struct card c2[],int n)
{
  int i;
  for(i=0;i<n;i++)
    {
      if(c1[i].suit != c2[i].suit)
	{
	  return 0;
	}
    }
  return 1;
}

int main()
{
  struct card c1[101],c2[101];
  int i,n;
  char ch;

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

  for(i=0;i<n;i++)
    {
      c2[i]=c1[i];
    }

  bubble(c1,n);
  selection(c2,n);

  print(c1,n);
  printf("stable\n");
  print(c2,n);
  if(isStable(c1,c2,n))
    {
      printf("Stable\n");
    }
  else
    {
      printf("Not stable\n");
    }
  return 0;
}
    

