from copy import copy

def bubbleSort(arr):
    cp_arr = copy(arr)
    indices = list(range(len(cp_arr)))
    
    finished_flag = False
    while not finished_flag:
        finished_flag = True

        for i in range(len(cp_arr)-1):
            if cp_arr[i] > cp_arr[i+1]:
                finished_flag = False

                t = cp_arr[i]
                cp_arr[i] = cp_arr[i+1]
                cp_arr[i+1] = t

                t = indices[i]
                indices[i] = indices[i+1]
                indices[i+1] = t
    return indices

def selectionSort(arr):
    cp_arr = copy(arr)
    indices = list(range(len(cp_arr)))

    for i in range(len(cp_arr)):
        min = i
        for j in range(i+1, len(cp_arr)):
            if cp_arr[j] < cp_arr[min]:
                min = j

        t = cp_arr[i]
        cp_arr[i] = cp_arr[min]
        cp_arr[min] = t

        t = indices[i]
        indices[i] = indices[min]
        indices[min] = t

    return indices

if __name__ == "__main__":
    input()
    cards = input().split()
    values = list(map(lambda x: int(x[1]), cards))
    bubbleSorted_indices = bubbleSort(values)
    selectionSorted_indices = selectionSort(values)
    bubble_stable = True
    selection_stable = True
    for i in range(len(values)-1):
        if values[bubbleSorted_indices[i]] == values[bubbleSorted_indices[i+1]] and bubbleSorted_indices[i] > bubbleSorted_indices[i+1]:
            bubble_stable = False
        if values[selectionSorted_indices[i]] == values[selectionSorted_indices[i+1]] and selectionSorted_indices[i] > selectionSorted_indices[i+1]:
            selection_stable = False
    bubble_sorted = [cards[i] for i in bubbleSorted_indices]
    selection_sorted = [cards[i] for i in selectionSorted_indices]
    print(" ".join(bubble_sorted))
    print("Stable" if bubble_stable else "Not stable") 
    print(" ".join(selection_sorted))
    print("Stable" if selection_stable else "Not stable") 


 from copy import copy def bubbleSort(arr):
    cp_arr = copy(arr)
    indices = list(range(len(cp_arr)))
    
    finished_flag = False
    while not finished_flag:
        finished_flag = True

        for i in range(len(cp_arr)-1):
            if cp_arr[i] > cp_arr[i+1]:
                finished_flag = False

                t = cp_arr[i]
                cp_arr[i] = cp_arr[i+1]
                cp_arr[i+1] = t

                t = indices[i]
                indices[i] = indices[i+1]
                indices[i+1] = t
    return indices bubbleSort cp_arr = copy(arr) cp_arr copy(arr) copy arr indices = list(range(len(cp_arr))) indices list(range(len(cp_arr))) list range(len(cp_arr)) range len(cp_arr) len cp_arr finished_flag = False finished_flag False while not finished_flag:
        finished_flag = True

        for i in range(len(cp_arr)-1):
            if cp_arr[i] > cp_arr[i+1]:
                finished_flag = False

                t = cp_arr[i]
                cp_arr[i] = cp_arr[i+1]
                cp_arr[i+1] = t

                t = indices[i]
                indices[i] = indices[i+1]
                indices[i+1] = t
     not finished_flag finished_flag finished_flag = True finished_flag True for i in range(len(cp_arr)-1):
            if cp_arr[i] > cp_arr[i+1]:
                finished_flag = False

                t = cp_arr[i]
                cp_arr[i] = cp_arr[i+1]
                cp_arr[i+1] = t

                t = indices[i]
                indices[i] = indices[i+1]
                indices[i+1] = t
     i range(len(cp_arr)-1) range len(cp_arr)-1 len(cp_arr) len cp_arr 1 if cp_arr[i] > cp_arr[i+1]:
                finished_flag = False

                t = cp_arr[i]
                cp_arr[i] = cp_arr[i+1]
                cp_arr[i+1] = t

                t = indices[i]
                indices[i] = indices[i+1]
                indices[i+1] = t
     cp_arr[i] > cp_arr[i+1] cp_arr[i] cp_arr i cp_arr[i+1] cp_arr i+1 i 1 finished_flag = False finished_flag False t = cp_arr[i] t cp_arr[i] cp_arr i cp_arr[i] = cp_arr[i+1] cp_arr[i] cp_arr i cp_arr[i+1] cp_arr i+1 i 1 cp_arr[i+1] = t cp_arr[i+1] cp_arr i+1 i 1 t t = indices[i] t indices[i] indices i indices[i] = indices[i+1] indices[i] indices i indices[i+1] indices i+1 i 1 indices[i+1] = t indices[i+1] indices i+1 i 1 t return indices indices arr arr def selectionSort(arr):
    cp_arr = copy(arr)
    indices = list(range(len(cp_arr)))

    for i in range(len(cp_arr)):
        min = i
        for j in range(i+1, len(cp_arr)):
            if cp_arr[j] < cp_arr[min]:
                min = j

        t = cp_arr[i]
        cp_arr[i] = cp_arr[min]
        cp_arr[min] = t

        t = indices[i]
        indices[i] = indices[min]
        indices[min] = t

    return indices selectionSort cp_arr = copy(arr) cp_arr copy(arr) copy arr indices = list(range(len(cp_arr))) indices list(range(len(cp_arr))) list range(len(cp_arr)) range len(cp_arr) len cp_arr for i in range(len(cp_arr)):
        min = i
        for j in range(i+1, len(cp_arr)):
            if cp_arr[j] < cp_arr[min]:
                min = j

        t = cp_arr[i]
        cp_arr[i] = cp_arr[min]
        cp_arr[min] = t

        t = indices[i]
        indices[i] = indices[min]
        indices[min] = t

     i range(len(cp_arr)) range len(cp_arr) len cp_arr min = i min i for j in range(i+1, len(cp_arr)):
            if cp_arr[j] < cp_arr[min]:
                min = j

         j range(i+1, len(cp_arr)) range i+1 i 1 len(cp_arr) len cp_arr if cp_arr[j] < cp_arr[min]:
                min = j

         cp_arr[j] < cp_arr[min] cp_arr[j] cp_arr j cp_arr[min] cp_arr min min = j min j t = cp_arr[i] t cp_arr[i] cp_arr i cp_arr[i] = cp_arr[min] cp_arr[i] cp_arr i cp_arr[min] cp_arr min cp_arr[min] = t cp_arr[min] cp_arr min t t = indices[i] t indices[i] indices i indices[i] = indices[min] indices[i] indices i indices[min] indices min indices[min] = t indices[min] indices min t return indices indices arr arr if __name__ == "__main__":
    input()
    cards = input().split()
    values = list(map(lambda x: int(x[1]), cards))
    bubbleSorted_indices = bubbleSort(values)
    selectionSorted_indices = selectionSort(values)
    bubble_stable = True
    selection_stable = True
    for i in range(len(values)-1):
        if values[bubbleSorted_indices[i]] == values[bubbleSorted_indices[i+1]] and bubbleSorted_indices[i] > bubbleSorted_indices[i+1]:
            bubble_stable = False
        if values[selectionSorted_indices[i]] == values[selectionSorted_indices[i+1]] and selectionSorted_indices[i] > selectionSorted_indices[i+1]:
            selection_stable = False
    bubble_sorted = [cards[i] for i in bubbleSorted_indices]
    selection_sorted = [cards[i] for i in selectionSorted_indices]
    print(" ".join(bubble_sorted))
    print("Stable" if bubble_stable else "Not stable") 
    print(" ".join(selection_sorted))
    print("Stable" if selection_stable else "Not stable")  __name__ == "__main__" __name__ "__main__" input() input cards = input().split() cards input().split() ().split () input split values = list(map(lambda x: int(x[1]), cards)) values list(map(lambda x: int(x[1]), cards)) list map(lambda x: int(x[1]), cards) map lambda x: int(x[1]) int(x[1]) int x[1] x 1 x cards bubbleSorted_indices = bubbleSort(values) bubbleSorted_indices bubbleSort(values) bubbleSort values selectionSorted_indices = selectionSort(values) selectionSorted_indices selectionSort(values) selectionSort values bubble_stable = True bubble_stable True selection_stable = True selection_stable True for i in range(len(values)-1):
        if values[bubbleSorted_indices[i]] == values[bubbleSorted_indices[i+1]] and bubbleSorted_indices[i] > bubbleSorted_indices[i+1]:
            bubble_stable = False
        if values[selectionSorted_indices[i]] == values[selectionSorted_indices[i+1]] and selectionSorted_indices[i] > selectionSorted_indices[i+1]:
            selection_stable = False
     i range(len(values)-1) range len(values)-1 len(values) len values 1 if values[bubbleSorted_indices[i]] == values[bubbleSorted_indices[i+1]] and bubbleSorted_indices[i] > bubbleSorted_indices[i+1]:
            bubble_stable = False
         values[bubbleSorted_indices[i]] == values[bubbleSorted_indices[i+1]] and bubbleSorted_indices[i] > bubbleSorted_indices[i+1] values[bubbleSorted_indices[i]] == values[bubbleSorted_indices[i+1]] values[bubbleSorted_indices[i]] values bubbleSorted_indices[i] bubbleSorted_indices i values[bubbleSorted_indices[i+1]] values bubbleSorted_indices[i+1] bubbleSorted_indices i+1 i 1 bubbleSorted_indices[i] > bubbleSorted_indices[i+1] bubbleSorted_indices[i] bubbleSorted_indices i bubbleSorted_indices[i+1] bubbleSorted_indices i+1 i 1 bubble_stable = False bubble_stable False if values[selectionSorted_indices[i]] == values[selectionSorted_indices[i+1]] and selectionSorted_indices[i] > selectionSorted_indices[i+1]:
            selection_stable = False
     values[selectionSorted_indices[i]] == values[selectionSorted_indices[i+1]] and selectionSorted_indices[i] > selectionSorted_indices[i+1] values[selectionSorted_indices[i]] == values[selectionSorted_indices[i+1]] values[selectionSorted_indices[i]] values selectionSorted_indices[i] selectionSorted_indices i values[selectionSorted_indices[i+1]] values selectionSorted_indices[i+1] selectionSorted_indices i+1 i 1 selectionSorted_indices[i] > selectionSorted_indices[i+1] selectionSorted_indices[i] selectionSorted_indices i selectionSorted_indices[i+1] selectionSorted_indices i+1 i 1 selection_stable = False selection_stable False cards[i] for i in bubbleSorted_indices for i in bubbleSorted_indices i bubbleSorted_indices for i in bubbleSorted_indices cards[i] cards i bubble_sorted = [cards[i] for i in bubbleSorted_indices] bubble_sorted [cards[i] for i in bubbleSorted_indices] cards[i] for i in selectionSorted_indices for i in selectionSorted_indices i selectionSorted_indices for i in selectionSorted_indices cards[i] cards i selection_sorted = [cards[i] for i in selectionSorted_indices] selection_sorted [cards[i] for i in selectionSorted_indices] print(" ".join(bubble_sorted)) print " ".join(bubble_sorted) " ".join " " join bubble_sorted print("Stable" if bubble_stable else "Not stable") print "Stable" if bubble_stable else "Not stable" bubble_stable "Stable" "Not stable" print(" ".join(selection_sorted)) print " ".join(selection_sorted) " ".join " " join selection_sorted print("Stable" if selection_stable else "Not stable") print "Stable" if selection_stable else "Not stable" selection_stable "Stable" "Not stable" bubble_sorted = [cards[i] for i in bubbleSorted_indices] [cards[i] for i in bubbleSorted_indices] bubble_sorted selection_stable = False False selection_stable selection_stable = True True selection_stable values = list(map(lambda x: int(x[1]), cards)) list(map(lambda x: int(x[1]), cards)) values bubble_stable = False False bubble_stable bubbleSorted_indices = bubbleSort(values) bubbleSort(values) bubbleSorted_indices selectionSorted_indices = selectionSort(values) selectionSort(values) selectionSorted_indices def bubbleSort(arr):
    cp_arr = copy(arr)
    indices = list(range(len(cp_arr)))
    
    finished_flag = False
    while not finished_flag:
        finished_flag = True

        for i in range(len(cp_arr)-1):
            if cp_arr[i] > cp_arr[i+1]:
                finished_flag = False

                t = cp_arr[i]
                cp_arr[i] = cp_arr[i+1]
                cp_arr[i+1] = t

                t = indices[i]
                indices[i] = indices[i+1]
                indices[i+1] = t
    return indices def bubbleSort(arr):
    cp_arr = copy(arr)
    indices = list(range(len(cp_arr)))
    
    finished_flag = False
    while not finished_flag:
        finished_flag = True

        for i in range(len(cp_arr)-1):
            if cp_arr[i] > cp_arr[i+1]:
                finished_flag = False

                t = cp_arr[i]
                cp_arr[i] = cp_arr[i+1]
                cp_arr[i+1] = t

                t = indices[i]
                indices[i] = indices[i+1]
                indices[i+1] = t
    return indices bubbleSort selection_sorted = [cards[i] for i in selectionSorted_indices] [cards[i] for i in selectionSorted_indices] selection_sorted cards = input().split() input().split() cards def selectionSort(arr):
    cp_arr = copy(arr)
    indices = list(range(len(cp_arr)))

    for i in range(len(cp_arr)):
        min = i
        for j in range(i+1, len(cp_arr)):
            if cp_arr[j] < cp_arr[min]:
                min = j

        t = cp_arr[i]
        cp_arr[i] = cp_arr[min]
        cp_arr[min] = t

        t = indices[i]
        indices[i] = indices[min]
        indices[min] = t

    return indices def selectionSort(arr):
    cp_arr = copy(arr)
    indices = list(range(len(cp_arr)))

    for i in range(len(cp_arr)):
        min = i
        for j in range(i+1, len(cp_arr)):
            if cp_arr[j] < cp_arr[min]:
                min = j

        t = cp_arr[i]
        cp_arr[i] = cp_arr[min]
        cp_arr[min] = t

        t = indices[i]
        indices[i] = indices[min]
        indices[min] = t

    return indices selectionSort bubble_stable = True True bubble_stable
