def selection_sort(numbers, n, key=lambda x: x):
    """selection sort method

    Args:
        numbers: a list of numbers to be sorted
        n: len(numbers)
        key: sort key

    Returns:
        sorted numberd, number of swapped times
    """
    x = []
    for data in numbers:
        x.append(data)

    counter = 0

    for i in range(0, n):
        min_j = i
        for j in range(i, n):
            if key(x[j]) < key(x[min_j]):
                min_j = j
        if min_j != i:
            x[min_j], x[i] = x[i], x[min_j]
            counter += 1
    return x, counter


def bubble_sort(numbers, n, key=lambda x: x):
    """bubble sort method

    Args:
        numbers: a list of numbers to be sorted by bubble sort
        n: len(list)
        key: sort key

    Returns:
        sorted list
    """
    x = []
    for data in numbers:
        x.append(data)

    flag = True
    counter = 0
    while flag:
        flag = False
        for index in range(n - 1, 0, -1):
            if key(x[index]) < key(x[index - 1]):
                x[index], x[index - 1] = x[index - 1], x[index]
                flag = True
                counter += 1
    return x, counter


def insertion_sort(numbers, n, key=lambda x: x):
    """insertion sort method

    Args:
        numbers: a list of numbers to be sorted
        n: len(numbers)

    Returns:
        sorted list
    """
    for i in range(1, n):
        target = numbers[i]
        index = i - 1
        while index >= 0 and target < numbers[index]:
            numbers[index + 1] = numbers[index]
            index -= 1
        numbers[index + 1] = target

    return numbers


def is_stable(list1, list2):
    """check stablity of sorting method used in list2

    Args:
        list1: sorted list(bubble sort)
        list2: sorted list(some sort)

    Returns:
        bool
    """
    flag = True
    for index, data in enumerate(list1):
        if list2[index] != data:
            flag = False
            break

    return flag


length = int(raw_input())
cards = raw_input().split()
bubble_sorted_card, bubble_swapped = bubble_sort(numbers=cards, n=length, key=lambda x: int(x[1]))
selection_sorted_card, selection_swapped = selection_sort(numbers=cards, n=length, key=lambda x: int(x[1]))
print(" ".join(bubble_sorted_card))
print("Stable")
print(" ".join(selection_sorted_card))

if is_stable(bubble_sorted_card, selection_sorted_card):
    print("Stable")
