def BubbleSort(C_B, N):
    for i in range(N):
        for j in range(N-1,i,-1):
            if int(C_B[j][1]) < int(C_B[j-1][1]) :
                C_B[j], C_B[j-1] = C_B[j-1], C_B[j]
    return C_B

def SelectionSort(C_S, N):
    for i in range(N):
        minj = i
        for j in range(i,N):
            if int(C_S[j][1]) < int(C_S[minj][1]):
                minj = j
        C_S[i], C_S[minj] = C_S[minj], C_S[i]
    return C_S

def J(C, C_):
    for i in range(1,len(C_)):
        if int(C_[i-1][1]) == int(C_[i][1]):
            p = C.index(C_[i-1])
            q = C.index(C_[i])
            if q<p:
                return 'Not stable'
    return 'Stable'

N = int(input())
C = list(input().split())
C_B = C.copy()
C_S = C.copy()
CB = BubbleSort(C_B, N)
CS = SelectionSort(C_S, N)

for i in range(len(CB)):
    if i==len(CB)-1:
        print(CB[i])
    else:
        print(CB[i],end=' ')
print(J(C, CB))
for i in range(len(CS)):
    if i==len(CS)-1:
        print(CS[i])
    else:
        print(CS[i],end=' ')
print(J(C, CS)) def BubbleSort(C_B, N):
    for i in range(N):
        for j in range(N-1,i,-1):
            if int(C_B[j][1]) < int(C_B[j-1][1]) :
                C_B[j], C_B[j-1] = C_B[j-1], C_B[j]
    return C_B BubbleSort for i in range(N):
        for j in range(N-1,i,-1):
            if int(C_B[j][1]) < int(C_B[j-1][1]) :
                C_B[j], C_B[j-1] = C_B[j-1], C_B[j]
     i range(N) range N for j in range(N-1,i,-1):
            if int(C_B[j][1]) < int(C_B[j-1][1]) :
                C_B[j], C_B[j-1] = C_B[j-1], C_B[j]
     j range(N-1,i,-1) range N-1 N 1 i -1 if int(C_B[j][1]) < int(C_B[j-1][1]) :
                C_B[j], C_B[j-1] = C_B[j-1], C_B[j]
     int(C_B[j][1]) < int(C_B[j-1][1]) int(C_B[j][1]) int C_B[j][1] [j] C_B j 1 int(C_B[j-1][1]) int C_B[j-1][1] [j-1] C_B j-1 j 1 1 C_B[j], C_B[j-1] = C_B[j-1], C_B[j] C_B[j] C_B j C_B[j-1] C_B j-1 j 1 C_B[j-1] C_B j-1 j 1 C_B[j] C_B j return C_B C_B C_B C_B N N def SelectionSort(C_S, N):
    for i in range(N):
        minj = i
        for j in range(i,N):
            if int(C_S[j][1]) < int(C_S[minj][1]):
                minj = j
        C_S[i], C_S[minj] = C_S[minj], C_S[i]
    return C_S SelectionSort for i in range(N):
        minj = i
        for j in range(i,N):
            if int(C_S[j][1]) < int(C_S[minj][1]):
                minj = j
        C_S[i], C_S[minj] = C_S[minj], C_S[i]
     i range(N) range N minj = i minj i for j in range(i,N):
            if int(C_S[j][1]) < int(C_S[minj][1]):
                minj = j
         j range(i,N) range i N if int(C_S[j][1]) < int(C_S[minj][1]):
                minj = j
         int(C_S[j][1]) < int(C_S[minj][1]) int(C_S[j][1]) int C_S[j][1] [j] C_S j 1 int(C_S[minj][1]) int C_S[minj][1] [minj] C_S minj 1 minj = j minj j C_S[i], C_S[minj] = C_S[minj], C_S[i] C_S[i] C_S i C_S[minj] C_S minj C_S[minj] C_S minj C_S[i] C_S i return C_S C_S C_S C_S N N def J(C, C_):
    for i in range(1,len(C_)):
        if int(C_[i-1][1]) == int(C_[i][1]):
            p = C.index(C_[i-1])
            q = C.index(C_[i])
            if q<p:
                return 'Not stable'
    return 'Stable' J for i in range(1,len(C_)):
        if int(C_[i-1][1]) == int(C_[i][1]):
            p = C.index(C_[i-1])
            q = C.index(C_[i])
            if q<p:
                return 'Not stable'
     i range(1,len(C_)) range 1 len(C_) len C_ if int(C_[i-1][1]) == int(C_[i][1]):
            p = C.index(C_[i-1])
            q = C.index(C_[i])
            if q<p:
                return 'Not stable'
     int(C_[i-1][1]) == int(C_[i][1]) int(C_[i-1][1]) int C_[i-1][1] [i-1] C_ i-1 i 1 1 int(C_[i][1]) int C_[i][1] [i] C_ i 1 p = C.index(C_[i-1]) p C.index(C_[i-1]) C.index C index C_[i-1] C_ i-1 i 1 q = C.index(C_[i]) q C.index(C_[i]) C.index C index C_[i] C_ i if q<p:
                return 'Not stable'
     q<p q p return 'Not stable' 'Not stable' return 'Stable' 'Stable' C C C_ C_ N = int(input()) N int(input()) int input() input C = list(input().split()) C list(input().split()) list input().split() ().split () input split C_B = C.copy() C_B C.copy() C.copy C copy C_S = C.copy() C_S C.copy() C.copy C copy CB = BubbleSort(C_B, N) CB BubbleSort(C_B, N) BubbleSort C_B N CS = SelectionSort(C_S, N) CS SelectionSort(C_S, N) SelectionSort C_S N for i in range(len(CB)):
    if i==len(CB)-1:
        print(CB[i])
    else:
        print(CB[i],end=' ') i range(len(CB)) range len(CB) len CB if i==len(CB)-1:
        print(CB[i])
    else:
        print(CB[i],end=' ') i==len(CB)-1 i len(CB)-1 len(CB) len CB 1 print(CB[i]) print CB[i] CB i print(CB[i],end=' ') print CB[i] CB i end=' ' end ' ' print(J(C, CB)) print J(C, CB) J C CB for i in range(len(CS)):
    if i==len(CS)-1:
        print(CS[i])
    else:
        print(CS[i],end=' ') i range(len(CS)) range len(CS) len CS if i==len(CS)-1:
        print(CS[i])
    else:
        print(CS[i],end=' ') i==len(CS)-1 i len(CS)-1 len(CS) len CS 1 print(CS[i]) print CS[i] CS i print(CS[i],end=' ') print CS[i] CS i end=' ' end ' ' print(J(C, CS)) print J(C, CS) J C CS C = list(input().split()) list(input().split()) C N = int(input()) int(input()) N CS = SelectionSort(C_S, N) SelectionSort(C_S, N) CS def BubbleSort(C_B, N):
    for i in range(N):
        for j in range(N-1,i,-1):
            if int(C_B[j][1]) < int(C_B[j-1][1]) :
                C_B[j], C_B[j-1] = C_B[j-1], C_B[j]
    return C_B def BubbleSort(C_B, N):
    for i in range(N):
        for j in range(N-1,i,-1):
            if int(C_B[j][1]) < int(C_B[j-1][1]) :
                C_B[j], C_B[j-1] = C_B[j-1], C_B[j]
    return C_B BubbleSort def SelectionSort(C_S, N):
    for i in range(N):
        minj = i
        for j in range(i,N):
            if int(C_S[j][1]) < int(C_S[minj][1]):
                minj = j
        C_S[i], C_S[minj] = C_S[minj], C_S[i]
    return C_S def SelectionSort(C_S, N):
    for i in range(N):
        minj = i
        for j in range(i,N):
            if int(C_S[j][1]) < int(C_S[minj][1]):
                minj = j
        C_S[i], C_S[minj] = C_S[minj], C_S[i]
    return C_S SelectionSort C_S = C.copy() C.copy() C_S def J(C, C_):
    for i in range(1,len(C_)):
        if int(C_[i-1][1]) == int(C_[i][1]):
            p = C.index(C_[i-1])
            q = C.index(C_[i])
            if q<p:
                return 'Not stable'
    return 'Stable' def J(C, C_):
    for i in range(1,len(C_)):
        if int(C_[i-1][1]) == int(C_[i][1]):
            p = C.index(C_[i-1])
            q = C.index(C_[i])
            if q<p:
                return 'Not stable'
    return 'Stable' J C_B = C.copy() C.copy() C_B CB = BubbleSort(C_B, N) BubbleSort(C_B, N) CB
