# coding: utf-8
# Stable Sort 2018/3/12

class Card(object):
    def __init__(self, card):
        self.value = int(card[1])
        self.card = card

    def __repr__(self):
        return self.card


def BubbleSort(C, N):
    for i in range(N):
        for j in reversed(range(i+1, N)):
            if C[j].value < C[j-1].value:
                C[j], C[j-1] = C[j-1], C[j]
    return C


def SelectionSort(C, N):
    for i in range(N):
        minj = i
        for j in range(i, N):
            if C[j].value < C[minj].value:
                minj = j
        C[i], C[minj] = C[minj], C[i]
    return C


def isStable(inl, out):
    n = len(inl)
    for i in range(n):
        for j in range(i+1, n):
            for a in range(n):
                for b in range(a+1, n):
                    if inl[i].value == inl[j].value and inl[i] == out[b] and inl[j] == out[a]:
                        return False
    return True


def print_stable(inl, out):
    if isStable(inl, out):
        print "Stable"
    else:
        print "Not stable"


if __name__ == "__main__":
    N = int(raw_input())
    inputs = raw_input().split()
    C = []
    for i in inputs:
        C.append(Card(i))
    Cb = BubbleSort(list(C), N)
    print " ".join([i.card for i in Cb])
    print_stable(C, Cb)
    Cs = SelectionSort(list(C), N)
    print " ".join([i.card for i in Cs])
    print_stable(C, Cs)
 class Card(object):
    def __init__(self, card):
        self.value = int(card[1])
        self.card = card

    def __repr__(self):
        return self.card Card def __init__(self, card):
        self.value = int(card[1])
        self.card = card

     __init__ self.value = int(card[1]) self.value self value int(card[1]) int card[1] card 1 self.card = card self.card self card card self self card card def __repr__(self):
        return self.card __repr__ return self.card self.card self card self self object def BubbleSort(C, N):
    for i in range(N):
        for j in reversed(range(i+1, N)):
            if C[j].value < C[j-1].value:
                C[j], C[j-1] = C[j-1], C[j]
    return C BubbleSort for i in range(N):
        for j in reversed(range(i+1, N)):
            if C[j].value < C[j-1].value:
                C[j], C[j-1] = C[j-1], C[j]
     i range(N) range N for j in reversed(range(i+1, N)):
            if C[j].value < C[j-1].value:
                C[j], C[j-1] = C[j-1], C[j]
     j reversed(range(i+1, N)) reversed range(i+1, N) range i+1 i 1 N if C[j].value < C[j-1].value:
                C[j], C[j-1] = C[j-1], C[j]
     C[j].value < C[j-1].value C[j].value [j] C j value C[j-1].value [j-1] C j-1 j 1 value C[j], C[j-1] = C[j-1], C[j] C[j] C j C[j-1] C j-1 j 1 C[j-1] C j-1 j 1 C[j] C j return C C C C N N def SelectionSort(C, N):
    for i in range(N):
        minj = i
        for j in range(i, N):
            if C[j].value < C[minj].value:
                minj = j
        C[i], C[minj] = C[minj], C[i]
    return C SelectionSort for i in range(N):
        minj = i
        for j in range(i, N):
            if C[j].value < C[minj].value:
                minj = j
        C[i], C[minj] = C[minj], C[i]
     i range(N) range N minj = i minj i for j in range(i, N):
            if C[j].value < C[minj].value:
                minj = j
         j range(i, N) range i N if C[j].value < C[minj].value:
                minj = j
         C[j].value < C[minj].value C[j].value [j] C j value C[minj].value [minj] C minj value minj = j minj j C[i], C[minj] = C[minj], C[i] C[i] C i C[minj] C minj C[minj] C minj C[i] C i return C C C C N N def isStable(inl, out):
    n = len(inl)
    for i in range(n):
        for j in range(i+1, n):
            for a in range(n):
                for b in range(a+1, n):
                    if inl[i].value == inl[j].value and inl[i] == out[b] and inl[j] == out[a]:
                        return False
    return True isStable n = len(inl) n len(inl) len inl for i in range(n):
        for j in range(i+1, n):
            for a in range(n):
                for b in range(a+1, n):
                    if inl[i].value == inl[j].value and inl[i] == out[b] and inl[j] == out[a]:
                        return False
     i range(n) range n for j in range(i+1, n):
            for a in range(n):
                for b in range(a+1, n):
                    if inl[i].value == inl[j].value and inl[i] == out[b] and inl[j] == out[a]:
                        return False
     j range(i+1, n) range i+1 i 1 n for a in range(n):
                for b in range(a+1, n):
                    if inl[i].value == inl[j].value and inl[i] == out[b] and inl[j] == out[a]:
                        return False
     a range(n) range n for b in range(a+1, n):
                    if inl[i].value == inl[j].value and inl[i] == out[b] and inl[j] == out[a]:
                        return False
     b range(a+1, n) range a+1 a 1 n if inl[i].value == inl[j].value and inl[i] == out[b] and inl[j] == out[a]:
                        return False
     inl[i].value == inl[j].value and inl[i] == out[b] and inl[j] == out[a] inl[i].value == inl[j].value and inl[i] == out[b] inl[i].value == inl[j].value inl[i].value [i] inl i value inl[j].value [j] inl j value inl[i] == out[b] inl[i] inl i out[b] out b inl[j] == out[a] inl[j] inl j out[a] out a return False False return True True inl inl out out def print_stable(inl, out):
    if isStable(inl, out):
        print "Stable"
    else:
        print "Not stable" print_stable if isStable(inl, out):
        print "Stable"
    else:
        print "Not stable" isStable(inl, out) isStable inl out print print inl inl out out if __name__ == "__main__":
    N = int(raw_input())
    inputs = raw_input().split()
    C = []
    for i in inputs:
        C.append(Card(i))
    Cb = BubbleSort(list(C), N)
    print " ".join([i.card for i in Cb])
    print_stable(C, Cb)
    Cs = SelectionSort(list(C), N)
    print " ".join([i.card for i in Cs])
    print_stable(C, Cs) __name__ == "__main__" __name__ "__main__" N = int(raw_input()) N int(raw_input()) int raw_input() raw_input inputs = raw_input().split() inputs raw_input().split() ().split () raw_input split C = [] C [] for i in inputs:
        C.append(Card(i))
     i inputs C.append(Card(i)) C.append C append Card(i) Card i Cb = BubbleSort(list(C), N) Cb BubbleSort(list(C), N) BubbleSort list(C) list C N print i.card for i in Cb for i in Cb i Cb for i in Cb i.card i card " ".join([i.card for i in Cb]) " ".join " " join [i.card for i in Cb] print_stable(C, Cb) print_stable C Cb Cs = SelectionSort(list(C), N) Cs SelectionSort(list(C), N) SelectionSort list(C) list C N print i.card for i in Cs for i in Cs i Cs for i in Cs i.card i card " ".join([i.card for i in Cs]) " ".join " " join [i.card for i in Cs] print_stable(C, Cs) print_stable C Cs def BubbleSort(C, N):
    for i in range(N):
        for j in reversed(range(i+1, N)):
            if C[j].value < C[j-1].value:
                C[j], C[j-1] = C[j-1], C[j]
    return C def BubbleSort(C, N):
    for i in range(N):
        for j in reversed(range(i+1, N)):
            if C[j].value < C[j-1].value:
                C[j], C[j-1] = C[j-1], C[j]
    return C BubbleSort N = int(raw_input()) int(raw_input()) N def print_stable(inl, out):
    if isStable(inl, out):
        print "Stable"
    else:
        print "Not stable" def print_stable(inl, out):
    if isStable(inl, out):
        print "Stable"
    else:
        print "Not stable" print_stable class Card(object):
    def __init__(self, card):
        self.value = int(card[1])
        self.card = card

    def __repr__(self):
        return self.card class Card(object):
    def __init__(self, card):
        self.value = int(card[1])
        self.card = card

    def __repr__(self):
        return self.card Card def SelectionSort(C, N):
    for i in range(N):
        minj = i
        for j in range(i, N):
            if C[j].value < C[minj].value:
                minj = j
        C[i], C[minj] = C[minj], C[i]
    return C def SelectionSort(C, N):
    for i in range(N):
        minj = i
        for j in range(i, N):
            if C[j].value < C[minj].value:
                minj = j
        C[i], C[minj] = C[minj], C[i]
    return C SelectionSort def isStable(inl, out):
    n = len(inl)
    for i in range(n):
        for j in range(i+1, n):
            for a in range(n):
                for b in range(a+1, n):
                    if inl[i].value == inl[j].value and inl[i] == out[b] and inl[j] == out[a]:
                        return False
    return True def isStable(inl, out):
    n = len(inl)
    for i in range(n):
        for j in range(i+1, n):
            for a in range(n):
                for b in range(a+1, n):
                    if inl[i].value == inl[j].value and inl[i] == out[b] and inl[j] == out[a]:
                        return False
    return True isStable inputs = raw_input().split() raw_input().split() inputs C = [] [] C Cb = BubbleSort(list(C), N) BubbleSort(list(C), N) Cb Cs = SelectionSort(list(C), N) SelectionSort(list(C), N) Cs
