def bubbleSort( cards )
    n = cards.length
    0.upto( n-1 ) { |i|
        ( n-1 ).downto( i+1 ) { |j|
            if cards[j][1].to_i < cards[ j-1 ][1].to_i
                cards[j], cards[ j-1 ] = cards[ j-1 ], cards[j]
            end
        }
    }
   puts( cards.join( " " ) )
end

def selectionSort( cards )
    n = cards.length
    0.upto( n-1 ) { |i|
        mini = i
        ( i+1 ).upto( n-1 ) { |j|
            if cards[j][1].to_i < cards[ mini ][1].to_i
                mini = j
            end
        }
        if mini != i
            cards[i], cards[ mini ] = cards[ mini ], cards[i]
        end
    }
    puts( cards.join( " " ) )
end


n = gets.to_i
cards = gets.split( " " )
cards2 = cards.clone
bubbleSort( cards )
puts( "Stable" )
selectionSort( cards2 )
if cards2 == cards
	puts( "Stable" )
else
	puts( "Not stable" )
end