import sys
import copy

def print_data(data):
    last_index = len(data) - 1
    
    for idx, item in enumerate(data):
        print(item, end='')
        if idx != last_index:
            print(' ', end='')

    print('')

def bubbleSort(data, N):
    for i in range(N):
        j = N -1
        while j >= i+1:
            if data[j][1] < data[j-1][1]:
                hoge = data[j]
                data[j] = data[j-1]
                data[j-1] = hoge
            j -= 1
    return data

def selectSort(data, N):
    for i in range(N):
        mini = i
        for j in range(i, N):
            if data[j][1] < data[mini][1]:
                mini = j
        if i == mini:
            pass
        else:
            hoge = data[mini]
            data[mini] = data[i]
            data[i] = hoge
    
    return data

def isStable(inData, outData, N):
    for i in range(N):
        for j in range(i+1, N):
            if inData[i][1] == inData[j][1]:
                a = outData.index(inData[i])
                b = outData.index(inData[j])
                if a > b :
                    return False
    return True




def main():
    N = int(input())
    data0 = list(input().split())
    data1 = copy.deepcopy(data0)
    data2 = copy.deepcopy(data0)
    
    bubbleData = bubbleSort(data1, N)
    selectData = selectSort(data2, N)
    
    
                  
        
    print_data(bubbleData)
    if isStable(data0, bubbleData, N):
        print('Stable')
    else:
        print('Not stable')
    
        
    print_data(selectData)
    if isStable(data0, selectData, N):
        print('Stable')
    else:
        print('Not stable')

            