# coding: utf-8
# Stable Sort 2018/3/12

class Card(object):
    def __init__(self, card):
        self.value = int(card[1])
        self.card = card

    def __repr__(self):
        return self.card


def BubbleSort(C, N):
    for i in range(N):
        for j in reversed(range(i+1, N)):
            if C[j].value < C[j-1].value:
                C[j], C[j-1] = C[j-1], C[j]
    return C


def SelectionSort(C, N):
    for i in range(N):
        minj = i
        for j in range(i, N):
            if C[j].value < C[minj].value:
                minj = j
        C[i], C[minj] = C[minj], C[i]
    return C


def isStable(inl, out):
    n = len(inl)
    for i in range(n):
        for j in range(i+1, n):
            for a in range(n):
                for b in range(a+1, n):
                    if inl[i].value == inl[j].value and inl[i] == out[b] and inl[j] == out[a]:
                        return False
    return True


def print_stable(inl, out):
    if isStable(inl, out):
        print "Stable"
    else:
        print "Not stable"


if __name__ == "__main__":
    N = int(raw_input())
    inputs = raw_input().split()
    C = []
    for i in inputs:
        C.append(Card(i))
    Cb = BubbleSort(list(C), N)
    print " ".join([i.card for i in Cb])
    print_stable(C, Cb)
    Cs = SelectionSort(list(C), N)
    print " ".join([i.card for i in Cs])
    print_stable(C, Cs)