else:
    print("Not stable") def selection_sort(numbers, n, key=lambda x: x):
    """selection sort method

    Args:
        numbers: a list of numbers to be sorted
        n: len(numbers)
        key: sort key

    Returns:
        sorted numberd, number of swapped times
    """
    x = []
    for data in numbers:
        x.append(data)

    counter = 0

    for i in range(0, n):
        min_j = i
        for j in range(i, n):
            if key(x[j]) < key(x[min_j]):
                min_j = j
        if min_j != i:
            x[min_j], x[i] = x[i], x[min_j]
            counter += 1
    return x, counter selection_sort """selection sort method

    Args:
        numbers: a list of numbers to be sorted
        n: len(numbers)
        key: sort key

    Returns:
        sorted numberd, number of swapped times
    """ x = [] x [] for data in numbers:
        x.append(data)

     data numbers x.append(data) x.append x append data counter = 0 counter 0 for i in range(0, n):
        min_j = i
        for j in range(i, n):
            if key(x[j]) < key(x[min_j]):
                min_j = j
        if min_j != i:
            x[min_j], x[i] = x[i], x[min_j]
            counter += 1
     i range(0, n) range 0 n min_j = i min_j i for j in range(i, n):
            if key(x[j]) < key(x[min_j]):
                min_j = j
         j range(i, n) range i n if key(x[j]) < key(x[min_j]):
                min_j = j
         key(x[j]) < key(x[min_j]) key(x[j]) key x[j] x j key(x[min_j]) key x[min_j] x min_j min_j = j min_j j if min_j != i:
            x[min_j], x[i] = x[i], x[min_j]
            counter += 1
     min_j != i min_j i x[min_j], x[i] = x[i], x[min_j] x[min_j] x min_j x[i] x i x[i] x i x[min_j] x min_j counter += 1 counter 1 return x, counter return x, counter x counter numbers numbers n n key=lambda x: x key lambda x: x x x def bubble_sort(numbers, n, key=lambda x: x):
    """bubble sort method

    Args:
        numbers: a list of numbers to be sorted by bubble sort
        n: len(list)
        key: sort key

    Returns:
        sorted list
    """
    x = []
    for data in numbers:
        x.append(data)

    flag = True
    counter = 0
    while flag:
        flag = False
        for index in range(n - 1, 0, -1):
            if key(x[index]) < key(x[index - 1]):
                x[index], x[index - 1] = x[index - 1], x[index]
                flag = True
                counter += 1
    return x, counter bubble_sort """bubble sort method

    Args:
        numbers: a list of numbers to be sorted by bubble sort
        n: len(list)
        key: sort key

    Returns:
        sorted list
    """ x = [] x [] for data in numbers:
        x.append(data)

     data numbers x.append(data) x.append x append data flag = True flag True counter = 0 counter 0 while flag:
        flag = False
        for index in range(n - 1, 0, -1):
            if key(x[index]) < key(x[index - 1]):
                x[index], x[index - 1] = x[index - 1], x[index]
                flag = True
                counter += 1
     flag flag = False flag False for index in range(n - 1, 0, -1):
            if key(x[index]) < key(x[index - 1]):
                x[index], x[index - 1] = x[index - 1], x[index]
                flag = True
                counter += 1
     index range(n - 1, 0, -1) range n - 1 n 1 0 -1 if key(x[index]) < key(x[index - 1]):
                x[index], x[index - 1] = x[index - 1], x[index]
                flag = True
                counter += 1
     key(x[index]) < key(x[index - 1]) key(x[index]) key x[index] x index key(x[index - 1]) key x[index - 1] x index - 1 index 1 x[index], x[index - 1] = x[index - 1], x[index] x[index] x index x[index - 1] x index - 1 index 1 x[index - 1] x index - 1 index 1 x[index] x index flag = True flag True counter += 1 counter 1 return x, counter return x, counter x counter numbers numbers n n key=lambda x: x key lambda x: x x x def insertion_sort(numbers, n, key=lambda x: x):
    """insertion sort method

    Args:
        numbers: a list of numbers to be sorted
        n: len(numbers)

    Returns:
        sorted list
    """
    for i in range(1, n):
        target = numbers[i]
        index = i - 1
        while index >= 0 and target < numbers[index]:
            numbers[index + 1] = numbers[index]
            index -= 1
        numbers[index + 1] = target

    return numbers insertion_sort """insertion sort method

    Args:
        numbers: a list of numbers to be sorted
        n: len(numbers)

    Returns:
        sorted list
    """ for i in range(1, n):
        target = numbers[i]
        index = i - 1
        while index >= 0 and target < numbers[index]:
            numbers[index + 1] = numbers[index]
            index -= 1
        numbers[index + 1] = target

     i range(1, n) range 1 n target = numbers[i] target numbers[i] numbers i index = i - 1 index i - 1 i 1 while index >= 0 and target < numbers[index]:
            numbers[index + 1] = numbers[index]
            index -= 1
         index >= 0 and target < numbers[index] index >= 0 index 0 target < numbers[index] target numbers[index] numbers index numbers[index + 1] = numbers[index] numbers[index + 1] numbers index + 1 index 1 numbers[index] numbers index index -= 1 index 1 numbers[index + 1] = target numbers[index + 1] numbers index + 1 index 1 target return numbers numbers numbers numbers n n key=lambda x: x key lambda x: x x x def is_stable(list1, list2):
    """check stablity of sorting method used in list2

    Args:
        list1: sorted list(bubble sort)
        list2: sorted list(some sort)

    Returns:
        bool
    """
    flag = True
    for index, data in enumerate(list1):
        if list2[index] != data:
            flag = False
            break

    return flag is_stable """check stablity of sorting method used in list2

    Args:
        list1: sorted list(bubble sort)
        list2: sorted list(some sort)

    Returns:
        bool
    """ flag = True flag True for index, data in enumerate(list1):
        if list2[index] != data:
            flag = False
            break

     index data enumerate(list1) enumerate list1 if list2[index] != data:
            flag = False
            break

     list2[index] != data list2[index] list2 index data flag = False flag False break return flag flag list1 list1 list2 list2 length = int(raw_input()) length int(raw_input()) int raw_input() raw_input cards = raw_input().split() cards raw_input().split() ().split () raw_input split bubble_sorted_card, bubble_swapped = bubble_sort(numbers=cards, n=length, key=lambda x: int(x[1])) bubble_sorted_card bubble_sort(numbers=cards, n=length, key=lambda x: int(x[1])) bubble_sort numbers=cards numbers cards n=length n length key=lambda x: int(x[1]) key lambda x: int(x[1]) int(x[1]) int x[1] x 1 x bubble_swapped selection_sorted_card, selection_swapped = selection_sort(numbers=cards, n=length, key=lambda x: int(x[1])) selection_sorted_card selection_sort(numbers=cards, n=length, key=lambda x: int(x[1])) selection_sort numbers=cards numbers cards n=length n length key=lambda x: int(x[1]) key lambda x: int(x[1]) int(x[1]) int x[1] x 1 x selection_swapped print(" ".join(bubble_sorted_card)) print " ".join(bubble_sorted_card) " ".join " " join bubble_sorted_card print("Stable") print "Stable" print(" ".join(selection_sorted_card)) print " ".join(selection_sorted_card) " ".join " " join selection_sorted_card if is_stable(bubble_sorted_card, selection_sorted_card):
    print("Stable")
