def buble_sort(array, n)
    i = 0
    while i < n 
        j = n-1
        while i < j
            if array[j-1][1] > array[j][1]
                array[j-1],array[j] = array[j],array[j-1]
            end
            j -= 1
        end
        i += 1
    end
    return array
end

def selection_sort(array, n)
    i = 0
    while i < n-1 
        minj = i
        j = i+1
        while j < n
            if array[minj][1] > array[j][1]
                minj = j
            end
            j +=1
        end

        if array[minj] != array[i]
            array[minj],array[i] = array[i],array[minj]
        end
        i += 1
    end
    array
end

def isstable(array1,array2,n)
    i = 0
    while i < n
        return "Not stable" if array1[i] != array2[i]
        i += 1
    end
    "Stable"
end

n = gets.to_i
array = gets.split(" ")
bub = buble_sort(array.dup,n)
sel = selection_sort(array.dup,n)
puts bub.join(" ")
puts "Stable"
puts sel.join(" ")
puts isstable(bub,sel,n)
