#include <stdio.h>
#include <stdlib.h>

#define MAX 36

struct Card {
  char suit;
  int value;
};

void bubbleSort(struct Card X[], int n) {
  int i, j;
  char tmp_char;
  int tmp_int;
  for (i = 0; i < n - 1; i++) {
    for (j = 0; j < n - i - 1; j++) {
      if (X[j].value > X[j + 1].value) {
        tmp_char = X[j].suit;
        X[j].suit = X[j + 1].suit;
        X[j + 1].suit = tmp_char;
        tmp_int = X[j].value;
        X[j].value = X[j + 1].value;
        X[j + 1].value = tmp_int;
      }
    }
  }
}

void selectionSort(struct Card X[], int n) {
  int i, j;
  int minj;
  struct Card tmp;
  for (i = 0; i < n; i++) {
    minj = i;
    for (j = i; j < n; j++) {
      if (X[minj].value > X[j].value) {
        minj = j;
      }
    }
    tmp = X[minj];
    X[minj] = X[i];
    X[i] = tmp;
  }
}

void printCard(struct Card X[], int n) {
  int i;
  printf("%c%d", X[0].suit, X[0].value);
  for (i = 1; i < n; i++) {
    printf(" %c%d", X[i].suit, X[i].value);
  }
  printf("\n");
}

int main(void) {
  struct Card A[MAX];
  struct Card B[MAX];
  char tmp[3];
  int i, j, n;
  int flag = 0;

  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    scanf("%s", tmp);
    A[i].suit = tmp[0];
    B[i].suit = tmp[0];
    A[i].value = atoi(&tmp[1]);
    B[i].value = atoi(&tmp[1]);
  }

  bubbleSort(A, n);
  selectionSort(B, n);
  printCard(A, n);
  printf("Stable\n");
  printCard(B, n);
  for (i = 0; i < n; i++) {
    if (A[i].suit == B[i].suit && A[i].value == B[i].value) {
      flag = 1;
    }
  }
  if (flag == 1) {
    printf("Not stable\n");
  } else {
    printf("Stable\n");
  }

  return 0;
}