#include<stdio.h>
#include<string.h>
#define N 100
??????
typedef struct{
????char mark;
????int value;
}trump;
??
void BubbleSort(trump *a,int n){
????int i,j;
????trump t;
????for(i=0;i<n;i++){
????????for(j=n-1;j>i;j--){
????????????if(a[j].value<a[j-1].value){
???????? t=a[j];
????????a[j]=a[j-1];
????????a[j-1]=t;
????????????}
????????}
????}
}

void SelectionSort(trump *a,int n){
????int i,j,min;
????trump t;
????for(i=0;i<n;i++){
????????min = i;
????????for(j=i;j<n;j++){
????????????if(a[j].value<a[min].value){
????????min = j;
????????????}
????????}
????????t=a[i];
????????a[i]=a[min];
????????a[min]=t;
????}
}
??
??
int main(){
????int n, i;
????trump a1[N], a2[N];
????int flag =0;
????scanf("%d", &n);

????for(i=0;i<n;i++){
????????scanf(" %c%d",&a1[i].mark,&a1[i].value);
????}
??
????for(i=0;i<n;i++) a2[i]=a1[i];
??
????BubbleSort(a1,n);
??????
????for(i=0;i<n-1;i++) printf("%c%d ",a1[i].mark,a1[i].value);

????printf("%c%d\n",a1[n-1].mark,a1[n-1].value);
????printf("Stable\n");
??
????SelectionSort(a2,n);

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

????printf("%c%d\n",a2[n-1].mark,a2[n-1].value);
??????
for(i=0;i<n;i++) if(c1[i].mark!=c2[i].mark) flag+=1;
????if(flag==0) printf("Stable\n");
????else printf("Not stable\n");
??
????return 0;
}