import time
import copy

def BubbleSort(A, N):
    flag = 1
    while flag:
        flag = 0
        for j in reversed(range(1, N)):
            if A[j]['value']<A[j-1]['value']:
                A[j], A[j-1] = A[j-1], A[j]
                flag = 1
    return A

def SelectionSort(A, N):
    for i in range(0, N):
        minj = i
        for j in range(i, N):
            if A[j]['value']<A[minj]['value']:
                minj = j
        if i!=minj:
            A[i], A[minj] = A[minj], A[i]
    return A

def IsStable(answer, target):
    N = len(answer)
    for i in range(N):
        if answer[i]['body'] != target[i]['body']:
            return False
    return True

# Stable Sort
def main():
    N = int(input())
    A = list(
        map(lambda x: {'body': x, 'value': int(x[1])}, input().split())
    )
    A_sortby_bubble = copy.deepcopy(A)
    A_sortby_select = copy.deepcopy(A)
    A_sortby_bubble = BubbleSort(A_sortby_bubble, N)
    A_sortby_select = SelectionSort(A_sortby_select, N)
    print(' '.join(map(lambda x: x['body'],A_sortby_bubble)))
    print('Stable')
    print(' '.join(map(lambda x: x['body'], A_sortby_select)))
    if IsStable(A_sortby_bubble, A_sortby_select):
        print('Stable')
    else:
        print('Not stable') 
    
main() import time time import copy copy def BubbleSort(A, N):
    flag = 1
    while flag:
        flag = 0
        for j in reversed(range(1, N)):
            if A[j]['value']<A[j-1]['value']:
                A[j], A[j-1] = A[j-1], A[j]
                flag = 1
    return A BubbleSort flag = 1 flag 1 while flag:
        flag = 0
        for j in reversed(range(1, N)):
            if A[j]['value']<A[j-1]['value']:
                A[j], A[j-1] = A[j-1], A[j]
                flag = 1
     flag flag = 0 flag 0 for j in reversed(range(1, N)):
            if A[j]['value']<A[j-1]['value']:
                A[j], A[j-1] = A[j-1], A[j]
                flag = 1
     j reversed(range(1, N)) reversed range(1, N) range 1 N if A[j]['value']<A[j-1]['value']:
                A[j], A[j-1] = A[j-1], A[j]
                flag = 1
     A[j]['value']<A[j-1]['value'] A[j]['value'] [j] A j 'value' A[j-1]['value'] [j-1] A j-1 j 1 'value' A[j], A[j-1] = A[j-1], A[j] A[j] A j A[j-1] A j-1 j 1 A[j-1] A j-1 j 1 A[j] A j flag = 1 flag 1 return A A A A N N def SelectionSort(A, N):
    for i in range(0, N):
        minj = i
        for j in range(i, N):
            if A[j]['value']<A[minj]['value']:
                minj = j
        if i!=minj:
            A[i], A[minj] = A[minj], A[i]
    return A SelectionSort for i in range(0, N):
        minj = i
        for j in range(i, N):
            if A[j]['value']<A[minj]['value']:
                minj = j
        if i!=minj:
            A[i], A[minj] = A[minj], A[i]
     i range(0, N) range 0 N minj = i minj i for j in range(i, N):
            if A[j]['value']<A[minj]['value']:
                minj = j
         j range(i, N) range i N if A[j]['value']<A[minj]['value']:
                minj = j
         A[j]['value']<A[minj]['value'] A[j]['value'] [j] A j 'value' A[minj]['value'] [minj] A minj 'value' minj = j minj j if i!=minj:
            A[i], A[minj] = A[minj], A[i]
     i!=minj i minj A[i], A[minj] = A[minj], A[i] A[i] A i A[minj] A minj A[minj] A minj A[i] A i return A A A A N N def IsStable(answer, target):
    N = len(answer)
    for i in range(N):
        if answer[i]['body'] != target[i]['body']:
            return False
    return True

