import copy

def stable(origin, new):
    for i in range(n-1):
        if int(new[i][1]) == int(new[i+1][1]):
            if origin.index(new[i]) > origin.index(new[i+1]):
                return False
    return True

n = int(raw_input())
a_origin = raw_input().split()

a = copy.copy(a_origin)
b = copy.copy(a_origin)

# bubble sort
for i in range(n):
    for j in range(n-1, i, -1):
        if int(a[j][1]) < int(a[j-1][1]):
            tmp = a[j]
            a[j] = a[j-1]
            a[j-1] = tmp

for i in range(n):
    if i == n-1:
        print a[i]
    else:
        print a[i], 

if stable(a_origin, a):
    print 'Stable'
else:
    print 'Not stable'

# selection sort
for i in range(n):
    minj = i
    for j in range(i, n):
        if int(b[j][1]) < int(b[minj][1]):
            minj = j
    tmp = b[i]
    b[i] = b[minj]
    b[minj] = tmp

for i in range(n):
    if i == n-1:
        print b[i]
    else:
        print b[i],

if stable(a_origin, b):
    print 'Stable'
else:
    print 'Not stable' import copy copy def stable(origin, new):
    for i in range(n-1):
        if int(new[i][1]) == int(new[i+1][1]):
            if origin.index(new[i]) > origin.index(new[i+1]):
                return False
    return True stable for i in range(n-1):
        if int(new[i][1]) == int(new[i+1][1]):
            if origin.index(new[i]) > origin.index(new[i+1]):
                return False
     i range(n-1) range n-1 n 1 if int(new[i][1]) == int(new[i+1][1]):
            if origin.index(new[i]) > origin.index(new[i+1]):
                return False
     int(new[i][1]) == int(new[i+1][1]) int(new[i][1]) int new[i][1] [i] new i 1 int(new[i+1][1]) int new[i+1][1] [i+1] new i+1 i 1 1 if origin.index(new[i]) > origin.index(new[i+1]):
                return False
     origin.index(new[i]) > origin.index(new[i+1]) origin.index(new[i]) origin.index origin index new[i] new i origin.index(new[i+1]) origin.index origin index new[i+1] new i+1 i 1 return False False return True True origin origin new new n = int(raw_input()) n int(raw_input()) int raw_input() raw_input a_origin = raw_input().split() a_origin raw_input().split() ().split () raw_input split a = copy.copy(a_origin) a copy.copy(a_origin) copy.copy copy copy a_origin b = copy.copy(a_origin) b copy.copy(a_origin) copy.copy copy copy a_origin for i in range(n):
    for j in range(n-1, i, -1):
        if int(a[j][1]) < int(a[j-1][1]):
            tmp = a[j]
            a[j] = a[j-1]
            a[j-1] = tmp i range(n) range n for j in range(n-1, i, -1):
        if int(a[j][1]) < int(a[j-1][1]):
            tmp = a[j]
            a[j] = a[j-1]
            a[j-1] = tmp j range(n-1, i, -1) range n-1 n 1 i -1 if int(a[j][1]) < int(a[j-1][1]):
            tmp = a[j]
            a[j] = a[j-1]
            a[j-1] = tmp int(a[j][1]) < int(a[j-1][1]) int(a[j][1]) int a[j][1] [j] a j 1 int(a[j-1][1]) int a[j-1][1] [j-1] a j-1 j 1 1 tmp = a[j] tmp a[j] a j a[j] = a[j-1] a[j] a j a[j-1] a j-1 j 1 a[j-1] = tmp a[j-1] a j-1 j 1 tmp for i in range(n):
    if i == n-1:
        print a[i]
    else:
        print a[i],  i range(n) range n if i == n-1:
        print a[i]
    else:
        print a[i],  i == n-1 i n-1 n 1 print a[i] a i print a[i] a i if stable(a_origin, a):
    print 'Stable'
else:
    print 'Not stable'

# selection sort stable(a_origin, a) stable a_origin a print print for i in range(n):
    minj = i
    for j in range(i, n):
        if int(b[j][1]) < int(b[minj][1]):
            minj = j
    tmp = b[i]
    b[i] = b[minj]
    b[minj] = tmp i range(n) range n minj = i minj i for j in range(i, n):
        if int(b[j][1]) < int(b[minj][1]):
            minj = j
     j range(i, n) range i n if int(b[j][1]) < int(b[minj][1]):
            minj = j
     int(b[j][1]) < int(b[minj][1]) int(b[j][1]) int b[j][1] [j] b j 1 int(b[minj][1]) int b[minj][1] [minj] b minj 1 minj = j minj j tmp = b[i] tmp b[i] b i b[i] = b[minj] b[i] b i b[minj] b minj b[minj] = tmp b[minj] b minj tmp for i in range(n):
    if i == n-1:
        print b[i]
    else:
        print b[i], i range(n) range n if i == n-1:
        print b[i]
    else:
        print b[i], i == n-1 i n-1 n 1 print b[i] b i print b[i] b i if stable(a_origin, b):
    print 'Stable'
else:
    print 'Not stable' stable(a_origin, b) stable a_origin b print print b = copy.copy(a_origin) copy.copy(a_origin) b tmp = a[j] a[j] tmp minj = i i minj tmp = b[i] b[i] tmp def stable(origin, new):
    for i in range(n-1):
        if int(new[i][1]) == int(new[i+1][1]):
            if origin.index(new[i]) > origin.index(new[i+1]):
                return False
    return True def stable(origin, new):
    for i in range(n-1):
        if int(new[i][1]) == int(new[i+1][1]):
            if origin.index(new[i]) > origin.index(new[i+1]):
                return False
    return True stable a = copy.copy(a_origin) copy.copy(a_origin) a minj = j j minj n = int(raw_input()) int(raw_input()) n a_origin = raw_input().split() raw_input().split() a_origin
import copy

def stable(origin, new):
    for i in range(n-1):
        if int(new[i][1]) == int(new[i+1][1]):
            if origin.index(new[i]) > origin.index(new[i+1]):
                return False
    return True

n = int(raw_input())
a_origin = raw_input().split()

a = copy.copy(a_origin)
b = copy.copy(a_origin)

# bubble sort
for i in range(n):
    for j in range(n-1, i, -1):
        if int(a[j][1]) < int(a[j-1][1]):
            tmp = a[j]
            a[j] = a[j-1]
            a[j-1] = tmp

for i in range(n):
    if i == n-1:
        print a[i]
    else:
        print a[i], 

if stable(a_origin, a):
    print 'Stable'
else:
    print 'Not stable'

# selection sort
for i in range(n):
    minj = i
    for j in range(i, n):
        if int(b[j][1]) < int(b[minj][1]):
            minj = j
    tmp = b[i]
    b[i] = b[minj]
    b[minj] = tmp

for i in range(n):
    if i == n-1:
        print b[i]
    else:
        print b[i],

if stable(a_origin, b):
    print 'Stable'
else:
    print 'Not stable'