if __name__ == '__main__':
    main() import sys sys import copy copy def print_data(data):
    last_index = len(data) - 1
    
    for idx, item in enumerate(data):
        print(item, end='')
        if idx != last_index:
            print(' ', end='')

    print('') print_data last_index = len(data) - 1 last_index len(data) - 1 len(data) len data 1 for idx, item in enumerate(data):
        print(item, end='')
        if idx != last_index:
            print(' ', end='')

     idx item enumerate(data) enumerate data print(item, end='') print item end='' end '' if idx != last_index:
            print(' ', end='')

     idx != last_index idx last_index print(' ', end='') print ' ' end='' end '' print('') print '' data data def bubbleSort(data, N):
    for i in range(N):
        j = N -1
        while j >= i+1:
            if data[j][1] < data[j-1][1]:
                hoge = data[j]
                data[j] = data[j-1]
                data[j-1] = hoge
            j -= 1
    return data bubbleSort for i in range(N):
        j = N -1
        while j >= i+1:
            if data[j][1] < data[j-1][1]:
                hoge = data[j]
                data[j] = data[j-1]
                data[j-1] = hoge
            j -= 1
     i range(N) range N j = N -1 j N -1 N 1 while j >= i+1:
            if data[j][1] < data[j-1][1]:
                hoge = data[j]
                data[j] = data[j-1]
                data[j-1] = hoge
            j -= 1
     j >= i+1 j i+1 i 1 if data[j][1] < data[j-1][1]:
                hoge = data[j]
                data[j] = data[j-1]
                data[j-1] = hoge
             data[j][1] < data[j-1][1] data[j][1] [j] data j 1 data[j-1][1] [j-1] data j-1 j 1 1 hoge = data[j] hoge data[j] data j data[j] = data[j-1] data[j] data j data[j-1] data j-1 j 1 data[j-1] = hoge data[j-1] data j-1 j 1 hoge j -= 1 j 1 return data data data data N N def selectSort(data, N):
    for i in range(N):
        mini = i
        for j in range(i, N):
            if data[j][1] < data[mini][1]:
                mini = j
        if i == mini:
            pass
        else:
            hoge = data[mini]
            data[mini] = data[i]
            data[i] = hoge
    
    return data selectSort for i in range(N):
        mini = i
        for j in range(i, N):
            if data[j][1] < data[mini][1]:
                mini = j
        if i == mini:
            pass
        else:
            hoge = data[mini]
            data[mini] = data[i]
            data[i] = hoge
    
     i range(N) range N mini = i mini i for j in range(i, N):
            if data[j][1] < data[mini][1]:
                mini = j
         j range(i, N) range i N if data[j][1] < data[mini][1]:
                mini = j
         data[j][1] < data[mini][1] data[j][1] [j] data j 1 data[mini][1] [mini] data mini 1 mini = j mini j if i == mini:
            pass
        else:
            hoge = data[mini]
            data[mini] = data[i]
            data[i] = hoge
    
     i == mini i mini hoge = data[mini] hoge data[mini] data mini data[mini] = data[i] data[mini] data mini data[i] data i data[i] = hoge data[i] data i hoge return data data data data N N def isStable(inData, outData, N):
    for i in range(N):
        for j in range(i+1, N):
            if inData[i][1] == inData[j][1]:
                a = outData.index(inData[i])
                b = outData.index(inData[j])
                if a > b :
                    return False
    return True isStable for i in range(N):
        for j in range(i+1, N):
            if inData[i][1] == inData[j][1]:
                a = outData.index(inData[i])
                b = outData.index(inData[j])
                if a > b :
                    return False
     i range(N) range N for j in range(i+1, N):
            if inData[i][1] == inData[j][1]:
                a = outData.index(inData[i])
                b = outData.index(inData[j])
                if a > b :
                    return False
     j range(i+1, N) range i+1 i 1 N if inData[i][1] == inData[j][1]:
                a = outData.index(inData[i])
                b = outData.index(inData[j])
                if a > b :
                    return False
     inData[i][1] == inData[j][1] inData[i][1] [i] inData i 1 inData[j][1] [j] inData j 1 a = outData.index(inData[i]) a outData.index(inData[i]) outData.index outData index inData[i] inData i b = outData.index(inData[j]) b outData.index(inData[j]) outData.index outData index inData[j] inData j if a > b :
                    return False
     a > b a b return False False return True True inData inData outData outData N N def main():
    N = int(input())
    data0 = list(input().split())
    data1 = copy.deepcopy(data0)
    data2 = copy.deepcopy(data0)
    
    bubbleData = bubbleSort(data1, N)
    selectData = selectSort(data2, N)
    
    
                  
        
    print_data(bubbleData)
    if isStable(data0, bubbleData, N):
        print('Stable')
    else:
        print('Not stable')
    
        
    print_data(selectData)
    if isStable(data0, selectData, N):
        print('Stable')
    else:
        print('Not stable')

             main N = int(input()) N int(input()) int input() input data0 = list(input().split()) data0 list(input().split()) list input().split() ().split () input split data1 = copy.deepcopy(data0) data1 copy.deepcopy(data0) copy.deepcopy copy deepcopy data0 data2 = copy.deepcopy(data0) data2 copy.deepcopy(data0) copy.deepcopy copy deepcopy data0 bubbleData = bubbleSort(data1, N) bubbleData bubbleSort(data1, N) bubbleSort data1 N selectData = selectSort(data2, N) selectData selectSort(data2, N) selectSort data2 N print_data(bubbleData) print_data bubbleData if isStable(data0, bubbleData, N):
        print('Stable')
    else:
        print('Not stable')
    
        
     isStable(data0, bubbleData, N) isStable data0 bubbleData N print('Stable') print 'Stable' print('Not stable') print 'Not stable' print_data(selectData) print_data selectData if isStable(data0, selectData, N):
        print('Stable')
    else:
        print('Not stable')

             isStable(data0, selectData, N) isStable data0 selectData N print('Stable') print 'Stable' print('Not stable') print 'Not stable' if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def selectSort(data, N):
    for i in range(N):
        mini = i
        for j in range(i, N):
            if data[j][1] < data[mini][1]:
                mini = j
        if i == mini:
            pass
        else:
            hoge = data[mini]
            data[mini] = data[i]
            data[i] = hoge
    
    return data def selectSort(data, N):
    for i in range(N):
        mini = i
        for j in range(i, N):
            if data[j][1] < data[mini][1]:
                mini = j
        if i == mini:
            pass
        else:
            hoge = data[mini]
            data[mini] = data[i]
            data[i] = hoge
    
    return data selectSort def main():
    N = int(input())
    data0 = list(input().split())
    data1 = copy.deepcopy(data0)
    data2 = copy.deepcopy(data0)
    
    bubbleData = bubbleSort(data1, N)
    selectData = selectSort(data2, N)
    
    
                  
        
    print_data(bubbleData)
    if isStable(data0, bubbleData, N):
        print('Stable')
    else:
        print('Not stable')
    
        
    print_data(selectData)
    if isStable(data0, selectData, N):
        print('Stable')
    else:
        print('Not stable')

             def main():
    N = int(input())
    data0 = list(input().split())
    data1 = copy.deepcopy(data0)
    data2 = copy.deepcopy(data0)
    
    bubbleData = bubbleSort(data1, N)
    selectData = selectSort(data2, N)
    
    
                  
        
    print_data(bubbleData)
    if isStable(data0, bubbleData, N):
        print('Stable')
    else:
        print('Not stable')
    
        
    print_data(selectData)
    if isStable(data0, selectData, N):
        print('Stable')
    else:
        print('Not stable')

             main def print_data(data):
    last_index = len(data) - 1
    
    for idx, item in enumerate(data):
        print(item, end='')
        if idx != last_index:
            print(' ', end='')

    print('') def print_data(data):
    last_index = len(data) - 1
    
    for idx, item in enumerate(data):
        print(item, end='')
        if idx != last_index:
            print(' ', end='')

    print('') print_data def isStable(inData, outData, N):
    for i in range(N):
        for j in range(i+1, N):
            if inData[i][1] == inData[j][1]:
                a = outData.index(inData[i])
                b = outData.index(inData[j])
                if a > b :
                    return False
    return True def isStable(inData, outData, N):
    for i in range(N):
        for j in range(i+1, N):
            if inData[i][1] == inData[j][1]:
                a = outData.index(inData[i])
                b = outData.index(inData[j])
                if a > b :
                    return False
    return True isStable def bubbleSort(data, N):
    for i in range(N):
        j = N -1
        while j >= i+1:
            if data[j][1] < data[j-1][1]:
                hoge = data[j]
                data[j] = data[j-1]
                data[j-1] = hoge
            j -= 1
    return data def bubbleSort(data, N):
    for i in range(N):
        j = N -1
        while j >= i+1:
            if data[j][1] < data[j-1][1]:
                hoge = data[j]
                data[j] = data[j-1]
                data[j-1] = hoge
            j -= 1
    return data bubbleSort
