import copy

def bubble_sort(a):
    ba = copy.copy(a)
    flag = True
    for i in range(0, n):
        if not flag:
            break
        flag = False
        for j in range(n-1, 0, -1):
            if int(ba[j][1]) < int(ba[j-1][1]):
                ba[j], ba[j-1] = ba[j-1], ba[j]
                flag = True

    return ba

def selection_sort(a):
    sa = copy.copy(a)
    for i in range(n):
        flag = False
        minj = i
        for j in range(i, n):
            if int(sa[j][1]) < int(sa[minj][1]):
                minj = j
                flag = True
        if flag:
            sa[i], sa[minj] = sa[minj], sa[i]

    return sa

def is_stable(ba, sa):
    for b, s in zip(ba, sa):
        if b != s:
            return 'Not stable'
    return 'Stable'


n = int(input())
a = list(map(str, input().split()))

ba = bubble_sort(a)
print(' '.join(map(str, ba)))
print('Stable')

sa = selection_sort(a)
print(' '.join(map(str, sa)))
print(is_stable(ba, sa))
 import copy copy def bubble_sort(a):
    ba = copy.copy(a)
    flag = True
    for i in range(0, n):
        if not flag:
            break
        flag = False
        for j in range(n-1, 0, -1):
            if int(ba[j][1]) < int(ba[j-1][1]):
                ba[j], ba[j-1] = ba[j-1], ba[j]
                flag = True

    return ba bubble_sort ba = copy.copy(a) ba copy.copy(a) copy.copy copy copy a flag = True flag True for i in range(0, n):
        if not flag:
            break
        flag = False
        for j in range(n-1, 0, -1):
            if int(ba[j][1]) < int(ba[j-1][1]):
                ba[j], ba[j-1] = ba[j-1], ba[j]
                flag = True

     i range(0, n) range 0 n if not flag:
            break
         not flag flag break flag = False flag False for j in range(n-1, 0, -1):
            if int(ba[j][1]) < int(ba[j-1][1]):
                ba[j], ba[j-1] = ba[j-1], ba[j]
                flag = True

     j range(n-1, 0, -1) range n-1 n 1 0 -1 if int(ba[j][1]) < int(ba[j-1][1]):
                ba[j], ba[j-1] = ba[j-1], ba[j]
                flag = True

     int(ba[j][1]) < int(ba[j-1][1]) int(ba[j][1]) int ba[j][1] [j] ba j 1 int(ba[j-1][1]) int ba[j-1][1] [j-1] ba j-1 j 1 1 ba[j], ba[j-1] = ba[j-1], ba[j] ba[j] ba j ba[j-1] ba j-1 j 1 ba[j-1] ba j-1 j 1 ba[j] ba j flag = True flag True return ba ba a a def selection_sort(a):
    sa = copy.copy(a)
    for i in range(n):
        flag = False
        minj = i
        for j in range(i, n):
            if int(sa[j][1]) < int(sa[minj][1]):
                minj = j
                flag = True
        if flag:
            sa[i], sa[minj] = sa[minj], sa[i]

    return sa selection_sort sa = copy.copy(a) sa copy.copy(a) copy.copy copy copy a for i in range(n):
        flag = False
        minj = i
        for j in range(i, n):
            if int(sa[j][1]) < int(sa[minj][1]):
                minj = j
                flag = True
        if flag:
            sa[i], sa[minj] = sa[minj], sa[i]

     i range(n) range n flag = False flag False minj = i minj i for j in range(i, n):
            if int(sa[j][1]) < int(sa[minj][1]):
                minj = j
                flag = True
         j range(i, n) range i n if int(sa[j][1]) < int(sa[minj][1]):
                minj = j
                flag = True
         int(sa[j][1]) < int(sa[minj][1]) int(sa[j][1]) int sa[j][1] [j] sa j 1 int(sa[minj][1]) int sa[minj][1] [minj] sa minj 1 minj = j minj j flag = True flag True if flag:
            sa[i], sa[minj] = sa[minj], sa[i]

     flag sa[i], sa[minj] = sa[minj], sa[i] sa[i] sa i sa[minj] sa minj sa[minj] sa minj sa[i] sa i return sa sa a a def is_stable(ba, sa):
    for b, s in zip(ba, sa):
        if b != s:
            return 'Not stable'
    return 'Stable' is_stable for b, s in zip(ba, sa):
        if b != s:
            return 'Not stable'
     b s zip(ba, sa) zip ba sa if b != s:
            return 'Not stable'
     b != s b s return 'Not stable' 'Not stable' return 'Stable' 'Stable' ba ba sa sa n = int(input()) n int(input()) int input() input a = list(map(str, input().split())) a list(map(str, input().split())) list map(str, input().split()) map str input().split() ().split () input split ba = bubble_sort(a) ba bubble_sort(a) bubble_sort a print(' '.join(map(str, ba))) print ' '.join(map(str, ba)) ' '.join ' ' join map(str, ba) map str ba print('Stable') print 'Stable' sa = selection_sort(a) sa selection_sort(a) selection_sort a print(' '.join(map(str, sa))) print ' '.join(map(str, sa)) ' '.join ' ' join map(str, sa) map str sa print(is_stable(ba, sa)) print is_stable(ba, sa) is_stable ba sa def bubble_sort(a):
    ba = copy.copy(a)
    flag = True
    for i in range(0, n):
        if not flag:
            break
        flag = False
        for j in range(n-1, 0, -1):
            if int(ba[j][1]) < int(ba[j-1][1]):
                ba[j], ba[j-1] = ba[j-1], ba[j]
                flag = True

    return ba def bubble_sort(a):
    ba = copy.copy(a)
    flag = True
    for i in range(0, n):
        if not flag:
            break
        flag = False
        for j in range(n-1, 0, -1):
            if int(ba[j][1]) < int(ba[j-1][1]):
                ba[j], ba[j-1] = ba[j-1], ba[j]
                flag = True

    return ba bubble_sort def selection_sort(a):
    sa = copy.copy(a)
    for i in range(n):
        flag = False
        minj = i
        for j in range(i, n):
            if int(sa[j][1]) < int(sa[minj][1]):
                minj = j
                flag = True
        if flag:
            sa[i], sa[minj] = sa[minj], sa[i]

    return sa def selection_sort(a):
    sa = copy.copy(a)
    for i in range(n):
        flag = False
        minj = i
        for j in range(i, n):
            if int(sa[j][1]) < int(sa[minj][1]):
                minj = j
                flag = True
        if flag:
            sa[i], sa[minj] = sa[minj], sa[i]

    return sa selection_sort ba = bubble_sort(a) bubble_sort(a) ba n = int(input()) int(input()) n a = list(map(str, input().split())) list(map(str, input().split())) a def is_stable(ba, sa):
    for b, s in zip(ba, sa):
        if b != s:
            return 'Not stable'
    return 'Stable' def is_stable(ba, sa):
    for b, s in zip(ba, sa):
        if b != s:
            return 'Not stable'
    return 'Stable' is_stable sa = selection_sort(a) selection_sort(a) sa
import copy

def bubble_sort(a):
    ba = copy.copy(a)
    flag = True
    for i in range(0, n):
        if not flag:
            break
        flag = False
        for j in range(n-1, 0, -1):
            if int(ba[j][1]) < int(ba[j-1][1]):
                ba[j], ba[j-1] = ba[j-1], ba[j]
                flag = True

    return ba

def selection_sort(a):
    sa = copy.copy(a)
    for i in range(n):
        flag = False
        minj = i
        for j in range(i, n):
            if int(sa[j][1]) < int(sa[minj][1]):
                minj = j
                flag = True
        if flag:
            sa[i], sa[minj] = sa[minj], sa[i]

    return sa

def is_stable(ba, sa):
    for b, s in zip(ba, sa):
        if b != s:
            return 'Not stable'
    return 'Stable'


n = int(input())
a = list(map(str, input().split()))

ba = bubble_sort(a)
print(' '.join(map(str, ba)))
print('Stable')

sa = selection_sort(a)
print(' '.join(map(str, sa)))
print(is_stable(ba, sa))

