def bubble_sort(n, key=0):
    c = 0
    r = [x if isinstance(x, list) else [x] for x in n[:]]
    l = len(r) - 1
    for i in range(0, l):
        for j in range(l, i, -1):
            if r[j][key] < r[j-1][key]:
                r[j], r[j-1] = r[j-1], r[j]
                c += 1
    r = [x[0] if len(x) == 1 else x for x in r]
    return {'list': r, 'count': c}

def selection_sort(n, key=0):
    c = 0
    r = [x if isinstance(x, list) else [x] for x in n[:]]
    l = len(r)
    for i in range(0, l):
        mini = i
        for j in range(i, l):
            if (r[j][key] < r[mini][key]):
                mini = j
        if mini != i:
            r[i], r[mini] = r[mini], r[i]
            c += 1
    r = [x[0] if len(x) == 1 else x for x in r]
    return {'list': r, 'count': c}

def card_format(cards):
    return ' '.join([x[0] + str(x[1]) for x in cards])

if __name__ == '__main__':
    n = int(input())
    c = list(map(list, input().split(' ')))
    c = [[x[0], int(x[1])] for x in c]
    bsc = card_format(bubble_sort(c, 1)['list'])
    ssc = card_format(selection_sort(c, 1)['list'])
    print(bsc)
    print('Stable')
    print(ssc)
    print('Stable' if bsc == ssc else 'Not stable')
 def bubble_sort(n, key=0):
    c = 0
    r = [x if isinstance(x, list) else [x] for x in n[:]]
    l = len(r) - 1
    for i in range(0, l):
        for j in range(l, i, -1):
            if r[j][key] < r[j-1][key]:
                r[j], r[j-1] = r[j-1], r[j]
                c += 1
    r = [x[0] if len(x) == 1 else x for x in r]
    return {'list': r, 'count': c} bubble_sort c = 0 c 0 x if isinstance(x, list) else [x] for x in n[:] for x in n[:] x n[:] n : for x in n[:] x if isinstance(x, list) else [x] isinstance(x, list) isinstance x list x [x] x r = [x if isinstance(x, list) else [x] for x in n[:]] r [x if isinstance(x, list) else [x] for x in n[:]] l = len(r) - 1 l len(r) - 1 len(r) len r 1 for i in range(0, l):
        for j in range(l, i, -1):
            if r[j][key] < r[j-1][key]:
                r[j], r[j-1] = r[j-1], r[j]
                c += 1
     i range(0, l) range 0 l for j in range(l, i, -1):
            if r[j][key] < r[j-1][key]:
                r[j], r[j-1] = r[j-1], r[j]
                c += 1
     j range(l, i, -1) range l i -1 if r[j][key] < r[j-1][key]:
                r[j], r[j-1] = r[j-1], r[j]
                c += 1
     r[j][key] < r[j-1][key] r[j][key] [j] r j key r[j-1][key] [j-1] r j-1 j 1 key r[j], r[j-1] = r[j-1], r[j] r[j] r j r[j-1] r j-1 j 1 r[j-1] r j-1 j 1 r[j] r j c += 1 c 1 x[0] if len(x) == 1 else x for x in r for x in r x r for x in r x[0] if len(x) == 1 else x len(x) == 1 len(x) len x 1 x[0] x 0 x r = [x[0] if len(x) == 1 else x for x in r] r [x[0] if len(x) == 1 else x for x in r] return {'list': r, 'count': c} {'list': r, 'count': c} 'list' 'list' r 'count' 'count' c n n key=0 key 0 def selection_sort(n, key=0):
    c = 0
    r = [x if isinstance(x, list) else [x] for x in n[:]]
    l = len(r)
    for i in range(0, l):
        mini = i
        for j in range(i, l):
            if (r[j][key] < r[mini][key]):
                mini = j
        if mini != i:
            r[i], r[mini] = r[mini], r[i]
            c += 1
    r = [x[0] if len(x) == 1 else x for x in r]
    return {'list': r, 'count': c} selection_sort c = 0 c 0 x if isinstance(x, list) else [x] for x in n[:] for x in n[:] x n[:] n : for x in n[:] x if isinstance(x, list) else [x] isinstance(x, list) isinstance x list x [x] x r = [x if isinstance(x, list) else [x] for x in n[:]] r [x if isinstance(x, list) else [x] for x in n[:]] l = len(r) l len(r) len r for i in range(0, l):
        mini = i
        for j in range(i, l):
            if (r[j][key] < r[mini][key]):
                mini = j
        if mini != i:
            r[i], r[mini] = r[mini], r[i]
            c += 1
     i range(0, l) range 0 l mini = i mini i for j in range(i, l):
            if (r[j][key] < r[mini][key]):
                mini = j
         j range(i, l) range i l if (r[j][key] < r[mini][key]):
                mini = j
         r[j][key] < r[mini][key] r[j][key] [j] r j key r[mini][key] [mini] r mini key mini = j mini j if mini != i:
            r[i], r[mini] = r[mini], r[i]
            c += 1
     mini != i mini i r[i], r[mini] = r[mini], r[i] r[i] r i r[mini] r mini r[mini] r mini r[i] r i c += 1 c 1 x[0] if len(x) == 1 else x for x in r for x in r x r for x in r x[0] if len(x) == 1 else x len(x) == 1 len(x) len x 1 x[0] x 0 x r = [x[0] if len(x) == 1 else x for x in r] r [x[0] if len(x) == 1 else x for x in r] return {'list': r, 'count': c} {'list': r, 'count': c} 'list' 'list' r 'count' 'count' c n n key=0 key 0 def card_format(cards):
    return ' '.join([x[0] + str(x[1]) for x in cards]) card_format x[0] + str(x[1]) for x in cards for x in cards x cards for x in cards x[0] + str(x[1]) x[0] x 0 str(x[1]) str x[1] x 1 return ' '.join([x[0] + str(x[1]) for x in cards]) ' '.join([x[0] + str(x[1]) for x in cards]) ' '.join ' ' join [x[0] + str(x[1]) for x in cards] cards cards if __name__ == '__main__':
    n = int(input())
    c = list(map(list, input().split(' ')))
    c = [[x[0], int(x[1])] for x in c]
    bsc = card_format(bubble_sort(c, 1)['list'])
    ssc = card_format(selection_sort(c, 1)['list'])
    print(bsc)
    print('Stable')
    print(ssc)
    print('Stable' if bsc == ssc else 'Not stable') __name__ == '__main__' __name__ '__main__' n = int(input()) n int(input()) int input() input c = list(map(list, input().split(' '))) c list(map(list, input().split(' '))) list map(list, input().split(' ')) map list input().split(' ') ().split () input split ' ' [x[0], int(x[1])] for x in c for x in c x c for x in c [x[0], int(x[1])] x[0] x 0 int(x[1]) int x[1] x 1 c = [[x[0], int(x[1])] for x in c] c [[x[0], int(x[1])] for x in c] bsc = card_format(bubble_sort(c, 1)['list']) bsc card_format(bubble_sort(c, 1)['list']) card_format bubble_sort(c, 1)['list'] (c, 1) bubble_sort c 1 'list' ssc = card_format(selection_sort(c, 1)['list']) ssc card_format(selection_sort(c, 1)['list']) card_format selection_sort(c, 1)['list'] (c, 1) selection_sort c 1 'list' print(bsc) print bsc print('Stable') print 'Stable' print(ssc) print ssc print('Stable' if bsc == ssc else 'Not stable') print 'Stable' if bsc == ssc else 'Not stable' bsc == ssc bsc ssc 'Stable' 'Not stable' bsc = card_format(bubble_sort(c, 1)['list']) card_format(bubble_sort(c, 1)['list']) bsc n = int(input()) int(input()) n def bubble_sort(n, key=0):
    c = 0
    r = [x if isinstance(x, list) else [x] for x in n[:]]
    l = len(r) - 1
    for i in range(0, l):
        for j in range(l, i, -1):
            if r[j][key] < r[j-1][key]:
                r[j], r[j-1] = r[j-1], r[j]
                c += 1
    r = [x[0] if len(x) == 1 else x for x in r]
    return {'list': r, 'count': c} def bubble_sort(n, key=0):
    c = 0
    r = [x if isinstance(x, list) else [x] for x in n[:]]
    l = len(r) - 1
    for i in range(0, l):
        for j in range(l, i, -1):
            if r[j][key] < r[j-1][key]:
                r[j], r[j-1] = r[j-1], r[j]
                c += 1
    r = [x[0] if len(x) == 1 else x for x in r]
    return {'list': r, 'count': c} bubble_sort c = [[x[0], int(x[1])] for x in c] [[x[0], int(x[1])] for x in c] c c = list(map(list, input().split(' '))) list(map(list, input().split(' '))) c def selection_sort(n, key=0):
    c = 0
    r = [x if isinstance(x, list) else [x] for x in n[:]]
    l = len(r)
    for i in range(0, l):
        mini = i
        for j in range(i, l):
            if (r[j][key] < r[mini][key]):
                mini = j
        if mini != i:
            r[i], r[mini] = r[mini], r[i]
            c += 1
    r = [x[0] if len(x) == 1 else x for x in r]
    return {'list': r, 'count': c} def selection_sort(n, key=0):
    c = 0
    r = [x if isinstance(x, list) else [x] for x in n[:]]
    l = len(r)
    for i in range(0, l):
        mini = i
        for j in range(i, l):
            if (r[j][key] < r[mini][key]):
                mini = j
        if mini != i:
            r[i], r[mini] = r[mini], r[i]
            c += 1
    r = [x[0] if len(x) == 1 else x for x in r]
    return {'list': r, 'count': c} selection_sort def card_format(cards):
    return ' '.join([x[0] + str(x[1]) for x in cards]) def card_format(cards):
    return ' '.join([x[0] + str(x[1]) for x in cards]) card_format ssc = card_format(selection_sort(c, 1)['list']) card_format(selection_sort(c, 1)['list']) ssc