def BubbleSort(C_B, N):
    for i in range(N):
        for j in range(N-1,i,-1):
            if int(C_B[j][1]) < int(C_B[j-1][1]) :
                C_B[j], C_B[j-1] = C_B[j-1], C_B[j]
    return C_B

def SelectionSort(C_S, N):
    for i in range(N):
        minj = i
        for j in range(i,N):
            if int(C_S[j][1]) < int(C_S[minj][1]):
                minj = j
        C_S[i], C_S[minj] = C_S[minj], C_S[i]
    return C_S

def J(C, C_):
    for i in range(1,len(C_)):
        if int(C_[i-1][1]) == int(C_[i][1]):
            p = C.index(C_[i-1])
            q = C.index(C_[i])
            if q<p:
                return 'Not stable'
    return 'Stable'

N = int(input())
C = list(input().split())
C_B = C.copy()
C_S = C.copy()
CB = BubbleSort(C_B, N)
CS = SelectionSort(C_S, N)

for i in range(len(CB)):
    if i==len(CB)-1:
        print(CB[i])
    else:
        print(CB[i],end=' ')
print(J(C, CB))
for i in range(len(CS)):
    if i==len(CS)-1:
        print(CS[i])
    else:
        print(CS[i],end=' ')
print(J(C, CS))
