import sys

ERROR_INPUT = 'input is invalid'


def main():
    n = get_length()
    arr = get_array(length=n)

    bubbleLi = bubbleSort(li=arr.copy(), length=n)
    selectionLi = selectionSort(li=arr.copy(), length=n)

    print(*list(map(lambda c: c.card, bubbleLi)))
    print(checkStable(inp=arr, out=bubbleLi))
    print(*list(map(lambda c: c.card, selectionLi)))
    print(checkStable(inp=arr, out=selectionLi))
    return 0


def get_length():
    n = int(input())
    if n < 1 or n > 36:
        print(ERROR_INPUT)
        sys.exit(1)
    else:
        return n


def get_array(length):
    nums = input().split(' ')
    return [Card(n) for n in nums]


def bubbleSort(li, length):
    for n in range(length):
        for n in range(1, length - n):
            if li[n].num < li[n - 1].num:
                li[n], li[n - 1] = li[n - 1], li[n]

    return li


def selectionSort(li, length):
    for i in range(0, length - 1):
        min_index = i
        for j in range(i, length):
            if li[j].num < li[min_index].num:
                min_index = j

        if i != min_index:
            li[i], li[min_index] = li[min_index], li[i]

    return li


def checkStable(inp, out):
    small_num = out[0].num

    for n in range(1, len(out)):
        if out[n].num == small_num:
            if inp.index(out[n]) < inp.index(out[n - 1]):
                return 'Not stable'
        elif small_num < out[n].num:
            small_num = out[n].num
            continue

    return 'Stable'


class Card:
    def __init__(self, disp):
        li = list(disp)
        self.card = disp
        self.chara = li[0]
        self.num = int(li[1])
        return


