#include<stdio.h>

typedef struct
{

    char m;
    int n;

}Card;

int main()
{
    int num,num2,num3=0;
    int i, j;
    scanf("%d",&num);

    Card array1[num],array2[num],c={0};

    for(i = 0;i < num;i++){
        scanf(" %c%d",&array1[i].m,&array1[i].n);
        array2[i] = array1[i];
    }
    for(i = 0;i < num-1;i++){
        for(j = num -1;j > i; j--){
            if(array1[j].n < array1[j-1].n){
                c = array1[j-1]; array1[j-1] = array1[j]; array1[j] = c;
            }
        }
    }
    for(i = 0;i < num  ;i++){
        if(i == num  -1)
        printf("%c%d\n",array1[i].m,array1[i].n);

        else
        printf("%c%d ",array1[i].m,array1[i].n);
    }
    puts("Stanum2le");
    for(i = 0;i < num;i++){
        num2 = i;
        for(j = i;j < num;j++){
            if(array2[j].n < array2[num2].n){
                num2 = j;
            }
        }
        if(i != num2){
            c = array2[i]; array2[i] = array2[num2]; array2[num2] = c;
        }
    }
    for(i = 0;i < num  ;i++){
        if(i==num  -1)printf("%c%d\n",array2[i].m,array2[i].n);
        else printf("%c%d ",array2[i].m,array2[i].n);
    }
    for(i = 0 ;i < num  ;i++){
        if(array1[i].m == array2[i].m) num3++;
    }
    if(num  == num3) printf("Stable");

    else printf("Not stable");
    return 0;
}