def bubble_sort(n, key=0):
    c = 0
    r = [x if isinstance(x, list) else [x] for x in n[:]]
    l = len(r) - 1
    for i in range(0, l):
        for j in range(l, i, -1):
            if r[j][key] < r[j-1][key]:
                r[j], r[j-1] = r[j-1], r[j]
                c += 1
    r = [x[0] if len(x) == 1 else x for x in r]
    return {'list': r, 'count': c}

def selection_sort(n, key=0):
    c = 0
    r = [x if isinstance(x, list) else [x] for x in n[:]]
    l = len(r)
    for i in range(0, l):
        mini = i
        for j in range(i, l):
            if (r[j][key] < r[mini][key]):
                mini = j
        if mini != i:
            r[i], r[mini] = r[mini], r[i]
            c += 1
    r = [x[0] if len(x) == 1 else x for x in r]
    return {'list': r, 'count': c}

def card_format(cards):
    return ' '.join([x[0] + str(x[1]) for x in cards])

if __name__ == '__main__':
    n = int(input())
    c = list(map(list, input().split(' ')))
    c = [[x[0], int(x[1])] for x in c]
    bsc = card_format(bubble_sort(c, 1)['list'])
    ssc = card_format(selection_sort(c, 1)['list'])
    print(bsc)
    print('Stable')
    print(ssc)
    print('Stable' if bsc == ssc else 'Not stable')