main() import sys sys ERROR_INPUT = 'input is invalid' ERROR_INPUT 'input is invalid' def main():
    n = get_length()
    arr = get_array(length=n)

    bubbleLi = bubbleSort(li=arr.copy(), length=n)
    selectionLi = selectionSort(li=arr.copy(), length=n)

    print(*list(map(lambda c: c.card, bubbleLi)))
    print(checkStable(inp=arr, out=bubbleLi))
    print(*list(map(lambda c: c.card, selectionLi)))
    print(checkStable(inp=arr, out=selectionLi))
    return 0 main n = get_length() n get_length() get_length arr = get_array(length=n) arr get_array(length=n) get_array length=n length n bubbleLi = bubbleSort(li=arr.copy(), length=n) bubbleLi bubbleSort(li=arr.copy(), length=n) bubbleSort li=arr.copy() li arr.copy() arr.copy arr copy length=n length n selectionLi = selectionSort(li=arr.copy(), length=n) selectionLi selectionSort(li=arr.copy(), length=n) selectionSort li=arr.copy() li arr.copy() arr.copy arr copy length=n length n print(*list(map(lambda c: c.card, bubbleLi))) print *list(map(lambda c: c.card, bubbleLi)) list(map(lambda c: c.card, bubbleLi)) list map(lambda c: c.card, bubbleLi) map lambda c: c.card c.card c card c bubbleLi print(checkStable(inp=arr, out=bubbleLi)) print checkStable(inp=arr, out=bubbleLi) checkStable inp=arr inp arr out=bubbleLi out bubbleLi print(*list(map(lambda c: c.card, selectionLi))) print *list(map(lambda c: c.card, selectionLi)) list(map(lambda c: c.card, selectionLi)) list map(lambda c: c.card, selectionLi) map lambda c: c.card c.card c card c selectionLi print(checkStable(inp=arr, out=selectionLi)) print checkStable(inp=arr, out=selectionLi) checkStable inp=arr inp arr out=selectionLi out selectionLi return 0 0 def get_length():
    n = int(input())
    if n < 1 or n > 36:
        print(ERROR_INPUT)
        sys.exit(1)
    else:
        return n get_length n = int(input()) n int(input()) int input() input if n < 1 or n > 36:
        print(ERROR_INPUT)
        sys.exit(1)
    else:
        return n n < 1 or n > 36 n < 1 n 1 n > 36 n 36 print(ERROR_INPUT) print ERROR_INPUT sys.exit(1) sys.exit sys exit 1 return n n def get_array(length):
    nums = input().split(' ')
    return [Card(n) for n in nums] get_array nums = input().split(' ') nums input().split(' ') ().split () input split ' ' Card(n) for n in nums for n in nums n nums for n in nums Card(n) Card n return [Card(n) for n in nums] [Card(n) for n in nums] length length def bubbleSort(li, length):
    for n in range(length):
        for n in range(1, length - n):
            if li[n].num < li[n - 1].num:
                li[n], li[n - 1] = li[n - 1], li[n]

    return li bubbleSort for n in range(length):
        for n in range(1, length - n):
            if li[n].num < li[n - 1].num:
                li[n], li[n - 1] = li[n - 1], li[n]

     n range(length) range length for n in range(1, length - n):
            if li[n].num < li[n - 1].num:
                li[n], li[n - 1] = li[n - 1], li[n]

     n range(1, length - n) range 1 length - n length n if li[n].num < li[n - 1].num:
                li[n], li[n - 1] = li[n - 1], li[n]

     li[n].num < li[n - 1].num li[n].num [n] li n num li[n - 1].num [n - 1] li n - 1 n 1 num li[n], li[n - 1] = li[n - 1], li[n] li[n] li n li[n - 1] li n - 1 n 1 li[n - 1] li n - 1 n 1 li[n] li n return li li li li length length def selectionSort(li, length):
    for i in range(0, length - 1):
        min_index = i
        for j in range(i, length):
            if li[j].num < li[min_index].num:
                min_index = j

        if i != min_index:
            li[i], li[min_index] = li[min_index], li[i]

    return li selectionSort for i in range(0, length - 1):
        min_index = i
        for j in range(i, length):
            if li[j].num < li[min_index].num:
                min_index = j

        if i != min_index:
            li[i], li[min_index] = li[min_index], li[i]

     i range(0, length - 1) range 0 length - 1 length 1 min_index = i min_index i for j in range(i, length):
            if li[j].num < li[min_index].num:
                min_index = j

         j range(i, length) range i length if li[j].num < li[min_index].num:
                min_index = j

         li[j].num < li[min_index].num li[j].num [j] li j num li[min_index].num [min_index] li min_index num min_index = j min_index j if i != min_index:
            li[i], li[min_index] = li[min_index], li[i]

     i != min_index i min_index li[i], li[min_index] = li[min_index], li[i] li[i] li i li[min_index] li min_index li[min_index] li min_index li[i] li i return li li li li length length def checkStable(inp, out):
    small_num = out[0].num

    for n in range(1, len(out)):
        if out[n].num == small_num:
            if inp.index(out[n]) < inp.index(out[n - 1]):
                return 'Not stable'
        elif small_num < out[n].num:
            small_num = out[n].num
            continue

    return 'Stable' checkStable small_num = out[0].num small_num out[0].num [0] out 0 num for n in range(1, len(out)):
        if out[n].num == small_num:
            if inp.index(out[n]) < inp.index(out[n - 1]):
                return 'Not stable'
        elif small_num < out[n].num:
            small_num = out[n].num
            continue

     n range(1, len(out)) range 1 len(out) len out if out[n].num == small_num:
            if inp.index(out[n]) < inp.index(out[n - 1]):
                return 'Not stable'
        elif small_num < out[n].num:
            small_num = out[n].num
            continue

     out[n].num == small_num out[n].num [n] out n num small_num if inp.index(out[n]) < inp.index(out[n - 1]):
                return 'Not stable'
         inp.index(out[n]) < inp.index(out[n - 1]) inp.index(out[n]) inp.index inp index out[n] out n inp.index(out[n - 1]) inp.index inp index out[n - 1] out n - 1 n 1 return 'Not stable' 'Not stable' elif small_num < out[n].num:
            small_num = out[n].num
            continue

     small_num < out[n].num small_num out[n].num [n] out n num small_num = out[n].num small_num out[n].num [n] out n num continue return 'Stable' 'Stable' inp inp out out class Card:
    def __init__(self, disp):
        li = list(disp)
        self.card = disp
        self.chara = li[0]
        self.num = int(li[1])
        return Card def __init__(self, disp):
        li = list(disp)
        self.card = disp
        self.chara = li[0]
        self.num = int(li[1])
        return __init__ li = list(disp) li list(disp) list disp self.card = disp self.card self card disp self.chara = li[0] self.chara self chara li[0] li 0 self.num = int(li[1]) self.num self num int(li[1]) int li[1] li 1 return self self disp disp main() main class Card:
    def __init__(self, disp):
        li = list(disp)
        self.card = disp
        self.chara = li[0]
        self.num = int(li[1])
        return class Card:
    def __init__(self, disp):
        li = list(disp)
        self.card = disp
        self.chara = li[0]
        self.num = int(li[1])
        return Card def checkStable(inp, out):
    small_num = out[0].num

    for n in range(1, len(out)):
        if out[n].num == small_num:
            if inp.index(out[n]) < inp.index(out[n - 1]):
                return 'Not stable'
        elif small_num < out[n].num:
            small_num = out[n].num
            continue

    return 'Stable' def checkStable(inp, out):
    small_num = out[0].num

    for n in range(1, len(out)):
        if out[n].num == small_num:
            if inp.index(out[n]) < inp.index(out[n - 1]):
                return 'Not stable'
        elif small_num < out[n].num:
            small_num = out[n].num
            continue

    return 'Stable' checkStable def get_length():
    n = int(input())
    if n < 1 or n > 36:
        print(ERROR_INPUT)
        sys.exit(1)
    else:
        return n def get_length():
    n = int(input())
    if n < 1 or n > 36:
        print(ERROR_INPUT)
        sys.exit(1)
    else:
        return n get_length ERROR_INPUT = 'input is invalid' 'input is invalid' ERROR_INPUT def main():
    n = get_length()
    arr = get_array(length=n)

    bubbleLi = bubbleSort(li=arr.copy(), length=n)
    selectionLi = selectionSort(li=arr.copy(), length=n)

    print(*list(map(lambda c: c.card, bubbleLi)))
    print(checkStable(inp=arr, out=bubbleLi))
    print(*list(map(lambda c: c.card, selectionLi)))
    print(checkStable(inp=arr, out=selectionLi))
    return 0 def main():
    n = get_length()
    arr = get_array(length=n)

    bubbleLi = bubbleSort(li=arr.copy(), length=n)
    selectionLi = selectionSort(li=arr.copy(), length=n)

    print(*list(map(lambda c: c.card, bubbleLi)))
    print(checkStable(inp=arr, out=bubbleLi))
    print(*list(map(lambda c: c.card, selectionLi)))
    print(checkStable(inp=arr, out=selectionLi))
    return 0 main def get_array(length):
    nums = input().split(' ')
    return [Card(n) for n in nums] def get_array(length):
    nums = input().split(' ')
    return [Card(n) for n in nums] get_array def selectionSort(li, length):
    for i in range(0, length - 1):
        min_index = i
        for j in range(i, length):
            if li[j].num < li[min_index].num:
                min_index = j

        if i != min_index:
            li[i], li[min_index] = li[min_index], li[i]

    return li def selectionSort(li, length):
    for i in range(0, length - 1):
        min_index = i
        for j in range(i, length):
            if li[j].num < li[min_index].num:
                min_index = j

        if i != min_index:
            li[i], li[min_index] = li[min_index], li[i]

    return li selectionSort def bubbleSort(li, length):
    for n in range(length):
        for n in range(1, length - n):
            if li[n].num < li[n - 1].num:
                li[n], li[n - 1] = li[n - 1], li[n]

    return li def bubbleSort(li, length):
    for n in range(length):
        for n in range(1, length - n):
            if li[n].num < li[n - 1].num:
                li[n], li[n - 1] = li[n - 1], li[n]

    return li bubbleSort
