#include<iostream>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

void selsort(string  a[], int n){
  for(int i = 0; i < n; i++){
    int minj = i;
    for(int j = i; j < n; j++)
      if(a[j][1] < a[minj][1]) minj = j;
    swap(a[i], a[minj]);
  }
}

void bsort(string  A[],int n){
  bool flag = 1;
  while(flag){
    flag = 0;
    for(int j = n-1; j >= 1; j--){
      if(A[j][1] < A[j-1][1]){
	swap(A[j],A[j-1]);
	flag = 1;
      }
    }
  }
}

int main(){
  string card[36],s[36],b[36];
  
  int n,c=0;
  cin>>n;
  for(int i = 0; i < n; i++){
    cin>>card[i];
    s[i]=card[i];
    b[i]=card[i];
  }
  
  bsort(b,n);
  selsort(s,n);
  
  for(int i = 0; i < n-1; i++)cout<<b[i]<<" ";
  cout<<b[n-1]<<endl<<"Stable"<<endl;
  for(int i = 0; i < n-1; i++)cout<<s[i]<<" ";
  cout<<s[n-1]<<endl;
  for(int i = 0; i < n; i++)if(b[i]==s[i])c++;
  if(c==n)cout << "Stable" << endl;
  else cout << "Not stable" << endl;
  return 0;
}

