def bubbleSort( cards ):
    n = len( cards )
    for i in range( 0, n ):
        for j in range( n-1 , i, -1 ):
            if int( cards[j][1] ) < int( cards[ j-1 ][1] ):
                cards[j], cards[ j-1 ] = cards[ j-1 ], cards[j]
 
    print( " ".join( map( str, cards ) ) )
 
def selectionSort( cards ):
	n = len( cards )
	for i in range( 0, n ):
		mini = i
		for j in range( i , n ):
			if int( cards[j][1] ) < int( cards[ mini ][1] ):
				mini = j
		if mini != i:
			cards[i], cards[ mini ] = cards[ mini ], cards[i]

	print( " ".join( map( str, cards ) ) )


n = int( raw_input( ) )
cards = raw_input( ).split( " " )
cards2 = list( cards )
bubbleSort( cards )
print( "Stable" )
selectionSort( cards2 )
if cards2 == cards:
	print( "Stable" )
else:
	print( "Not stable" ) def bubbleSort( cards ):
    n = len( cards )
    for i in range( 0, n ):
        for j in range( n-1 , i, -1 ):
            if int( cards[j][1] ) < int( cards[ j-1 ][1] ):
                cards[j], cards[ j-1 ] = cards[ j-1 ], cards[j]
 
    print( " ".join( map( str, cards ) ) )
  bubbleSort n = len( cards ) n len( cards ) len cards for i in range( 0, n ):
        for j in range( n-1 , i, -1 ):
            if int( cards[j][1] ) < int( cards[ j-1 ][1] ):
                cards[j], cards[ j-1 ] = cards[ j-1 ], cards[j]
 
     i range( 0, n ) range 0 n for j in range( n-1 , i, -1 ):
            if int( cards[j][1] ) < int( cards[ j-1 ][1] ):
                cards[j], cards[ j-1 ] = cards[ j-1 ], cards[j]
 
     j range( n-1 , i, -1 ) range n-1 n 1 i -1 if int( cards[j][1] ) < int( cards[ j-1 ][1] ):
                cards[j], cards[ j-1 ] = cards[ j-1 ], cards[j]
 
     int( cards[j][1] ) < int( cards[ j-1 ][1] ) int( cards[j][1] ) int cards[j][1] [j] cards j 1 int( cards[ j-1 ][1] ) int cards[ j-1 ][1] [ j-1 ] cards j-1 j 1 1 cards[j], cards[ j-1 ] = cards[ j-1 ], cards[j] cards[j] cards j cards[ j-1 ] cards j-1 j 1 cards[ j-1 ] cards j-1 j 1 cards[j] cards j print( " ".join( map( str, cards ) ) ) print " ".join( map( str, cards ) ) " ".join " " join map( str, cards ) map str cards cards cards def selectionSort( cards ):
	n = len( cards )
	for i in range( 0, n ):
		mini = i
		for j in range( i , n ):
			if int( cards[j][1] ) < int( cards[ mini ][1] ):
				mini = j
		if mini != i:
			cards[i], cards[ mini ] = cards[ mini ], cards[i]

	print( " ".join( map( str, cards ) ) ) selectionSort n = len( cards ) n len( cards ) len cards for i in range( 0, n ):
		mini = i
		for j in range( i , n ):
			if int( cards[j][1] ) < int( cards[ mini ][1] ):
				mini = j
		if mini != i:
			cards[i], cards[ mini ] = cards[ mini ], cards[i]

	 i range( 0, n ) range 0 n mini = i mini i for j in range( i , n ):
			if int( cards[j][1] ) < int( cards[ mini ][1] ):
				mini = j
		 j range( i , n ) range i n if int( cards[j][1] ) < int( cards[ mini ][1] ):
				mini = j
		 int( cards[j][1] ) < int( cards[ mini ][1] ) int( cards[j][1] ) int cards[j][1] [j] cards j 1 int( cards[ mini ][1] ) int cards[ mini ][1] [ mini ] cards mini 1 mini = j mini j if mini != i:
			cards[i], cards[ mini ] = cards[ mini ], cards[i]

	 mini != i mini i cards[i], cards[ mini ] = cards[ mini ], cards[i] cards[i] cards i cards[ mini ] cards mini cards[ mini ] cards mini cards[i] cards i print( " ".join( map( str, cards ) ) ) print " ".join( map( str, cards ) ) " ".join " " join map( str, cards ) map str cards cards cards n = int( raw_input( ) ) n int( raw_input( ) ) int raw_input( ) raw_input cards = raw_input( ).split( " " ) cards raw_input( ).split( " " ) ( ).split ( ) raw_input split " " cards2 = list( cards ) cards2 list( cards ) list cards bubbleSort( cards ) bubbleSort cards print( "Stable" ) print "Stable" selectionSort( cards2 ) selectionSort cards2 if cards2 == cards:
	print( "Stable" )
else:
	print( "Not stable" ) cards2 == cards cards2 cards print( "Stable" ) print "Stable" print( "Not stable" ) print "Not stable" n = int( raw_input( ) ) int( raw_input( ) ) n cards = raw_input( ).split( " " ) raw_input( ).split( " " ) cards def bubbleSort( cards ):
    n = len( cards )
    for i in range( 0, n ):
        for j in range( n-1 , i, -1 ):
            if int( cards[j][1] ) < int( cards[ j-1 ][1] ):
                cards[j], cards[ j-1 ] = cards[ j-1 ], cards[j]
 
    print( " ".join( map( str, cards ) ) )
  def bubbleSort( cards ):
    n = len( cards )
    for i in range( 0, n ):
        for j in range( n-1 , i, -1 ):
            if int( cards[j][1] ) < int( cards[ j-1 ][1] ):
                cards[j], cards[ j-1 ] = cards[ j-1 ], cards[j]
 
    print( " ".join( map( str, cards ) ) )
  bubbleSort def selectionSort( cards ):
	n = len( cards )
	for i in range( 0, n ):
		mini = i
		for j in range( i , n ):
			if int( cards[j][1] ) < int( cards[ mini ][1] ):
				mini = j
		if mini != i:
			cards[i], cards[ mini ] = cards[ mini ], cards[i]

	print( " ".join( map( str, cards ) ) ) def selectionSort( cards ):
	n = len( cards )
	for i in range( 0, n ):
		mini = i
		for j in range( i , n ):
			if int( cards[j][1] ) < int( cards[ mini ][1] ):
				mini = j
		if mini != i:
			cards[i], cards[ mini ] = cards[ mini ], cards[i]

	print( " ".join( map( str, cards ) ) ) selectionSort cards2 = list( cards ) list( cards ) cards2
def bubbleSort( cards ):
    n = len( cards )
    for i in range( 0, n ):
        for j in range( n-1 , i, -1 ):
            if int( cards[j][1] ) < int( cards[ j-1 ][1] ):
                cards[j], cards[ j-1 ] = cards[ j-1 ], cards[j]
 
    print( " ".join( map( str, cards ) ) )
 
def selectionSort( cards ):
	n = len( cards )
	for i in range( 0, n ):
		mini = i
		for j in range( i , n ):
			if int( cards[j][1] ) < int( cards[ mini ][1] ):
				mini = j
		if mini != i:
			cards[i], cards[ mini ] = cards[ mini ], cards[i]

	print( " ".join( map( str, cards ) ) )


n = int( raw_input( ) )
cards = raw_input( ).split( " " )
cards2 = list( cards )
bubbleSort( cards )
print( "Stable" )
selectionSort( cards2 )
if cards2 == cards:
	print( "Stable" )
else:
	print( "Not stable" )