import sys

class Card:
    def __init__(self, kind, num):
        self.kind = kind
        self.num = num
    def __eq__(self, other):
        return (self.kind == other.kind) and (self.num == other.num)
    def __ne__(self, other):
        return not ((self.kind == other.kind) and (self.num == other.num))
    def __str__(self):
        return self.kind + self.num

def list_to_string(array):
    s = ""
    for n in array:
        s += str(n) + " "
    return s.strip()
    
def swap(array, i, j):
    tmp = array[i]
    array[i] = array[j]
    array[j] = tmp

def is_stable(array1, array2):
    if reduce(lambda x, y: x and y, map(lambda x: x[0] == x[1], zip(array1, array2))):
        return "Stable"
    else:
        return "Not stable"

def bubble_sort(array):
    for i in range(0, len(array)):
        for j in reversed(range(i+1, len(array))):
            if array[j].num < array[j-1].num:
                swap(array, j, j-1)
    return array
        
def selection_sort(array):
    for i in range(0, len(array)):
        min_index = i
        for j in range(i, len(array)):
            if array[min_index].num > array[j].num:
                min_index = j
        swap(array, i, min_index)
    return array

def main():
    num = int(sys.stdin.readline().strip())
    array = map(lambda x: Card(x[0], x[1]), sys.stdin.readline().strip().split(" "))

    a = list(array)
    b = list(array)

    print list_to_string(bubble_sort(a))
    print "Stable"
    print list_to_string(selection_sort(b))
    print is_stable(a, b)

