class Trump(str):
    def __init__(self, card):
        super().__init__()
        self.num = int(card[1])

def trump_bubble_sort(A, N):
    flg = True
    while flg:
        flg = False
        for i in range(1, N):
            if A[i - 1].num > A[i].num:
                A[i], A[i - 1] = A[i - 1], A[i]
                flg = True
    return A


def trump_selection_sort(A, N):
    for i in range(0, N-1):
        min_idx = i
        for j in range(i+1, N):
            if A[min_idx].num > A[j].num:
                min_idx = j
        A[i], A[min_idx] = A[min_idx], A[i]
        
    return A

N = int(input().rstrip())
A = [Trump(a) for a in input().rstrip().split()]
A_2 = A.copy()

bubble_result = trump_bubble_sort(A, N)
selection_result = trump_selection_sort(A_2, N)

stable = True
for i in range(0, N):
    if bubble_result[i] != selection_result[i]:
        stable = False
        break
print(" ".join(bubble_result))
print("Stable")
print(" ".join(selection_result))
print("Stable" if stable else "Not stable")
    
 class Trump(str):
    def __init__(self, card):
        super().__init__()
        self.num = int(card[1]) Trump def __init__(self, card):
        super().__init__()
        self.num = int(card[1]) __init__ super().__init__() ().__init__ () super __init__ self.num = int(card[1]) self.num self num int(card[1]) int card[1] card 1 self self card card str def trump_bubble_sort(A, N):
    flg = True
    while flg:
        flg = False
        for i in range(1, N):
            if A[i - 1].num > A[i].num:
                A[i], A[i - 1] = A[i - 1], A[i]
                flg = True
    return A trump_bubble_sort flg = True flg True while flg:
        flg = False
        for i in range(1, N):
            if A[i - 1].num > A[i].num:
                A[i], A[i - 1] = A[i - 1], A[i]
                flg = True
     flg flg = False flg False for i in range(1, N):
            if A[i - 1].num > A[i].num:
                A[i], A[i - 1] = A[i - 1], A[i]
                flg = True
     i range(1, N) range 1 N if A[i - 1].num > A[i].num:
                A[i], A[i - 1] = A[i - 1], A[i]
                flg = True
     A[i - 1].num > A[i].num A[i - 1].num [i - 1] A i - 1 i 1 num A[i].num [i] A i num A[i], A[i - 1] = A[i - 1], A[i] A[i] A i A[i - 1] A i - 1 i 1 A[i - 1] A i - 1 i 1 A[i] A i flg = True flg True return A A A A N N def trump_selection_sort(A, N):
    for i in range(0, N-1):
        min_idx = i
        for j in range(i+1, N):
            if A[min_idx].num > A[j].num:
                min_idx = j
        A[i], A[min_idx] = A[min_idx], A[i]
        
    return A trump_selection_sort for i in range(0, N-1):
        min_idx = i
        for j in range(i+1, N):
            if A[min_idx].num > A[j].num:
                min_idx = j
        A[i], A[min_idx] = A[min_idx], A[i]
        
     i range(0, N-1) range 0 N-1 N 1 min_idx = i min_idx i for j in range(i+1, N):
            if A[min_idx].num > A[j].num:
                min_idx = j
         j range(i+1, N) range i+1 i 1 N if A[min_idx].num > A[j].num:
                min_idx = j
         A[min_idx].num > A[j].num A[min_idx].num [min_idx] A min_idx num A[j].num [j] A j num min_idx = j min_idx j A[i], A[min_idx] = A[min_idx], A[i] A[i] A i A[min_idx] A min_idx A[min_idx] A min_idx A[i] A i return A A A A N N N = int(input().rstrip()) N int(input().rstrip()) int input().rstrip() ().rstrip () input rstrip Trump(a) for a in input().rstrip().split() for a in input().rstrip().split() a input().rstrip().split() ().rstrip().split ().rstrip() ().rstrip () input rstrip split for a in input().rstrip().split() Trump(a) Trump a A = [Trump(a) for a in input().rstrip().split()] A [Trump(a) for a in input().rstrip().split()] A_2 = A.copy() A_2 A.copy() A.copy A copy bubble_result = trump_bubble_sort(A, N) bubble_result trump_bubble_sort(A, N) trump_bubble_sort A N selection_result = trump_selection_sort(A_2, N) selection_result trump_selection_sort(A_2, N) trump_selection_sort A_2 N stable = True stable True for i in range(0, N):
    if bubble_result[i] != selection_result[i]:
        stable = False
        break i range(0, N) range 0 N if bubble_result[i] != selection_result[i]:
        stable = False
        break bubble_result[i] != selection_result[i] bubble_result[i] bubble_result i selection_result[i] selection_result i stable = False stable False break print(" ".join(bubble_result)) print " ".join(bubble_result) " ".join " " join bubble_result print("Stable") print "Stable" print(" ".join(selection_result)) print " ".join(selection_result) " ".join " " join selection_result print("Stable" if stable else "Not stable") print "Stable" if stable else "Not stable" stable "Stable" "Not stable" selection_result = trump_selection_sort(A_2, N) trump_selection_sort(A_2, N) selection_result def trump_selection_sort(A, N):
    for i in range(0, N-1):
        min_idx = i
        for j in range(i+1, N):
            if A[min_idx].num > A[j].num:
                min_idx = j
        A[i], A[min_idx] = A[min_idx], A[i]
        
    return A def trump_selection_sort(A, N):
    for i in range(0, N-1):
        min_idx = i
        for j in range(i+1, N):
            if A[min_idx].num > A[j].num:
                min_idx = j
        A[i], A[min_idx] = A[min_idx], A[i]
        
    return A trump_selection_sort A_2 = A.copy() A.copy() A_2 class Trump(str):
    def __init__(self, card):
        super().__init__()
        self.num = int(card[1]) class Trump(str):
    def __init__(self, card):
        super().__init__()
        self.num = int(card[1]) Trump A = [Trump(a) for a in input().rstrip().split()] [Trump(a) for a in input().rstrip().split()] A bubble_result = trump_bubble_sort(A, N) trump_bubble_sort(A, N) bubble_result stable = True True stable def trump_bubble_sort(A, N):
    flg = True
    while flg:
        flg = False
        for i in range(1, N):
            if A[i - 1].num > A[i].num:
                A[i], A[i - 1] = A[i - 1], A[i]
                flg = True
    return A def trump_bubble_sort(A, N):
    flg = True
    while flg:
        flg = False
        for i in range(1, N):
            if A[i - 1].num > A[i].num:
                A[i], A[i - 1] = A[i - 1], A[i]
                flg = True
    return A trump_bubble_sort N = int(input().rstrip()) int(input().rstrip()) N stable = False False stable
class Trump(str):
    def __init__(self, card):
        super().__init__()
        self.num = int(card[1])

def trump_bubble_sort(A, N):
    flg = True
    while flg:
        flg = False
        for i in range(1, N):
            if A[i - 1].num > A[i].num:
                A[i], A[i - 1] = A[i - 1], A[i]
                flg = True
    return A


def trump_selection_sort(A, N):
    for i in range(0, N-1):
        min_idx = i
        for j in range(i+1, N):
            if A[min_idx].num > A[j].num:
                min_idx = j
        A[i], A[min_idx] = A[min_idx], A[i]
        
    return A

N = int(input().rstrip())
A = [Trump(a) for a in input().rstrip().split()]
A_2 = A.copy()

bubble_result = trump_bubble_sort(A, N)
selection_result = trump_selection_sort(A_2, N)

stable = True
for i in range(0, N):
    if bubble_result[i] != selection_result[i]:
        stable = False
        break
print(" ".join(bubble_result))
print("Stable")
print(" ".join(selection_result))
print("Stable" if stable else "Not stable")
    

