#include <stdio.h>

struct Card{
    char suit;
    char value;
};

int main(){
    struct Card C1[101], C2[101];
    int n, i, j, min, flag = 0;
    char ch, tmp;
    
    scanf("%d%c", &n, &ch);
    for (i = 0; i < n; i++) scanf("%c%c%c", &C1[i].suit, &C1[i].value, &ch);
    
    for (i = 0;i < n;i++) C2[i]  =C1[i];
    
    //bubble sort
    for (i = 0; i < n; i++) {
        for (j = n; j >= i + 1; j--) {
            if (C1[j].value < C1[j - 1].value) {
                tmp = C1[j].value;
                ch = C1[j].suit;
                C1[j] = C1[j - 1];
                C1[j - 1].value = tmp;
                C1[j - 1].suit = ch;
            }
        }
    }
    
    //Selection sort
    for (i = 0; i < n; i++) {
        min = i;
        for (j = i; j < n; j++)
            if (C2[j].value < C2[min].value) min = j;
        tmp = C2[i].value;
        ch = C2[i].suit;
        C2[i] = C2[min];
        C2[min].value = tmp;
        C2[min].suit  = ch;
    }
    
    for (i = 0; i < n - 1;i++) printf("%c%c ", C1[i].suit, C1[i].value);
    printf("%c%c\nStable\n", C1[i].suit, C1[i].value);
    
    for (i = 0;i < n - 1;i++) printf("%c%c ", C2[i].suit, C2[i].value);
    printf("%c%c\n", C2[i].suit, C2[i].value);
    
    for (i = 0;i < n;i++)
        if (C2[i].suit != C1[i].suit)
            flag++;
    if (flag == 0) printf("Stable\n");
    else printf("Not Stable\n");
    return 0;
}