data class Card(val suit: Char, val value: Char) {
    override fun toString(): String {
        return "$suit$value"
    }
}

fun bubbleSort(a: Array<Card>) {
    (0 until (a.size)).forEach { i ->
        ((a.size - 1) downTo (i + 1)).forEach{ j ->
            if (a[j].value < a[j - 1].value) {
                run { val t = a[j]; a[j] = a[j - 1]; a[j - 1] = t }
            }
        }
    }
}

fun selectionSort(a: Array<Card>) {
    (0 until a.size).forEach { i ->
        val minIdx = (i until a.size).minBy { a[it].value }
        minIdx?.let { val t = a[i]; a[i] = a[minIdx]; a[minIdx] = t }
    }
}

fun isStable(a: Array<Card>, b: Array<Card>) =
    (0 until a.size).all { a[it].suit == b[it].suit }

fun main(args: Array<String>) {
    val n = readLine()!!.toInt()
    val a = readLine()!!.split(" ").map {
        val char = it.toCharArray()
        Card(char[0], char[1])
    }.toTypedArray()
    if (n != a.size) return

    val b = a.copyOf()

    bubbleSort(a)
    selectionSort(b)

    println(a.joinToString(" "))
    println("Stable")
    println(b.joinToString(" "))
    println(if (isStable(a, b)) "Stable" else "Not stable")
}