else:
    print("Not stable") is_stable(bubble_sorted_card, selection_sorted_card) is_stable bubble_sorted_card selection_sorted_card print("Stable") print "Stable" print("Not stable") print "Not stable" selection_sorted_card, selection_swapped = selection_sort(numbers=cards, n=length, key=lambda x: int(x[1])) selection_sort(numbers=cards, n=length, key=lambda x: int(x[1])) selection_sorted_card cards = raw_input().split() raw_input().split() cards selection_swapped = selection_sort(numbers=cards, n=length, key=lambda x: int(x[1])) selection_sort(numbers=cards, n=length, key=lambda x: int(x[1])) selection_swapped def bubble_sort(numbers, n, key=lambda x: x):
    """bubble sort method

    Args:
        numbers: a list of numbers to be sorted by bubble sort
        n: len(list)
        key: sort key

    Returns:
        sorted list
    """
    x = []
    for data in numbers:
        x.append(data)

    flag = True
    counter = 0
    while flag:
        flag = False
        for index in range(n - 1, 0, -1):
            if key(x[index]) < key(x[index - 1]):
                x[index], x[index - 1] = x[index - 1], x[index]
                flag = True
                counter += 1
    return x, counter def bubble_sort(numbers, n, key=lambda x: x):
    """bubble sort method

    Args:
        numbers: a list of numbers to be sorted by bubble sort
        n: len(list)
        key: sort key

    Returns:
        sorted list
    """
    x = []
    for data in numbers:
        x.append(data)

    flag = True
    counter = 0
    while flag:
        flag = False
        for index in range(n - 1, 0, -1):
            if key(x[index]) < key(x[index - 1]):
                x[index], x[index - 1] = x[index - 1], x[index]
                flag = True
                counter += 1
    return x, counter bubble_sort def is_stable(list1, list2):
    """check stablity of sorting method used in list2

    Args:
        list1: sorted list(bubble sort)
        list2: sorted list(some sort)

    Returns:
        bool
    """
    flag = True
    for index, data in enumerate(list1):
        if list2[index] != data:
            flag = False
            break

    return flag def is_stable(list1, list2):
    """check stablity of sorting method used in list2

    Args:
        list1: sorted list(bubble sort)
        list2: sorted list(some sort)

    Returns:
        bool
    """
    flag = True
    for index, data in enumerate(list1):
        if list2[index] != data:
            flag = False
            break

    return flag is_stable def insertion_sort(numbers, n, key=lambda x: x):
    """insertion sort method

    Args:
        numbers: a list of numbers to be sorted
        n: len(numbers)

    Returns:
        sorted list
    """
    for i in range(1, n):
        target = numbers[i]
        index = i - 1
        while index >= 0 and target < numbers[index]:
            numbers[index + 1] = numbers[index]
            index -= 1
        numbers[index + 1] = target

    return numbers def insertion_sort(numbers, n, key=lambda x: x):
    """insertion sort method

    Args:
        numbers: a list of numbers to be sorted
        n: len(numbers)

    Returns:
        sorted list
    """
    for i in range(1, n):
        target = numbers[i]
        index = i - 1
        while index >= 0 and target < numbers[index]:
            numbers[index + 1] = numbers[index]
            index -= 1
        numbers[index + 1] = target

    return numbers insertion_sort length = int(raw_input()) int(raw_input()) length def selection_sort(numbers, n, key=lambda x: x):
    """selection sort method

    Args:
        numbers: a list of numbers to be sorted
        n: len(numbers)
        key: sort key

    Returns:
        sorted numberd, number of swapped times
    """
    x = []
    for data in numbers:
        x.append(data)

    counter = 0

    for i in range(0, n):
        min_j = i
        for j in range(i, n):
            if key(x[j]) < key(x[min_j]):
                min_j = j
        if min_j != i:
            x[min_j], x[i] = x[i], x[min_j]
            counter += 1
    return x, counter def selection_sort(numbers, n, key=lambda x: x):
    """selection sort method

    Args:
        numbers: a list of numbers to be sorted
        n: len(numbers)
        key: sort key

    Returns:
        sorted numberd, number of swapped times
    """
    x = []
    for data in numbers:
        x.append(data)

    counter = 0

    for i in range(0, n):
        min_j = i
        for j in range(i, n):
            if key(x[j]) < key(x[min_j]):
                min_j = j
        if min_j != i:
            x[min_j], x[i] = x[i], x[min_j]
            counter += 1
    return x, counter selection_sort bubble_sorted_card, bubble_swapped = bubble_sort(numbers=cards, n=length, key=lambda x: int(x[1])) bubble_sort(numbers=cards, n=length, key=lambda x: int(x[1])) bubble_sorted_card bubble_swapped = bubble_sort(numbers=cards, n=length, key=lambda x: int(x[1])) bubble_sort(numbers=cards, n=length, key=lambda x: int(x[1])) bubble_swapped
