class card:
    def __init__(self, types, value):
        self.types = types
        self.value = value

def bubbleSort(A, N):
    for i in range(0, N):
        for j in range(N - 1, i, -1):
            if A[j].value < A[j - 1].value:
                exchange(A, j - 1, j)
    printCards(A)
    print "Stable"

def selectionSort(A, N):
    origin = A[:]
    for i in range(0, N):
        minj = i
        for j in range(i, N):
            if A[j].value < A[minj].value:
                minj = j
        if i != minj:
            exchange(A, i, minj)
    printCards(A)
    if checkStable(origin, A, N):
        print "Stable"
    else:
        print "Not stable"

def exchange(array, i, j):
    tmp = array[i]
    array[i] = array[j]
    array[j] = tmp

def printCards(cards):
    print " ".join(t.types + str(t.value) for t in cards)

def checkStable(origin, sorted, N):
    for i in range(0, N - 1):
        if sorted[i].value == sorted[i + 1].value:
            if origin.index(sorted[i]) > origin.index(sorted[i + 1]):
                return False
    return True

if __name__ == "__main__":
    N = input()
    A = [card(c[0], int(c[1])) for c in raw_input().split()]
    bubbleSort(A[:], N)
    selectionSort(A[:], N) class card:
    def __init__(self, types, value):
        self.types = types
        self.value = value card def __init__(self, types, value):
        self.types = types
        self.value = value __init__ self.types = types self.types self types types self.value = value self.value self value value self self types types value value def bubbleSort(A, N):
    for i in range(0, N):
        for j in range(N - 1, i, -1):
            if A[j].value < A[j - 1].value:
                exchange(A, j - 1, j)
    printCards(A)
    print "Stable" bubbleSort for i in range(0, N):
        for j in range(N - 1, i, -1):
            if A[j].value < A[j - 1].value:
                exchange(A, j - 1, j)
     i range(0, N) range 0 N for j in range(N - 1, i, -1):
            if A[j].value < A[j - 1].value:
                exchange(A, j - 1, j)
     j range(N - 1, i, -1) range N - 1 N 1 i -1 if A[j].value < A[j - 1].value:
                exchange(A, j - 1, j)
     A[j].value < A[j - 1].value A[j].value [j] A j value A[j - 1].value [j - 1] A j - 1 j 1 value exchange(A, j - 1, j) exchange A j - 1 j 1 j printCards(A) printCards A print A A N N def selectionSort(A, N):
    origin = A[:]
    for i in range(0, N):
        minj = i
        for j in range(i, N):
            if A[j].value < A[minj].value:
                minj = j
        if i != minj:
            exchange(A, i, minj)
    printCards(A)
    if checkStable(origin, A, N):
        print "Stable"
    else:
        print "Not stable" selectionSort origin = A[:] origin A[:] A : for i in range(0, N):
        minj = i
        for j in range(i, N):
            if A[j].value < A[minj].value:
                minj = j
        if i != minj:
            exchange(A, i, minj)
     i range(0, N) range 0 N minj = i minj i for j in range(i, N):
            if A[j].value < A[minj].value:
                minj = j
         j range(i, N) range i N if A[j].value < A[minj].value:
                minj = j
         A[j].value < A[minj].value A[j].value [j] A j value A[minj].value [minj] A minj value minj = j minj j if i != minj:
            exchange(A, i, minj)
     i != minj i minj exchange(A, i, minj) exchange A i minj printCards(A) printCards A if checkStable(origin, A, N):
        print "Stable"
    else:
        print "Not stable" checkStable(origin, A, N) checkStable origin A N print print A A N N def exchange(array, i, j):
    tmp = array[i]
    array[i] = array[j]
    array[j] = tmp exchange tmp = array[i] tmp array[i] array i array[i] = array[j] array[i] array i array[j] array j array[j] = tmp array[j] array j tmp array array i i j j def printCards(cards):
    print " ".join(t.types + str(t.value) for t in cards) printCards print " ".join(t.types + str(t.value) for t in cards) " ".join " " join t.types + str(t.value) t.types t types str(t.value) str t.value t value cards cards def checkStable(origin, sorted, N):
    for i in range(0, N - 1):
        if sorted[i].value == sorted[i + 1].value:
            if origin.index(sorted[i]) > origin.index(sorted[i + 1]):
                return False
    return True checkStable for i in range(0, N - 1):
        if sorted[i].value == sorted[i + 1].value:
            if origin.index(sorted[i]) > origin.index(sorted[i + 1]):
                return False
     i range(0, N - 1) range 0 N - 1 N 1 if sorted[i].value == sorted[i + 1].value:
            if origin.index(sorted[i]) > origin.index(sorted[i + 1]):
                return False
     sorted[i].value == sorted[i + 1].value sorted[i].value [i] sorted i value sorted[i + 1].value [i + 1] sorted i + 1 i 1 value if origin.index(sorted[i]) > origin.index(sorted[i + 1]):
                return False
     origin.index(sorted[i]) > origin.index(sorted[i + 1]) origin.index(sorted[i]) origin.index origin index sorted[i] sorted i origin.index(sorted[i + 1]) origin.index origin index sorted[i + 1] sorted i + 1 i 1 return False False return True True origin origin sorted sorted N N if __name__ == "__main__":
    N = input()
    A = [card(c[0], int(c[1])) for c in raw_input().split()]
    bubbleSort(A[:], N)
    selectionSort(A[:], N) __name__ == "__main__" __name__ "__main__" N = input() N input() input card(c[0], int(c[1])) for c in raw_input().split() for c in raw_input().split() c raw_input().split() ().split () raw_input split for c in raw_input().split() card(c[0], int(c[1])) card c[0] c 0 int(c[1]) int c[1] c 1 A = [card(c[0], int(c[1])) for c in raw_input().split()] A [card(c[0], int(c[1])) for c in raw_input().split()] bubbleSort(A[:], N) bubbleSort A[:] A : N selectionSort(A[:], N) selectionSort A[:] A : N def selectionSort(A, N):
    origin = A[:]
    for i in range(0, N):
        minj = i
        for j in range(i, N):
            if A[j].value < A[minj].value:
                minj = j
        if i != minj:
            exchange(A, i, minj)
    printCards(A)
    if checkStable(origin, A, N):
        print "Stable"
    else:
        print "Not stable" def selectionSort(A, N):
    origin = A[:]
    for i in range(0, N):
        minj = i
        for j in range(i, N):
            if A[j].value < A[minj].value:
                minj = j
        if i != minj:
            exchange(A, i, minj)
    printCards(A)
    if checkStable(origin, A, N):
        print "Stable"
    else:
        print "Not stable" selectionSort def printCards(cards):
    print " ".join(t.types + str(t.value) for t in cards) def printCards(cards):
    print " ".join(t.types + str(t.value) for t in cards) printCards A = [card(c[0], int(c[1])) for c in raw_input().split()] [card(c[0], int(c[1])) for c in raw_input().split()] A N = input() input() N class card:
    def __init__(self, types, value):
        self.types = types
        self.value = value class card:
    def __init__(self, types, value):
        self.types = types
        self.value = value card def bubbleSort(A, N):
    for i in range(0, N):
        for j in range(N - 1, i, -1):
            if A[j].value < A[j - 1].value:
                exchange(A, j - 1, j)
    printCards(A)
    print "Stable" def bubbleSort(A, N):
    for i in range(0, N):
        for j in range(N - 1, i, -1):
            if A[j].value < A[j - 1].value:
                exchange(A, j - 1, j)
    printCards(A)
    print "Stable" bubbleSort def exchange(array, i, j):
    tmp = array[i]
    array[i] = array[j]
    array[j] = tmp def exchange(array, i, j):
    tmp = array[i]
    array[i] = array[j]
    array[j] = tmp exchange def checkStable(origin, sorted, N):
    for i in range(0, N - 1):
        if sorted[i].value == sorted[i + 1].value:
            if origin.index(sorted[i]) > origin.index(sorted[i + 1]):
                return False
    return True def checkStable(origin, sorted, N):
    for i in range(0, N - 1):
        if sorted[i].value == sorted[i + 1].value:
            if origin.index(sorted[i]) > origin.index(sorted[i + 1]):
                return False
    return True checkStable
class card:
    def __init__(self, types, value):
        self.types = types
        self.value = value

def bubbleSort(A, N):
    for i in range(0, N):
        for j in range(N - 1, i, -1):
            if A[j].value < A[j - 1].value:
                exchange(A, j - 1, j)
    printCards(A)
    print "Stable"

def selectionSort(A, N):
    origin = A[:]
    for i in range(0, N):
        minj = i
        for j in range(i, N):
            if A[j].value < A[minj].value:
                minj = j
        if i != minj:
            exchange(A, i, minj)
    printCards(A)
    if checkStable(origin, A, N):
        print "Stable"
    else:
        print "Not stable"

def exchange(array, i, j):
    tmp = array[i]
    array[i] = array[j]
    array[j] = tmp

def printCards(cards):
    print " ".join(t.types + str(t.value) for t in cards)

def checkStable(origin, sorted, N):
    for i in range(0, N - 1):
        if sorted[i].value == sorted[i + 1].value:
            if origin.index(sorted[i]) > origin.index(sorted[i + 1]):
                return False
    return True

if __name__ == "__main__":
    N = input()
    A = [card(c[0], int(c[1])) for c in raw_input().split()]
    bubbleSort(A[:], N)
    selectionSort(A[:], N)