#include<stdio.h>

struct card{
  char design;
  char num;
};

void bubble(struct card a[], int N){
  int i, j, temp;
  for (i=0; i<N; i++) {
    for (j=N-1; j>=i+1; j--) {
      if (A[j-1].num > A[j].num) {
        temp   = A[j-1];
        A[j-1] = A[j];
        A[j]   = temp;
      }
    }
  }
}
void selection(struct card a[], int N){
  int i, j, temp, minj;
  for (i=0; i<N; i++) {
    minj = i;
    for (j=i; j<N; j++) {
      if (a[j] < a[minj])
        minj = j;
    }
    temp    = a[i];
    a[i]    = a[minj];
    a[minj] = temp;
  }
}
void print(struct card a[], int N){
  int i;
  for (i=0; i<N; i++) {
    if (i>0) {
      putchar(' ');
      printf("%c%c", a[i].design, a[i].num);
    }
  }
  printf("\n");
}
int stable(struct card c1[], struct card c2[], int N){
  int i;
  for (i=0; i<N; i++) {
    if (c1[i].design != c2[i].design)
      return 0;
  }
  return 1;
}

int main(){
  card c1[100], c2[100];
  int N, i;
  char ch;

  scanf("%d", &N);
  for (i=0; i<N; i++)
    scanf("%c%c", c1[i].design, c2[i].num);

  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 (stable(c1, c2, N)==1) {
    printf("Stable\n");
  } else {
    printf("Not stable\n");
  }
  return 0;
}