n, *aa = open(0).read().split()

def bubble(aa):
    aa = list(map(list, aa))
    n = len(aa)
    for i in range(n):
        for j in range(n-1, i, -1):
            if aa[j-1][1] > aa[j][1]:
                aa[j-1], aa[j] = aa[j], aa[j-1]
    aa = list(map(lambda x: ''.join(x), aa))
    return aa

def selection(aa):
    aa = list(map(list, aa))
    n = len(aa)
    for i in range(n):
        mini = i
        for j in range(i, n):
            if aa[j][1] < aa[mini][1]:
                mini = j
        aa[mini], aa[i] = aa[i], aa[mini]
    aa = list(map(lambda x: ''.join(x), aa))
    return aa

bres = bubble(aa)
sres = selection(aa)

print(*bres)
print('Stable')
print(*sres)
if bres == sres:
    print('Stable')
else:
    print('Not stable') n, *aa = open(0).read().split() n open(0).read().split() (0).read().split (0).read() (0).read (0) open 0 read split *aa aa def bubble(aa):
    aa = list(map(list, aa))
    n = len(aa)
    for i in range(n):
        for j in range(n-1, i, -1):
            if aa[j-1][1] > aa[j][1]:
                aa[j-1], aa[j] = aa[j], aa[j-1]
    aa = list(map(lambda x: ''.join(x), aa))
    return aa bubble aa = list(map(list, aa)) aa list(map(list, aa)) list map(list, aa) map list aa n = len(aa) n len(aa) len aa for i in range(n):
        for j in range(n-1, i, -1):
            if aa[j-1][1] > aa[j][1]:
                aa[j-1], aa[j] = aa[j], aa[j-1]
     i range(n) range n for j in range(n-1, i, -1):
            if aa[j-1][1] > aa[j][1]:
                aa[j-1], aa[j] = aa[j], aa[j-1]
     j range(n-1, i, -1) range n-1 n 1 i -1 if aa[j-1][1] > aa[j][1]:
                aa[j-1], aa[j] = aa[j], aa[j-1]
     aa[j-1][1] > aa[j][1] aa[j-1][1] [j-1] aa j-1 j 1 1 aa[j][1] [j] aa j 1 aa[j-1], aa[j] = aa[j], aa[j-1] aa[j-1] aa j-1 j 1 aa[j] aa j aa[j] aa j aa[j-1] aa j-1 j 1 aa = list(map(lambda x: ''.join(x), aa)) aa list(map(lambda x: ''.join(x), aa)) list map(lambda x: ''.join(x), aa) map lambda x: ''.join(x) ''.join(x) ''.join '' join x x aa return aa aa aa aa def selection(aa):
    aa = list(map(list, aa))
    n = len(aa)
    for i in range(n):
        mini = i
        for j in range(i, n):
            if aa[j][1] < aa[mini][1]:
                mini = j
        aa[mini], aa[i] = aa[i], aa[mini]
    aa = list(map(lambda x: ''.join(x), aa))
    return aa selection aa = list(map(list, aa)) aa list(map(list, aa)) list map(list, aa) map list aa n = len(aa) n len(aa) len aa for i in range(n):
        mini = i
        for j in range(i, n):
            if aa[j][1] < aa[mini][1]:
                mini = j
        aa[mini], aa[i] = aa[i], aa[mini]
     i range(n) range n mini = i mini i for j in range(i, n):
            if aa[j][1] < aa[mini][1]:
                mini = j
         j range(i, n) range i n if aa[j][1] < aa[mini][1]:
                mini = j
         aa[j][1] < aa[mini][1] aa[j][1] [j] aa j 1 aa[mini][1] [mini] aa mini 1 mini = j mini j aa[mini], aa[i] = aa[i], aa[mini] aa[mini] aa mini aa[i] aa i aa[i] aa i aa[mini] aa mini aa = list(map(lambda x: ''.join(x), aa)) aa list(map(lambda x: ''.join(x), aa)) list map(lambda x: ''.join(x), aa) map lambda x: ''.join(x) ''.join(x) ''.join '' join x x aa return aa aa aa aa bres = bubble(aa) bres bubble(aa) bubble aa sres = selection(aa) sres selection(aa) selection aa print(*bres) print *bres bres print('Stable') print 'Stable' print(*sres) print *sres sres if bres == sres:
    print('Stable')
else:
    print('Not stable') bres == sres bres sres print('Stable') print 'Stable' print('Not stable') print 'Not stable' def selection(aa):
    aa = list(map(list, aa))
    n = len(aa)
    for i in range(n):
        mini = i
        for j in range(i, n):
            if aa[j][1] < aa[mini][1]:
                mini = j
        aa[mini], aa[i] = aa[i], aa[mini]
    aa = list(map(lambda x: ''.join(x), aa))
    return aa def selection(aa):
    aa = list(map(list, aa))
    n = len(aa)
    for i in range(n):
        mini = i
        for j in range(i, n):
            if aa[j][1] < aa[mini][1]:
                mini = j
        aa[mini], aa[i] = aa[i], aa[mini]
    aa = list(map(lambda x: ''.join(x), aa))
    return aa selection bres = bubble(aa) bubble(aa) bres n, *aa = open(0).read().split() open(0).read().split() n sres = selection(aa) selection(aa) sres def bubble(aa):
    aa = list(map(list, aa))
    n = len(aa)
    for i in range(n):
        for j in range(n-1, i, -1):
            if aa[j-1][1] > aa[j][1]:
                aa[j-1], aa[j] = aa[j], aa[j-1]
    aa = list(map(lambda x: ''.join(x), aa))
    return aa def bubble(aa):
    aa = list(map(list, aa))
    n = len(aa)
    for i in range(n):
        for j in range(n-1, i, -1):
            if aa[j-1][1] > aa[j][1]:
                aa[j-1], aa[j] = aa[j], aa[j-1]
    aa = list(map(lambda x: ''.join(x), aa))
    return aa bubble
n, *aa = open(0).read().split()

def bubble(aa):
    aa = list(map(list, aa))
    n = len(aa)
    for i in range(n):
        for j in range(n-1, i, -1):
            if aa[j-1][1] > aa[j][1]:
                aa[j-1], aa[j] = aa[j], aa[j-1]
    aa = list(map(lambda x: ''.join(x), aa))
    return aa

def selection(aa):
    aa = list(map(list, aa))
    n = len(aa)
    for i in range(n):
        mini = i
        for j in range(i, n):
            if aa[j][1] < aa[mini][1]:
                mini = j
        aa[mini], aa[i] = aa[i], aa[mini]
    aa = list(map(lambda x: ''.join(x), aa))
    return aa

bres = bubble(aa)
sres = selection(aa)

print(*bres)
print('Stable')
print(*sres)
if bres == sres:
    print('Stable')
else:
    print('Not stable')
