def BSort(A,n):
    flag=1
    while flag:
        flag=0
        for i in range(n-1,0,-1):
            if A[i][1]<A[i-1][1]:
                A[i],A[i-1]=A[i-1],A[i]
                flag=1

def SSort(A,n):
    for i in range(n):
        minj=i
        for j in range(i,n):
            if A[j][1]<A[minj][1]:
                minj=j
        A[i],A[minj]=A[minj],A[i]

n = int(input())

A = list(input().split())
B=A[:]

BSort(A,n)
print(' '.join(A))
print('Stable')
SSort(B,n)
print(' '.join(B))

if A==B:
    print('Stable')
else:
    print('Not stable')
 def BSort(A,n):
    flag=1
    while flag:
        flag=0
        for i in range(n-1,0,-1):
            if A[i][1]<A[i-1][1]:
                A[i],A[i-1]=A[i-1],A[i]
                flag=1 BSort flag=1 flag 1 while flag:
        flag=0
        for i in range(n-1,0,-1):
            if A[i][1]<A[i-1][1]:
                A[i],A[i-1]=A[i-1],A[i]
                flag=1 flag flag=0 flag 0 for i in range(n-1,0,-1):
            if A[i][1]<A[i-1][1]:
                A[i],A[i-1]=A[i-1],A[i]
                flag=1 i range(n-1,0,-1) range n-1 n 1 0 -1 if A[i][1]<A[i-1][1]:
                A[i],A[i-1]=A[i-1],A[i]
                flag=1 A[i][1]<A[i-1][1] A[i][1] [i] A i 1 A[i-1][1] [i-1] A i-1 i 1 1 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 flag=1 flag 1 A A n n def SSort(A,n):
    for i in range(n):
        minj=i
        for j in range(i,n):
            if A[j][1]<A[minj][1]:
                minj=j
        A[i],A[minj]=A[minj],A[i] SSort for i in range(n):
        minj=i
        for j in range(i,n):
            if A[j][1]<A[minj][1]:
                minj=j
        A[i],A[minj]=A[minj],A[i] i range(n) range n minj=i minj i for j in range(i,n):
            if A[j][1]<A[minj][1]:
                minj=j
         j range(i,n) range i n if A[j][1]<A[minj][1]:
                minj=j
         A[j][1]<A[minj][1] A[j][1] [j] A j 1 A[minj][1] [minj] A minj 1 minj=j minj j A[i],A[minj]=A[minj],A[i] A[i] A i A[minj] A minj A[minj] A minj A[i] A i A A 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 B=A[:] B A[:] A : BSort(A,n) BSort A n print(' '.join(A)) print ' '.join(A) ' '.join ' ' join A print('Stable') print 'Stable' SSort(B,n) SSort B n print(' '.join(B)) print ' '.join(B) ' '.join ' ' join B if A==B:
    print('Stable')
else:
    print('Not stable') A==B A B print('Stable') print 'Stable' print('Not stable') print 'Not stable' def BSort(A,n):
    flag=1
    while flag:
        flag=0
        for i in range(n-1,0,-1):
            if A[i][1]<A[i-1][1]:
                A[i],A[i-1]=A[i-1],A[i]
                flag=1 def BSort(A,n):
    flag=1
    while flag:
        flag=0
        for i in range(n-1,0,-1):
            if A[i][1]<A[i-1][1]:
                A[i],A[i-1]=A[i-1],A[i]
                flag=1 BSort n = int(input()) int(input()) n B=A[:] A[:] B def SSort(A,n):
    for i in range(n):
        minj=i
        for j in range(i,n):
            if A[j][1]<A[minj][1]:
                minj=j
        A[i],A[minj]=A[minj],A[i] def SSort(A,n):
    for i in range(n):
        minj=i
        for j in range(i,n):
            if A[j][1]<A[minj][1]:
                minj=j
        A[i],A[minj]=A[minj],A[i] SSort A = list(input().split()) list(input().split()) A
def BSort(A,n):
    flag=1
    while flag:
        flag=0
        for i in range(n-1,0,-1):
            if A[i][1]<A[i-1][1]:
                A[i],A[i-1]=A[i-1],A[i]
                flag=1

def SSort(A,n):
    for i in range(n):
        minj=i
        for j in range(i,n):
            if A[j][1]<A[minj][1]:
                minj=j
        A[i],A[minj]=A[minj],A[i]

n = int(input())

A = list(input().split())
B=A[:]

BSort(A,n)
print(' '.join(A))
print('Stable')
SSort(B,n)
print(' '.join(B))

if A==B:
    print('Stable')
else:
    print('Not stable')