if __name__ == "__main__":
    main() import sys sys class Card:
    def __init__(self, kind, num):
        self.kind = kind
        self.num = num
    def __eq__(self, other):
        return (self.kind == other.kind) and (self.num == other.num)
    def __ne__(self, other):
        return not ((self.kind == other.kind) and (self.num == other.num))
    def __str__(self):
        return self.kind + self.num Card def __init__(self, kind, num):
        self.kind = kind
        self.num = num
     __init__ self.kind = kind self.kind self kind kind self.num = num self.num self num num self self kind kind num num def __eq__(self, other):
        return (self.kind == other.kind) and (self.num == other.num)
     __eq__ return (self.kind == other.kind) and (self.num == other.num) (self.kind == other.kind) and (self.num == other.num) self.kind == other.kind self.kind self kind other.kind other kind self.num == other.num self.num self num other.num other num self self other other def __ne__(self, other):
        return not ((self.kind == other.kind) and (self.num == other.num))
     __ne__ return not ((self.kind == other.kind) and (self.num == other.num)) not ((self.kind == other.kind) and (self.num == other.num)) (self.kind == other.kind) and (self.num == other.num) self.kind == other.kind self.kind self kind other.kind other kind self.num == other.num self.num self num other.num other num self self other other def __str__(self):
        return self.kind + self.num __str__ return self.kind + self.num self.kind + self.num self.kind self kind self.num self num self self def list_to_string(array):
    s = ""
    for n in array:
        s += str(n) + " "
    return s.strip()
     list_to_string s = "" s "" for n in array:
        s += str(n) + " "
     n array s += str(n) + " " s str(n) + " " str(n) str n " " return s.strip() s.strip() s.strip s strip array array def swap(array, i, j):
    tmp = array[i]
    array[i] = array[j]
    array[j] = tmp swap tmp = array[i] tmp array[i] array i array[i] = array[j] array[i] array i array[j] array j array[j] = tmp array[j] array j tmp array array i i j j def is_stable(array1, array2):
    if reduce(lambda x, y: x and y, map(lambda x: x[0] == x[1], zip(array1, array2))):
        return "Stable"
    else:
        return "Not stable" is_stable if reduce(lambda x, y: x and y, map(lambda x: x[0] == x[1], zip(array1, array2))):
        return "Stable"
    else:
        return "Not stable" reduce(lambda x, y: x and y, map(lambda x: x[0] == x[1], zip(array1, array2))) reduce lambda x, y: x and y x and y x y x y map(lambda x: x[0] == x[1], zip(array1, array2)) map lambda x: x[0] == x[1] x[0] == x[1] x[0] x 0 x[1] x 1 x zip(array1, array2) zip array1 array2 return "Stable" "Stable" return "Not stable" "Not stable" array1 array1 array2 array2 def bubble_sort(array):
    for i in range(0, len(array)):
        for j in reversed(range(i+1, len(array))):
            if array[j].num < array[j-1].num:
                swap(array, j, j-1)
    return array
         bubble_sort for i in range(0, len(array)):
        for j in reversed(range(i+1, len(array))):
            if array[j].num < array[j-1].num:
                swap(array, j, j-1)
     i range(0, len(array)) range 0 len(array) len array for j in reversed(range(i+1, len(array))):
            if array[j].num < array[j-1].num:
                swap(array, j, j-1)
     j reversed(range(i+1, len(array))) reversed range(i+1, len(array)) range i+1 i 1 len(array) len array if array[j].num < array[j-1].num:
                swap(array, j, j-1)
     array[j].num < array[j-1].num array[j].num [j] array j num array[j-1].num [j-1] array j-1 j 1 num swap(array, j, j-1) swap array j j-1 j 1 return array array array array def selection_sort(array):
    for i in range(0, len(array)):
        min_index = i
        for j in range(i, len(array)):
            if array[min_index].num > array[j].num:
                min_index = j
        swap(array, i, min_index)
    return array selection_sort for i in range(0, len(array)):
        min_index = i
        for j in range(i, len(array)):
            if array[min_index].num > array[j].num:
                min_index = j
        swap(array, i, min_index)
     i range(0, len(array)) range 0 len(array) len array min_index = i min_index i for j in range(i, len(array)):
            if array[min_index].num > array[j].num:
                min_index = j
         j range(i, len(array)) range i len(array) len array if array[min_index].num > array[j].num:
                min_index = j
         array[min_index].num > array[j].num array[min_index].num [min_index] array min_index num array[j].num [j] array j num min_index = j min_index j swap(array, i, min_index) swap array i min_index return array array array array def main():
    num = int(sys.stdin.readline().strip())
    array = map(lambda x: Card(x[0], x[1]), sys.stdin.readline().strip().split(" "))

    a = list(array)
    b = list(array)

    print list_to_string(bubble_sort(a))
    print "Stable"
    print list_to_string(selection_sort(b))
    print is_stable(a, b) main num = int(sys.stdin.readline().strip()) num int(sys.stdin.readline().strip()) int sys.stdin.readline().strip() sys.stdin.readline().strip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline strip array = map(lambda x: Card(x[0], x[1]), sys.stdin.readline().strip().split(" ")) array map(lambda x: Card(x[0], x[1]), sys.stdin.readline().strip().split(" ")) map lambda x: Card(x[0], x[1]) Card(x[0], x[1]) Card x[0] x 0 x[1] x 1 x sys.stdin.readline().strip().split(" ") sys.stdin.readline().strip().split sys.stdin.readline().strip() sys.stdin.readline().strip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline strip split " " a = list(array) a list(array) list array b = list(array) b list(array) list array print list_to_string(bubble_sort(a)) list_to_string bubble_sort(a) bubble_sort a print print list_to_string(selection_sort(b)) list_to_string selection_sort(b) selection_sort b print is_stable(a, b) is_stable a b if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main def list_to_string(array):
    s = ""
    for n in array:
        s += str(n) + " "
    return s.strip()
     def list_to_string(array):
    s = ""
    for n in array:
        s += str(n) + " "
    return s.strip()
     list_to_string def main():
    num = int(sys.stdin.readline().strip())
    array = map(lambda x: Card(x[0], x[1]), sys.stdin.readline().strip().split(" "))

    a = list(array)
    b = list(array)

    print list_to_string(bubble_sort(a))
    print "Stable"
    print list_to_string(selection_sort(b))
    print is_stable(a, b) def main():
    num = int(sys.stdin.readline().strip())
    array = map(lambda x: Card(x[0], x[1]), sys.stdin.readline().strip().split(" "))

    a = list(array)
    b = list(array)

    print list_to_string(bubble_sort(a))
    print "Stable"
    print list_to_string(selection_sort(b))
    print is_stable(a, b) main class Card:
    def __init__(self, kind, num):
        self.kind = kind
        self.num = num
    def __eq__(self, other):
        return (self.kind == other.kind) and (self.num == other.num)
    def __ne__(self, other):
        return not ((self.kind == other.kind) and (self.num == other.num))
    def __str__(self):
        return self.kind + self.num class Card:
    def __init__(self, kind, num):
        self.kind = kind
        self.num = num
    def __eq__(self, other):
        return (self.kind == other.kind) and (self.num == other.num)
    def __ne__(self, other):
        return not ((self.kind == other.kind) and (self.num == other.num))
    def __str__(self):
        return self.kind + self.num Card def is_stable(array1, array2):
    if reduce(lambda x, y: x and y, map(lambda x: x[0] == x[1], zip(array1, array2))):
        return "Stable"
    else:
        return "Not stable" def is_stable(array1, array2):
    if reduce(lambda x, y: x and y, map(lambda x: x[0] == x[1], zip(array1, array2))):
        return "Stable"
    else:
        return "Not stable" is_stable def bubble_sort(array):
    for i in range(0, len(array)):
        for j in reversed(range(i+1, len(array))):
            if array[j].num < array[j-1].num:
                swap(array, j, j-1)
    return array
         def bubble_sort(array):
    for i in range(0, len(array)):
        for j in reversed(range(i+1, len(array))):
            if array[j].num < array[j-1].num:
                swap(array, j, j-1)
    return array
         bubble_sort def selection_sort(array):
    for i in range(0, len(array)):
        min_index = i
        for j in range(i, len(array)):
            if array[min_index].num > array[j].num:
                min_index = j
        swap(array, i, min_index)
    return array def selection_sort(array):
    for i in range(0, len(array)):
        min_index = i
        for j in range(i, len(array)):
            if array[min_index].num > array[j].num:
                min_index = j
        swap(array, i, min_index)
    return array selection_sort def swap(array, i, j):
    tmp = array[i]
    array[i] = array[j]
    array[j] = tmp def swap(array, i, j):
    tmp = array[i]
    array[i] = array[j]
    array[j] = tmp swap