# Stable Sort IsStable N = len(answer) N len(answer) len answer for i in range(N):
        if answer[i]['body'] != target[i]['body']:
            return False
     i range(N) range N if answer[i]['body'] != target[i]['body']:
            return False
     answer[i]['body'] != target[i]['body'] answer[i]['body'] [i] answer i 'body' target[i]['body'] [i] target i 'body' return False False return True True answer answer target target def main():
    N = int(input())
    A = list(
        map(lambda x: {'body': x, 'value': int(x[1])}, input().split())
    )
    A_sortby_bubble = copy.deepcopy(A)
    A_sortby_select = copy.deepcopy(A)
    A_sortby_bubble = BubbleSort(A_sortby_bubble, N)
    A_sortby_select = SelectionSort(A_sortby_select, N)
    print(' '.join(map(lambda x: x['body'],A_sortby_bubble)))
    print('Stable')
    print(' '.join(map(lambda x: x['body'], A_sortby_select)))
    if IsStable(A_sortby_bubble, A_sortby_select):
        print('Stable')
    else:
        print('Not stable') 
     main N = int(input()) N int(input()) int input() input A = list(
        map(lambda x: {'body': x, 'value': int(x[1])}, input().split())
    ) A list(
        map(lambda x: {'body': x, 'value': int(x[1])}, input().split())
    ) list map(lambda x: {'body': x, 'value': int(x[1])}, input().split()) map lambda x: {'body': x, 'value': int(x[1])} {'body': x, 'value': int(x[1])} 'body' 'body' x 'value' 'value' int(x[1]) int x[1] x 1 x input().split() ().split () input split A_sortby_bubble = copy.deepcopy(A) A_sortby_bubble copy.deepcopy(A) copy.deepcopy copy deepcopy A A_sortby_select = copy.deepcopy(A) A_sortby_select copy.deepcopy(A) copy.deepcopy copy deepcopy A A_sortby_bubble = BubbleSort(A_sortby_bubble, N) A_sortby_bubble BubbleSort(A_sortby_bubble, N) BubbleSort A_sortby_bubble N A_sortby_select = SelectionSort(A_sortby_select, N) A_sortby_select SelectionSort(A_sortby_select, N) SelectionSort A_sortby_select N print(' '.join(map(lambda x: x['body'],A_sortby_bubble))) print ' '.join(map(lambda x: x['body'],A_sortby_bubble)) ' '.join ' ' join map(lambda x: x['body'],A_sortby_bubble) map lambda x: x['body'] x['body'] x 'body' x A_sortby_bubble print('Stable') print 'Stable' print(' '.join(map(lambda x: x['body'], A_sortby_select))) print ' '.join(map(lambda x: x['body'], A_sortby_select)) ' '.join ' ' join map(lambda x: x['body'], A_sortby_select) map lambda x: x['body'] x['body'] x 'body' x A_sortby_select if IsStable(A_sortby_bubble, A_sortby_select):
        print('Stable')
    else:
        print('Not stable') 
     IsStable(A_sortby_bubble, A_sortby_select) IsStable A_sortby_bubble A_sortby_select print('Stable') print 'Stable' print('Not stable') print 'Not stable' main() main def BubbleSort(A, N):
    flag = 1
    while flag:
        flag = 0
        for j in reversed(range(1, N)):
            if A[j]['value']<A[j-1]['value']:
                A[j], A[j-1] = A[j-1], A[j]
                flag = 1
    return A def BubbleSort(A, N):
    flag = 1
    while flag:
        flag = 0
        for j in reversed(range(1, N)):
            if A[j]['value']<A[j-1]['value']:
                A[j], A[j-1] = A[j-1], A[j]
                flag = 1
    return A BubbleSort def main():
    N = int(input())
    A = list(
        map(lambda x: {'body': x, 'value': int(x[1])}, input().split())
    )
    A_sortby_bubble = copy.deepcopy(A)
    A_sortby_select = copy.deepcopy(A)
    A_sortby_bubble = BubbleSort(A_sortby_bubble, N)
    A_sortby_select = SelectionSort(A_sortby_select, N)
    print(' '.join(map(lambda x: x['body'],A_sortby_bubble)))
    print('Stable')
    print(' '.join(map(lambda x: x['body'], A_sortby_select)))
    if IsStable(A_sortby_bubble, A_sortby_select):
        print('Stable')
    else:
        print('Not stable') 
     def main():
    N = int(input())
    A = list(
        map(lambda x: {'body': x, 'value': int(x[1])}, input().split())
    )
    A_sortby_bubble = copy.deepcopy(A)
    A_sortby_select = copy.deepcopy(A)
    A_sortby_bubble = BubbleSort(A_sortby_bubble, N)
    A_sortby_select = SelectionSort(A_sortby_select, N)
    print(' '.join(map(lambda x: x['body'],A_sortby_bubble)))
    print('Stable')
    print(' '.join(map(lambda x: x['body'], A_sortby_select)))
    if IsStable(A_sortby_bubble, A_sortby_select):
        print('Stable')
    else:
        print('Not stable') 
     main def IsStable(answer, target):
    N = len(answer)
    for i in range(N):
        if answer[i]['body'] != target[i]['body']:
            return False
    return True

# Stable Sort def IsStable(answer, target):
    N = len(answer)
    for i in range(N):
        if answer[i]['body'] != target[i]['body']:
            return False
    return True

# Stable Sort IsStable def SelectionSort(A, N):
    for i in range(0, N):
        minj = i
        for j in range(i, N):
            if A[j]['value']<A[minj]['value']:
                minj = j
        if i!=minj:
            A[i], A[minj] = A[minj], A[i]
    return A def SelectionSort(A, N):
    for i in range(0, N):
        minj = i
        for j in range(i, N):
            if A[j]['value']<A[minj]['value']:
                minj = j
        if i!=minj:
            A[i], A[minj] = A[minj], A[i]
    return A SelectionSort
import time
import copy

def BubbleSort(A, N):
    flag = 1
    while flag:
        flag = 0
        for j in reversed(range(1, N)):
            if A[j]['value']<A[j-1]['value']:
                A[j], A[j-1] = A[j-1], A[j]
                flag = 1
    return A

def SelectionSort(A, N):
    for i in range(0, N):
        minj = i
        for j in range(i, N):
            if A[j]['value']<A[minj]['value']:
                minj = j
        if i!=minj:
            A[i], A[minj] = A[minj], A[i]
    return A

def IsStable(answer, target):
    N = len(answer)
    for i in range(N):
        if answer[i]['body'] != target[i]['body']:
            return False
    return True

# Stable Sort
def main():
    N = int(input())
    A = list(
        map(lambda x: {'body': x, 'value': int(x[1])}, input().split())
    )
    A_sortby_bubble = copy.deepcopy(A)
    A_sortby_select = copy.deepcopy(A)
    A_sortby_bubble = BubbleSort(A_sortby_bubble, N)
    A_sortby_select = SelectionSort(A_sortby_select, N)
    print(' '.join(map(lambda x: x['body'],A_sortby_bubble)))
    print('Stable')
    print(' '.join(map(lambda x: x['body'], A_sortby_select)))
    if IsStable(A_sortby_bubble, A_sortby_select):
        print('Stable')
    else:
        print('Not stable') 
    
main()
