#include<stdio.h>

typedef struct{
  int num;
  char mark;
}trump;


int isStable(trump B[],trump C[], int m){
  int a,b,i,j;
  for(i = 0; i < m; i++){
    for(j = i+1; j < m; j++){
      for(a = 0; a < m; a++){
	for(b = a+1;b < m; b++){
	  if(B[i].num == B[j].num && B[i].mark == C[b].mark && B[i].mark == C[a].mark)
	    return 0;
	}
      }
    }
  }
  return 1;
}

int main(){
  trump A[36],D[36],key;
  int i,j,n,minj;

  scanf("%d%*c",&n);

  for(i = 0; i < n; i++)
    scanf("%c%d%*c",&A[i].mark,&A[i].num);


  for(i = 0; i < n; i++)
    D[i] = A[i];
    
  for(i = 0; i < n; i++){
    minj = i;

    for(j = i; j < n; j++){
      if(A[j].num < A[minj].num)
	minj = j;
    }

    if(i != minj){
      key = A[minj];
      A[minj] = A[i];
      A[i] = key;
    }
  }

  for(i = 0; i < n; i++){
    minj = i;

    for(j = i; j < n; j++){
      if(D[j].num < D[minj].num)
	minj = j;
    }

    if(i != minj){
      key = D[minj];
      D[minj] = D[i];
      D[i] = key;
    }
  }

  for(i = 0; i < n-1; i++)
      printf("%c%d ",A[i].mark,A[i].num);

      printf("%c%d\n",A[n-1].mark,A[n-1].num);

      if(isStable(A,D,n))
	printf("Stable\n");

      else printf("Not Stable\n");
      
  return 0;
}