def selection_sort(numbers, n, key=lambda x: x):
    """selection sort method

    Args:
        numbers: a list of numbers to be sorted
        n: len(numbers)
        key: sort key

    Returns:
        sorted numberd, number of swapped times
    """
    x = []
    for data in numbers:
        x.append(data)

    counter = 0

    for i in range(0, n):
        min_j = i
        for j in range(i, n):
            if key(x[j]) < key(x[min_j]):
                min_j = j
        if min_j != i:
            x[min_j], x[i] = x[i], x[min_j]
            counter += 1
    return x, counter


def bubble_sort(numbers, n, key=lambda x: x):
    """bubble sort method

    Args:
        numbers: a list of numbers to be sorted by bubble sort
        n: len(list)
        key: sort key

    Returns:
        sorted list
    """
    x = []
    for data in numbers:
        x.append(data)

    flag = True
    counter = 0
    while flag:
        flag = False
        for index in range(n - 1, 0, -1):
            if key(x[index]) < key(x[index - 1]):
                x[index], x[index - 1] = x[index - 1], x[index]
                flag = True
                counter += 1
    return x, counter


def insertion_sort(numbers, n, key=lambda x: x):
    """insertion sort method

    Args:
        numbers: a list of numbers to be sorted
        n: len(numbers)

    Returns:
        sorted list
    """
    for i in range(1, n):
        target = numbers[i]
        index = i - 1
        while index >= 0 and target < numbers[index]:
            numbers[index + 1] = numbers[index]
            index -= 1
        numbers[index + 1] = target

    return numbers


def is_stable(list1, list2):
    """check stablity of sorting method used in list2

    Args:
        list1: sorted list(bubble sort)
        list2: sorted list(some sort)

    Returns:
        bool
    """
    flag = True
    for index, data in enumerate(list1):
        if list2[index] != data:
            flag = False
            break

    return flag


length = int(raw_input())
cards = raw_input().split()
bubble_sorted_card, bubble_swapped = bubble_sort(numbers=cards, n=length, key=lambda x: int(x[1]))
selection_sorted_card, selection_swapped = selection_sort(numbers=cards, n=length, key=lambda x: int(x[1]))
print(" ".join(bubble_sorted_card))
print("Stable")
print(" ".join(selection_sorted_card))

if is_stable(bubble_sorted_card, selection_sorted_card):
    print("Stable")
else:
    print("Not stable")