#バブルソート
def BubbleSort(C,N):
    for i in range (0,N,1):
        for j in range(N-1,i,-1):
            if int(C[j][1:2]) < int(C[j-1][1:2]):
                tmp = C[j]
                C[j] = C[j-1]
                C[j-1] = tmp
    print(' '.join(C))

    
                
#選択ソート
def SelectionSort(C,N):
    for i in range(0,N,1):
        minj = i
        for j in range(i,N,1):
            if int(C[j][1:2]) < int(C[minj][1:2]):
                minj = j
        tmp = C[i]
        C[i] = C[minj]
        C[minj] = tmp
    print(' '.join(C))



N = int(input())
A = list(input().split())
Bubble_A = A.copy()
Selection_A = A.copy()

BubbleSort(Bubble_A,N)
print("Stable")
SelectionSort(Selection_A,N)
flag = True
for Bubble,Selection in zip(Bubble_A,Selection_A):
    if Bubble != Selection:
        flag = False
if flag :
    print("Stable")
else:
    print("Not stable")
 def BubbleSort(C,N):
    for i in range (0,N,1):
        for j in range(N-1,i,-1):
            if int(C[j][1:2]) < int(C[j-1][1:2]):
                tmp = C[j]
                C[j] = C[j-1]
                C[j-1] = tmp
    print(' '.join(C))

    
                
#選択ソート BubbleSort for i in range (0,N,1):
        for j in range(N-1,i,-1):
            if int(C[j][1:2]) < int(C[j-1][1:2]):
                tmp = C[j]
                C[j] = C[j-1]
                C[j-1] = tmp
     i range (0,N,1) range 0 N 1 for j in range(N-1,i,-1):
            if int(C[j][1:2]) < int(C[j-1][1:2]):
                tmp = C[j]
                C[j] = C[j-1]
                C[j-1] = tmp
     j range(N-1,i,-1) range N-1 N 1 i -1 if int(C[j][1:2]) < int(C[j-1][1:2]):
                tmp = C[j]
                C[j] = C[j-1]
                C[j-1] = tmp
     int(C[j][1:2]) < int(C[j-1][1:2]) int(C[j][1:2]) int C[j][1:2] [j] C j 1:2 1 2 int(C[j-1][1:2]) int C[j-1][1:2] [j-1] C j-1 j 1 1:2 1 2 tmp = C[j] tmp C[j] C j C[j] = C[j-1] C[j] C j C[j-1] C j-1 j 1 C[j-1] = tmp C[j-1] C j-1 j 1 tmp print(' '.join(C)) print ' '.join(C) ' '.join ' ' join C C C N N def SelectionSort(C,N):
    for i in range(0,N,1):
        minj = i
        for j in range(i,N,1):
            if int(C[j][1:2]) < int(C[minj][1:2]):
                minj = j
        tmp = C[i]
        C[i] = C[minj]
        C[minj] = tmp
    print(' '.join(C)) SelectionSort for i in range(0,N,1):
        minj = i
        for j in range(i,N,1):
            if int(C[j][1:2]) < int(C[minj][1:2]):
                minj = j
        tmp = C[i]
        C[i] = C[minj]
        C[minj] = tmp
     i range(0,N,1) range 0 N 1 minj = i minj i for j in range(i,N,1):
            if int(C[j][1:2]) < int(C[minj][1:2]):
                minj = j
         j range(i,N,1) range i N 1 if int(C[j][1:2]) < int(C[minj][1:2]):
                minj = j
         int(C[j][1:2]) < int(C[minj][1:2]) int(C[j][1:2]) int C[j][1:2] [j] C j 1:2 1 2 int(C[minj][1:2]) int C[minj][1:2] [minj] C minj 1:2 1 2 minj = j minj j tmp = C[i] tmp C[i] C i C[i] = C[minj] C[i] C i C[minj] C minj C[minj] = tmp C[minj] C minj tmp print(' '.join(C)) print ' '.join(C) ' '.join ' ' join C C C N N N = int(input()) N int(input()) int input() input A = list(input().split()) A list(input().split()) list input().split() ().split () input split Bubble_A = A.copy() Bubble_A A.copy() A.copy A copy Selection_A = A.copy() Selection_A A.copy() A.copy A copy BubbleSort(Bubble_A,N) BubbleSort Bubble_A N print("Stable") print "Stable" SelectionSort(Selection_A,N) SelectionSort Selection_A N flag = True flag True for Bubble,Selection in zip(Bubble_A,Selection_A):
    if Bubble != Selection:
        flag = False Bubble Selection zip(Bubble_A,Selection_A) zip Bubble_A Selection_A if Bubble != Selection:
        flag = False Bubble != Selection Bubble Selection flag = False flag False if flag :
    print("Stable")
else:
    print("Not stable") flag print("Stable") print "Stable" print("Not stable") print "Not stable" def SelectionSort(C,N):
    for i in range(0,N,1):
        minj = i
        for j in range(i,N,1):
            if int(C[j][1:2]) < int(C[minj][1:2]):
                minj = j
        tmp = C[i]
        C[i] = C[minj]
        C[minj] = tmp
    print(' '.join(C)) def SelectionSort(C,N):
    for i in range(0,N,1):
        minj = i
        for j in range(i,N,1):
            if int(C[j][1:2]) < int(C[minj][1:2]):
                minj = j
        tmp = C[i]
        C[i] = C[minj]
        C[minj] = tmp
    print(' '.join(C)) SelectionSort N = int(input()) int(input()) N A = list(input().split()) list(input().split()) A Selection_A = A.copy() A.copy() Selection_A def BubbleSort(C,N):
    for i in range (0,N,1):
        for j in range(N-1,i,-1):
            if int(C[j][1:2]) < int(C[j-1][1:2]):
                tmp = C[j]
                C[j] = C[j-1]
                C[j-1] = tmp
    print(' '.join(C))

    
                
#選択ソート def BubbleSort(C,N):
    for i in range (0,N,1):
        for j in range(N-1,i,-1):
            if int(C[j][1:2]) < int(C[j-1][1:2]):
                tmp = C[j]
                C[j] = C[j-1]
                C[j-1] = tmp
    print(' '.join(C))

    
                
#選択ソート BubbleSort flag = True True flag flag = False False flag Bubble_A = A.copy() A.copy() Bubble_A
#バブルソート
def BubbleSort(C,N):
    for i in range (0,N,1):
        for j in range(N-1,i,-1):
            if int(C[j][1:2]) < int(C[j-1][1:2]):
                tmp = C[j]
                C[j] = C[j-1]
                C[j-1] = tmp
    print(' '.join(C))

    
                
#選択ソート
def SelectionSort(C,N):
    for i in range(0,N,1):
        minj = i
        for j in range(i,N,1):
            if int(C[j][1:2]) < int(C[minj][1:2]):
                minj = j
        tmp = C[i]
        C[i] = C[minj]
        C[minj] = tmp
    print(' '.join(C))



N = int(input())
A = list(input().split())
Bubble_A = A.copy()
Selection_A = A.copy()

BubbleSort(Bubble_A,N)
print("Stable")
SelectionSort(Selection_A,N)
flag = True
for Bubble,Selection in zip(Bubble_A,Selection_A):
    if Bubble != Selection:
        flag = False
if flag :
    print("Stable")
else:
    print("Not stable")

