def bSort(A):
    flag = 1
    i = 0
    count = 0
    while flag:
        flag = 0
        for j in range(len(A)-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
                flag = 1
                count += 1
        i += 1
    return A, count

def sSort(A):
    count = 0
    for i in range(len(A)):
        minj = i
        for j in range(i, len(A)):
            if int(A[j][1]) < int(A[minj][1]):
                minj = j
        if minj != i:
            count += 1
        tmp = A[i]
        A[i] = A[minj]
        A[minj] = tmp
    return A, count

def isStable(input_a, output_a):
    for v1, v2 in zip(input_a, output_a):
        if v1 != v2:
            return 'Not stable'
    return 'Stable'

if __name__ == '__main__':
    length = input()
    data = input().split()
    sort_s, _= sSort(list(data))
    sort_b, _= bSort(list(data))
    print (' '.join([str(x) for x in sort_b]))
    print (isStable(sort_b, sort_b))
    print (' '.join([str(x) for x in sort_s]))
    print (isStable(sort_b, sort_s)) def bSort(A):
    flag = 1
    i = 0
    count = 0
    while flag:
        flag = 0
        for j in range(len(A)-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
                flag = 1
                count += 1
        i += 1
    return A, count bSort flag = 1 flag 1 i = 0 i 0 count = 0 count 0 while flag:
        flag = 0
        for j in range(len(A)-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
                flag = 1
                count += 1
        i += 1
     flag flag = 0 flag 0 for j in range(len(A)-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
                flag = 1
                count += 1
         j range(len(A)-1, i, -1) range len(A)-1 len(A) len A 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
                flag = 1
                count += 1
         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 flag = 1 flag 1 count += 1 count 1 i += 1 i 1 return A, count return A, count A count A A def sSort(A):
    count = 0
    for i in range(len(A)):
        minj = i
        for j in range(i, len(A)):
            if int(A[j][1]) < int(A[minj][1]):
                minj = j
        if minj != i:
            count += 1
        tmp = A[i]
        A[i] = A[minj]
        A[minj] = tmp
    return A, count sSort count = 0 count 0 for i in range(len(A)):
        minj = i
        for j in range(i, len(A)):
            if int(A[j][1]) < int(A[minj][1]):
                minj = j
        if minj != i:
            count += 1
        tmp = A[i]
        A[i] = A[minj]
        A[minj] = tmp
     i range(len(A)) range len(A) len A minj = i minj i for j in range(i, len(A)):
            if int(A[j][1]) < int(A[minj][1]):
                minj = j
         j range(i, len(A)) range i len(A) len A if int(A[j][1]) < int(A[minj][1]):
                minj = j
         int(A[j][1]) < int(A[minj][1]) int(A[j][1]) int A[j][1] [j] A j 1 int(A[minj][1]) int A[minj][1] [minj] A minj 1 minj = j minj j if minj != i:
            count += 1
         minj != i minj i count += 1 count 1 tmp = A[i] tmp A[i] A i A[i] = A[minj] A[i] A i A[minj] A minj A[minj] = tmp A[minj] A minj tmp return A, count return A, count A count A A def isStable(input_a, output_a):
    for v1, v2 in zip(input_a, output_a):
        if v1 != v2:
            return 'Not stable'
    return 'Stable' isStable for v1, v2 in zip(input_a, output_a):
        if v1 != v2:
            return 'Not stable'
     v1 v2 zip(input_a, output_a) zip input_a output_a if v1 != v2:
            return 'Not stable'
     v1 != v2 v1 v2 return 'Not stable' 'Not stable' return 'Stable' 'Stable' input_a input_a output_a output_a if __name__ == '__main__':
    length = input()
    data = input().split()
    sort_s, _= sSort(list(data))
    sort_b, _= bSort(list(data))
    print (' '.join([str(x) for x in sort_b]))
    print (isStable(sort_b, sort_b))
    print (' '.join([str(x) for x in sort_s]))
    print (isStable(sort_b, sort_s)) __name__ == '__main__' __name__ '__main__' length = input() length input() input data = input().split() data input().split() ().split () input split sort_s, _= sSort(list(data)) sort_s sSort(list(data)) sSort list(data) list data _ sort_b, _= bSort(list(data)) sort_b bSort(list(data)) bSort list(data) list data _ str(x) for x in sort_b for x in sort_b x sort_b for x in sort_b str(x) str x print (' '.join([str(x) for x in sort_b])) print ' '.join([str(x) for x in sort_b]) ' '.join ' ' join [str(x) for x in sort_b] print (isStable(sort_b, sort_b)) print isStable(sort_b, sort_b) isStable sort_b sort_b str(x) for x in sort_s for x in sort_s x sort_s for x in sort_s str(x) str x print (' '.join([str(x) for x in sort_s])) print ' '.join([str(x) for x in sort_s]) ' '.join ' ' join [str(x) for x in sort_s] print (isStable(sort_b, sort_s)) print isStable(sort_b, sort_s) isStable sort_b sort_s _= sSort(list(data)) sSort(list(data)) _ sort_b, _= bSort(list(data)) bSort(list(data)) sort_b _= bSort(list(data)) bSort(list(data)) _ def isStable(input_a, output_a):
    for v1, v2 in zip(input_a, output_a):
        if v1 != v2:
            return 'Not stable'
    return 'Stable' def isStable(input_a, output_a):
    for v1, v2 in zip(input_a, output_a):
        if v1 != v2:
            return 'Not stable'
    return 'Stable' isStable sort_s, _= sSort(list(data)) sSort(list(data)) sort_s length = input() input() length def sSort(A):
    count = 0
    for i in range(len(A)):
        minj = i
        for j in range(i, len(A)):
            if int(A[j][1]) < int(A[minj][1]):
                minj = j
        if minj != i:
            count += 1
        tmp = A[i]
        A[i] = A[minj]
        A[minj] = tmp
    return A, count def sSort(A):
    count = 0
    for i in range(len(A)):
        minj = i
        for j in range(i, len(A)):
            if int(A[j][1]) < int(A[minj][1]):
                minj = j
        if minj != i:
            count += 1
        tmp = A[i]
        A[i] = A[minj]
        A[minj] = tmp
    return A, count sSort data = input().split() input().split() data def bSort(A):
    flag = 1
    i = 0
    count = 0
    while flag:
        flag = 0
        for j in range(len(A)-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
                flag = 1
                count += 1
        i += 1
    return A, count def bSort(A):
    flag = 1
    i = 0
    count = 0
    while flag:
        flag = 0
        for j in range(len(A)-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
                flag = 1
                count += 1
        i += 1
    return A, count bSort
def bSort(A):
    flag = 1
    i = 0
    count = 0
    while flag:
        flag = 0
        for j in range(len(A)-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
                flag = 1
                count += 1
        i += 1
    return A, count

def sSort(A):
    count = 0
    for i in range(len(A)):
        minj = i
        for j in range(i, len(A)):
            if int(A[j][1]) < int(A[minj][1]):
                minj = j
        if minj != i:
            count += 1
        tmp = A[i]
        A[i] = A[minj]
        A[minj] = tmp
    return A, count

def isStable(input_a, output_a):
    for v1, v2 in zip(input_a, output_a):
        if v1 != v2:
            return 'Not stable'
    return 'Stable'

if __name__ == '__main__':
    length = input()
    data = input().split()
    sort_s, _= sSort(list(data))
    sort_b, _= bSort(list(data))
    print (' '.join([str(x) for x in sort_b]))
    print (isStable(sort_b, sort_b))
    print (' '.join([str(x) for x in sort_s]))
    print (isStable(sort_b, sort_s))