import sys

ERROR_INPUT = 'input is invalid'


def main():
    n = get_length()
    arr = get_array(length=n)

    bubbleLi = bubbleSort(li=arr.copy(), length=n)
    selectionLi = selectionSort(li=arr.copy(), length=n)

    print(*list(map(lambda c: c.card, bubbleLi)))
    print(checkStable(inp=arr, out=bubbleLi))
    print(*list(map(lambda c: c.card, selectionLi)))
    print(checkStable(inp=arr, out=selectionLi))
    return 0


def get_length():
    n = int(input())
    if n < 1 or n > 36:
        print(ERROR_INPUT)
        sys.exit(1)
    else:
        return n


def get_array(length):
    nums = input().split(' ')
    return [Card(n) for n in nums]


def bubbleSort(li, length):
    for n in range(length):
        for n in range(1, length - n):
            if li[n].num < li[n - 1].num:
                li[n], li[n - 1] = li[n - 1], li[n]

    return li


def selectionSort(li, length):
    for i in range(0, length - 1):
        min_index = i
        for j in range(i, length):
            if li[j].num < li[min_index].num:
                min_index = j

        if i != min_index:
            li[i], li[min_index] = li[min_index], li[i]

    return li


def checkStable(inp, out):
    small_num = out[0].num

    for n in range(1, len(out)):
        if out[n].num == small_num:
            if inp.index(out[n]) < inp.index(out[n - 1]):
                return 'Not stable'
        elif small_num < out[n].num:
            small_num = out[n].num
            continue

    return 'Stable'


class Card:
    def __init__(self, disp):
        li = list(disp)
        self.card = disp
        self.chara = li[0]
        self.num = int(li[1])
        return


main()