import sys
import copy

def print_data(data):
    last_index = len(data) - 1
    
    for idx, item in enumerate(data):
        print(item, end='')
        if idx != last_index:
            print(' ', end='')

    print('')

def bubbleSort(data, N):
    for i in range(N):
        j = N -1
        while j >= i+1:
            if data[j][1] < data[j-1][1]:
                hoge = data[j]
                data[j] = data[j-1]
                data[j-1] = hoge
            j -= 1
    return data

def selectSort(data, N):
    for i in range(N):
        mini = i
        for j in range(i, N):
            if data[j][1] < data[mini][1]:
                mini = j
        if i == mini:
            pass
        else:
            hoge = data[mini]
            data[mini] = data[i]
            data[i] = hoge
    
    return data

def isStable(inData, outData, N):
    for i in range(N):
        for j in range(i+1, N):
            if inData[i][1] == inData[j][1]:
                a = outData.index(inData[i])
                b = outData.index(inData[j])
                if a > b :
                    return False
    return True




def main():
    N = int(input())
    data0 = list(input().split())
    data1 = copy.deepcopy(data0)
    data2 = copy.deepcopy(data0)
    
    bubbleData = bubbleSort(data1, N)
    selectData = selectSort(data2, N)
    
    
                  
        
    print_data(bubbleData)
    if isStable(data0, bubbleData, N):
        print('Stable')
    else:
        print('Not stable')
    
        
    print_data(selectData)
    if isStable(data0, selectData, N):
        print('Stable')
    else:
        print('Not stable')

            
if __name__ == '__main__':
    main()