from copy import copy

def bubbleSort(arr):
    cp_arr = copy(arr)
    indices = list(range(len(cp_arr)))
    
    finished_flag = False
    while not finished_flag:
        finished_flag = True

        for i in range(len(cp_arr)-1):
            if cp_arr[i] > cp_arr[i+1]:
                finished_flag = False

                t = cp_arr[i]
                cp_arr[i] = cp_arr[i+1]
                cp_arr[i+1] = t

                t = indices[i]
                indices[i] = indices[i+1]
                indices[i+1] = t
    return indices

def selectionSort(arr):
    cp_arr = copy(arr)
    indices = list(range(len(cp_arr)))

    for i in range(len(cp_arr)):
        min = i
        for j in range(i+1, len(cp_arr)):
            if cp_arr[j] < cp_arr[min]:
                min = j

        t = cp_arr[i]
        cp_arr[i] = cp_arr[min]
        cp_arr[min] = t

        t = indices[i]
        indices[i] = indices[min]
        indices[min] = t

    return indices

if __name__ == "__main__":
    input()
    cards = input().split()
    values = list(map(lambda x: int(x[1]), cards))
    bubbleSorted_indices = bubbleSort(values)
    selectionSorted_indices = selectionSort(values)
    bubble_stable = True
    selection_stable = True
    for i in range(len(values)-1):
        if values[bubbleSorted_indices[i]] == values[bubbleSorted_indices[i+1]] and bubbleSorted_indices[i] > bubbleSorted_indices[i+1]:
            bubble_stable = False
        if values[selectionSorted_indices[i]] == values[selectionSorted_indices[i+1]] and selectionSorted_indices[i] > selectionSorted_indices[i+1]:
            selection_stable = False
    bubble_sorted = [cards[i] for i in bubbleSorted_indices]
    selection_sorted = [cards[i] for i in selectionSorted_indices]
    print(" ".join(bubble_sorted))
    print("Stable" if bubble_stable else "Not stable") 
    print(" ".join(selection_sorted))
    print("Stable" if selection_stable else "Not stable") 



