class Card():

    def __init__(self):
        self.suit = 0
        self.value = 0


def bubble_sort(c, n):
    for i in range(n):
        for j in range(n - 1, i, -1):
            if c[j].value < c[j - 1].value:
                c[j], c[j - 1] = c[j - 1], c[j]


def selection_sort(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]


def is_stable(c1, c2, n):
    for i in range(n):
        if c1[i].suit != c2[i].suit:
            return False
    return True


def print_result(c, n):
    r = []
    for i in range(n):
        r.append(c[i].suit + str(c[i].value))
    print(' '.join(r))


def main():
    n = int(input())
    c1 = [Card() for x in range(n)]
    c2 = [Card() for x in range(n)]
    ary = [x for x in input().split(' ')]

    for i in range(n):
        s, v = list(ary[i])
        c1[i].suit, c1[i].value = s, int(v)
        c2[i].suit, c2[i].value = s, int(v)

    bubble_sort(c1, n)
    selection_sort(c2, n)

    print_result(c1, n)
    print('Stable')
    print_result(c2, n)
    if is_stable(c1, c2, n):
        print('Stable')
    else:
        print('Not stable')


if __name__ == '__main__':
    main() class Card():

    def __init__(self):
        self.suit = 0
        self.value = 0 Card def __init__(self):
        self.suit = 0
        self.value = 0 __init__ self.suit = 0 self.suit self suit 0 self.value = 0 self.value self value 0 self self def bubble_sort(c, n):
    for i in range(n):
        for j in range(n - 1, i, -1):
            if c[j].value < c[j - 1].value:
                c[j], c[j - 1] = c[j - 1], c[j] bubble_sort for i in range(n):
        for j in range(n - 1, i, -1):
            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 range(n - 1, i, -1):
            if c[j].value < c[j - 1].value:
                c[j], c[j - 1] = c[j - 1], c[j] j range(n - 1, i, -1) range n - 1 n 1 i -1 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 c c n n def selection_sort(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] selection_sort 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 c c n n def is_stable(c1, c2, n):
    for i in range(n):
        if c1[i].suit != c2[i].suit:
            return False
    return True is_stable for i in range(n):
        if c1[i].suit != c2[i].suit:
            return False
     i range(n) range n if c1[i].suit != c2[i].suit:
            return False
     c1[i].suit != c2[i].suit c1[i].suit [i] c1 i suit c2[i].suit [i] c2 i suit return False False return True True c1 c1 c2 c2 n n def print_result(c, n):
    r = []
    for i in range(n):
        r.append(c[i].suit + str(c[i].value))
    print(' '.join(r)) print_result r = [] r [] for i in range(n):
        r.append(c[i].suit + str(c[i].value))
     i range(n) range n r.append(c[i].suit + str(c[i].value)) r.append r append c[i].suit + str(c[i].value) c[i].suit [i] c i suit str(c[i].value) str c[i].value [i] c i value print(' '.join(r)) print ' '.join(r) ' '.join ' ' join r c c n n def main():
    n = int(input())
    c1 = [Card() for x in range(n)]
    c2 = [Card() for x in range(n)]
    ary = [x for x in input().split(' ')]

    for i in range(n):
        s, v = list(ary[i])
        c1[i].suit, c1[i].value = s, int(v)
        c2[i].suit, c2[i].value = s, int(v)

    bubble_sort(c1, n)
    selection_sort(c2, n)

    print_result(c1, n)
    print('Stable')
    print_result(c2, n)
    if is_stable(c1, c2, n):
        print('Stable')
    else:
        print('Not stable') main n = int(input()) n int(input()) int input() input Card() for x in range(n) for x in range(n) x range(n) range n for x in range(n) Card() Card c1 = [Card() for x in range(n)] c1 [Card() for x in range(n)] Card() for x in range(n) for x in range(n) x range(n) range n for x in range(n) Card() Card c2 = [Card() for x in range(n)] c2 [Card() for x in range(n)] x for x in input().split(' ') for x in input().split(' ') x input().split(' ') ().split () input split ' ' for x in input().split(' ') x ary = [x for x in input().split(' ')] ary [x for x in input().split(' ')] for i in range(n):
        s, v = list(ary[i])
        c1[i].suit, c1[i].value = s, int(v)
        c2[i].suit, c2[i].value = s, int(v)

     i range(n) range n s, v = list(ary[i]) s list(ary[i]) list ary[i] ary i v c1[i].suit, c1[i].value = s, int(v) c1[i].suit [i] c1 i suit s c1[i].value [i] c1 i value int(v) int v c2[i].suit, c2[i].value = s, int(v) c2[i].suit [i] c2 i suit s c2[i].value [i] c2 i value int(v) int v bubble_sort(c1, n) bubble_sort c1 n selection_sort(c2, n) selection_sort c2 n print_result(c1, n) print_result c1 n print('Stable') print 'Stable' print_result(c2, n) print_result c2 n if is_stable(c1, c2, n):
        print('Stable')
    else:
        print('Not stable') is_stable(c1, c2, n) is_stable c1 c2 n print('Stable') print 'Stable' print('Not stable') print 'Not stable' if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def selection_sort(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] def selection_sort(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] selection_sort def bubble_sort(c, n):
    for i in range(n):
        for j in range(n - 1, i, -1):
            if c[j].value < c[j - 1].value:
                c[j], c[j - 1] = c[j - 1], c[j] def bubble_sort(c, n):
    for i in range(n):
        for j in range(n - 1, i, -1):
            if c[j].value < c[j - 1].value:
                c[j], c[j - 1] = c[j - 1], c[j] bubble_sort def main():
    n = int(input())
    c1 = [Card() for x in range(n)]
    c2 = [Card() for x in range(n)]
    ary = [x for x in input().split(' ')]

    for i in range(n):
        s, v = list(ary[i])
        c1[i].suit, c1[i].value = s, int(v)
        c2[i].suit, c2[i].value = s, int(v)

    bubble_sort(c1, n)
    selection_sort(c2, n)

    print_result(c1, n)
    print('Stable')
    print_result(c2, n)
    if is_stable(c1, c2, n):
        print('Stable')
    else:
        print('Not stable') def main():
    n = int(input())
    c1 = [Card() for x in range(n)]
    c2 = [Card() for x in range(n)]
    ary = [x for x in input().split(' ')]

    for i in range(n):
        s, v = list(ary[i])
        c1[i].suit, c1[i].value = s, int(v)
        c2[i].suit, c2[i].value = s, int(v)

    bubble_sort(c1, n)
    selection_sort(c2, n)

    print_result(c1, n)
    print('Stable')
    print_result(c2, n)
    if is_stable(c1, c2, n):
        print('Stable')
    else:
        print('Not stable') main def print_result(c, n):
    r = []
    for i in range(n):
        r.append(c[i].suit + str(c[i].value))
    print(' '.join(r)) def print_result(c, n):
    r = []
    for i in range(n):
        r.append(c[i].suit + str(c[i].value))
    print(' '.join(r)) print_result def is_stable(c1, c2, n):
    for i in range(n):
        if c1[i].suit != c2[i].suit:
            return False
    return True def is_stable(c1, c2, n):
    for i in range(n):
        if c1[i].suit != c2[i].suit:
            return False
    return True is_stable class Card():

    def __init__(self):
        self.suit = 0
        self.value = 0 class Card():

    def __init__(self):
        self.suit = 0
        self.value = 0 Card
class Card():

    def __init__(self):
        self.suit = 0
        self.value = 0


def bubble_sort(c, n):
    for i in range(n):
        for j in range(n - 1, i, -1):
            if c[j].value < c[j - 1].value:
                c[j], c[j - 1] = c[j - 1], c[j]


def selection_sort(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]


def is_stable(c1, c2, n):
    for i in range(n):
        if c1[i].suit != c2[i].suit:
            return False
    return True


def print_result(c, n):
    r = []
    for i in range(n):
        r.append(c[i].suit + str(c[i].value))
    print(' '.join(r))


def main():
    n = int(input())
    c1 = [Card() for x in range(n)]
    c2 = [Card() for x in range(n)]
    ary = [x for x in input().split(' ')]

    for i in range(n):
        s, v = list(ary[i])
        c1[i].suit, c1[i].value = s, int(v)
        c2[i].suit, c2[i].value = s, int(v)

    bubble_sort(c1, n)
    selection_sort(c2, n)

    print_result(c1, n)
    print('Stable')
    print_result(c2, n)
    if is_stable(c1, c2, n):
        print('Stable')
    else:
        print('Not stable')


if __name__ == '__main__':
    main()