#if __cplusplus > 199711L      /* C++11 */
#include <unordered_map>
#include <unordered_set>
#include <forward_list>
#include <array>
#endif                         /* C++11 */
#include <cstdio>
#include <cmath>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <climits>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <set>
#include <map>
#include <list>
#include <deque>
#include <vector>
#include <string>
#include <bitset>
#define y0 qvya13579
#define y1 qvyb24680
#define j0 qvja13579
#define j1 qvjb24680
#define next qvne13579xt
#define prev qvpr13579ev
#define INF 1000000007
#define PI acos(-1.0)
#define endl "\n"
#define IOS cin.tie(0);ios::sync_with_stdio(false)
#define M_P make_pair
#define P_B push_back
#define U_B upper_bound
#define L_B lower_bound
#define P_Q priority_queue
#define FIR first
#define SEC second
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define REP_R(i,n) for(int i=((int)(n)-1);i>=0;--i)
#define FOR(i,m,n) for(int i=((int)(m));i<(int)(n);++i)
#define FOR_R(i,m,n) for(int i=((int)(m)-1);i>=(int)(n);--i)
#define ALL(v) (v).begin(),(v).end()
#define RALL(v) (v).rbegin(),(v).rend()
#define SIZ(x) ((int)(x).size())
#define COUT(x) cout<<(x)<<endl
#define CIN(x) cin>>(x)
#define CIN2(x,y) cin>>(x)>>(y)
#define CIN3(x,y,z) cin>>(x)>>(y)>>(z)
#define CIN4(x,y,z,w) cin>>(x)>>(y)>>(z)>>(w)
#define SCAND(x) scanf("%d",&(x))
#define SCAND2(x,y) scanf("%d%d",&(x),&(y))
#define SCAND3(x,y,z) scanf("%d%d%d",&(x),&(y),&(z))
#define SCAND4(x,y,z,w) scanf("%d%d%d%d",&(x),&(y),&(z),&(w))
#define SCANLLD(x) scanf("%lld",&(x))
#define SCANLLD2(x,y) scanf("%lld%lld",&(x),&(y))
#define SCANLLD3(x,y,z) scanf("%lld%lld%lld",&(x),&(y),&(z))
#define SCANLLD4(x,y,z,w) scanf("%lld%lld%lld%lld",&(x),&(y),&(z),&(w))
#define PRINTD(x) printf("%d\n",(x))
#define PRINTLLD(x) printf("%lld\n",(x))
typedef long long int lli;
using namespace std;

int ctoi(char c)
{
  if( c >= '0' and c <= '9' )
    {
      return (int)(c-'0');
    }

  return -1;
}

int alphabet_pos(char c) 
{
  if( c >= 'a' and c <= 'z' )
    {
      return (int)(c-'a');
    }

  return -1;
}


int alphabet_pos_capital(char c)
{
  if( c >= 'A' and c <= 'Z' )
    {
      return (int)(c-'A');
    }

  return -1;
}


vector<string> split(string str, char ch)
{
  int first = 0;
  int last = str.find_first_of(ch);
  
  if(last == string::npos)
    {
      last = SIZ(str);
    }

  vector<string> result;

  while( first < SIZ(str) )
    {
      string Ssubstr(str, first, last - first);
      result.push_back(Ssubstr);
      first = last + 1;
      last = str.find_first_of(ch, first);

      if(last == string::npos)
	{
	  last = SIZ(str);
	}
    }
  
  return result;
}

/*--------------- the end of the template --------------------*/

int val( string S )
{
  return ctoi(S[1]);
  
}

vector<string> sls( int N , vector<string> vs )
{
  vector<string> vo = vs;
  
  
  int mi;
  
  REP(i,N)
    {
      mi = i;
      
      FOR(j,i+1,N)
	{
	  if( val(vo[j]) < val(vo[mi]) )
	    {
	      mi = j;

	    }

	  
	}

      swap(vo[i],vo[mi]);
      
      
    }

  return vo;
  

    
  
}


vector<string> bbs( int N , vector<string> vs )
{
  
  int mi;
  vector<string> vo = vs;
  int fl_end = 1;
  
  
  REP(i,N-1)
    {
      fl_end = 1;
      
       
      REP(j,N-1)
	{
	  if( val(vo[j+1]) < val(vo[j]) )
	    {

	      swap(vo[j],vo[j+1]);
	      fl_end = 0;

	    }
	  
	}

      if( fl_end )
	{
	  break;
	  
	}
      
      
    }

  return vo;
  
  
  
}

void order(int N, vector<string> v1 , vector<string> v2)
{
  REP(i1,N)
    {
      FOR(i2,i1+1,N)
	{

	  REP(j1,N)
	    {
	      FOR(j2,j1+1,N)
		{

		  if(
		     val(v1[i1]) == val(v1[i2])
		     and v1[i1] == v2[j2]
		     and v1[i2] == v2[j1]
		     )
		    {
		      cout << "Not stable" << endl;
		      
		      return;
		      
		    }
		  
		}
	      
	    }
	  
	  
	}
      
    }

  cout << "Stable" << endl;
  return;
  
}



int main()
{
  IOS; /* making cin faster */
  int N;
  CIN(N);

  vector<string> vs(N);

  REP(i,N)
    {
      CIN(vs[i]);
      
    }

  vector<string> vo_sls = sls(N,vs);
  vector<string> vo_bbs = bbs(N,vs);
  
  REP(i,N)
    {
      if( i != N-1 )
	{
	  cout << vo_bbs[i] << " ";
	}
      else
	{
	  cout << vo_bbs[i] << endl;
	}
      
    }

  order(N,vo_bbs,vs);


  REP(i,N)
    {
      if( i != N-1 )
	{
	  cout << vo_sls[i] << " ";
	}
      else
	{
	  cout << vo_sls[i] << endl;
	}
      
    }

  order(N,vo_sls,vs);
  
  
  
  

}



Measured memory_bytes: 3220