import sys

class Card:
    def __init__(self, kind, num):
        self.kind = kind
        self.num = num
    def __eq__(self, other):
        return (self.kind == other.kind) and (self.num == other.num)
    def __ne__(self, other):
        return not ((self.kind == other.kind) and (self.num == other.num))
    def __str__(self):
        return self.kind + self.num

def list_to_string(array):
    s = ""
    for n in array:
        s += str(n) + " "
    return s.strip()
    
def swap(array, i, j):
    tmp = array[i]
    array[i] = array[j]
    array[j] = tmp

def is_stable(array1, array2):
    if reduce(lambda x, y: x and y, map(lambda x: x[0] == x[1], zip(array1, array2))):
        return "Stable"
    else:
        return "Not stable"

def bubble_sort(array):
    for i in range(0, len(array)):
        for j in reversed(range(i+1, len(array))):
            if array[j].num < array[j-1].num:
                swap(array, j, j-1)
    return array
        
def selection_sort(array):
    for i in range(0, len(array)):
        min_index = i
        for j in range(i, len(array)):
            if array[min_index].num > array[j].num:
                min_index = j
        swap(array, i, min_index)
    return array

def main():
    num = int(sys.stdin.readline().strip())
    array = map(lambda x: Card(x[0], x[1]), sys.stdin.readline().strip().split(" "))

    a = list(array)
    b = list(array)

    print list_to_string(bubble_sort(a))
    print "Stable"
    print list_to_string(selection_sort(b))
    print is_stable(a, b)

if __name__ == "__main__":
    main()