#coding:UTF-8
def BS(N,A):
    for i in range(N):
        for j in reversed(range(i+1,N)):
            if int(A[j][1:])<int(A[j-1][1:]):
                swap=A[j]
                A[j]=A[j-1]
                A[j-1]=swap
    return A

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

def Stable(A1,A2):
    ans="Stable"
    for i in range(len(A1)):
        if A1[i][1:]==A2[i][1:] and A1[i][:1]!=A2[i][:1]:
            ans="Not stable"
    return ans

def SS2(N,A,B):
    A1=BS(N,A)
    print(" ".join(A1))
    print("Stable")
    A2=SS(N,B)
    print(" ".join(A2))
    print(Stable(A1,A2))

if __name__=="__main__":
    N=int(input())
    a=input()
    A=a.split(" ")
    B=a.split(" ")
    SS2(N,A,B) def BS(N,A):
    for i in range(N):
        for j in reversed(range(i+1,N)):
            if int(A[j][1:])<int(A[j-1][1:]):
                swap=A[j]
                A[j]=A[j-1]
                A[j-1]=swap
    return A BS for i in range(N):
        for j in reversed(range(i+1,N)):
            if int(A[j][1:])<int(A[j-1][1:]):
                swap=A[j]
                A[j]=A[j-1]
                A[j-1]=swap
     i range(N) range N for j in reversed(range(i+1,N)):
            if int(A[j][1:])<int(A[j-1][1:]):
                swap=A[j]
                A[j]=A[j-1]
                A[j-1]=swap
     j reversed(range(i+1,N)) reversed range(i+1,N) range i+1 i 1 N if int(A[j][1:])<int(A[j-1][1:]):
                swap=A[j]
                A[j]=A[j-1]
                A[j-1]=swap
     int(A[j][1:])<int(A[j-1][1:]) int(A[j][1:]) int A[j][1:] [j] A j 1: 1 int(A[j-1][1:]) int A[j-1][1:] [j-1] A j-1 j 1 1: 1 swap=A[j] swap A[j] A j A[j]=A[j-1] A[j] A j A[j-1] A j-1 j 1 A[j-1]=swap A[j-1] A j-1 j 1 swap return A A N N A A def SS(N,A):
    for i in range(N):
        minj=i
        for j in range(i,N):
            if int(A[j][1:]) < int(A[minj][1:]):
                minj=j
        
        swap=A[i]
        A[i]=A[minj]
        A[minj]=swap
    return A SS for i in range(N):
        minj=i
        for j in range(i,N):
            if int(A[j][1:]) < int(A[minj][1:]):
                minj=j
        
        swap=A[i]
        A[i]=A[minj]
        A[minj]=swap
     i range(N) range N minj=i minj i for j in range(i,N):
            if int(A[j][1:]) < int(A[minj][1:]):
                minj=j
        
         j range(i,N) range i N if int(A[j][1:]) < int(A[minj][1:]):
                minj=j
        
         int(A[j][1:]) < int(A[minj][1:]) int(A[j][1:]) int A[j][1:] [j] A j 1: 1 int(A[minj][1:]) int A[minj][1:] [minj] A minj 1: 1 minj=j minj j swap=A[i] swap A[i] A i A[i]=A[minj] A[i] A i A[minj] A minj A[minj]=swap A[minj] A minj swap return A A N N A A def Stable(A1,A2):
    ans="Stable"
    for i in range(len(A1)):
        if A1[i][1:]==A2[i][1:] and A1[i][:1]!=A2[i][:1]:
            ans="Not stable"
    return ans Stable ans="Stable" ans "Stable" for i in range(len(A1)):
        if A1[i][1:]==A2[i][1:] and A1[i][:1]!=A2[i][:1]:
            ans="Not stable"
     i range(len(A1)) range len(A1) len A1 if A1[i][1:]==A2[i][1:] and A1[i][:1]!=A2[i][:1]:
            ans="Not stable"
     A1[i][1:]==A2[i][1:] and A1[i][:1]!=A2[i][:1] A1[i][1:]==A2[i][1:] A1[i][1:] [i] A1 i 1: 1 A2[i][1:] [i] A2 i 1: 1 A1[i][:1]!=A2[i][:1] A1[i][:1] [i] A1 i :1 1 A2[i][:1] [i] A2 i :1 1 ans="Not stable" ans "Not stable" return ans ans A1 A1 A2 A2 def SS2(N,A,B):
    A1=BS(N,A)
    print(" ".join(A1))
    print("Stable")
    A2=SS(N,B)
    print(" ".join(A2))
    print(Stable(A1,A2)) SS2 A1=BS(N,A) A1 BS(N,A) BS N A print(" ".join(A1)) print " ".join(A1) " ".join " " join A1 print("Stable") print "Stable" A2=SS(N,B) A2 SS(N,B) SS N B print(" ".join(A2)) print " ".join(A2) " ".join " " join A2 print(Stable(A1,A2)) print Stable(A1,A2) Stable A1 A2 N N A A B B if __name__=="__main__":
    N=int(input())
    a=input()
    A=a.split(" ")
    B=a.split(" ")
    SS2(N,A,B) __name__=="__main__" __name__ "__main__" N=int(input()) N int(input()) int input() input a=input() a input() input A=a.split(" ") A a.split(" ") a.split a split " " B=a.split(" ") B a.split(" ") a.split a split " " SS2(N,A,B) SS2 N A B def SS2(N,A,B):
    A1=BS(N,A)
    print(" ".join(A1))
    print("Stable")
    A2=SS(N,B)
    print(" ".join(A2))
    print(Stable(A1,A2)) def SS2(N,A,B):
    A1=BS(N,A)
    print(" ".join(A1))
    print("Stable")
    A2=SS(N,B)
    print(" ".join(A2))
    print(Stable(A1,A2)) SS2 B=a.split(" ") a.split(" ") B def SS(N,A):
    for i in range(N):
        minj=i
        for j in range(i,N):
            if int(A[j][1:]) < int(A[minj][1:]):
                minj=j
        
        swap=A[i]
        A[i]=A[minj]
        A[minj]=swap
    return A def SS(N,A):
    for i in range(N):
        minj=i
        for j in range(i,N):
            if int(A[j][1:]) < int(A[minj][1:]):
                minj=j
        
        swap=A[i]
        A[i]=A[minj]
        A[minj]=swap
    return A SS def BS(N,A):
    for i in range(N):
        for j in reversed(range(i+1,N)):
            if int(A[j][1:])<int(A[j-1][1:]):
                swap=A[j]
                A[j]=A[j-1]
                A[j-1]=swap
    return A def BS(N,A):
    for i in range(N):
        for j in reversed(range(i+1,N)):
            if int(A[j][1:])<int(A[j-1][1:]):
                swap=A[j]
                A[j]=A[j-1]
                A[j-1]=swap
    return A BS A=a.split(" ") a.split(" ") A N=int(input()) int(input()) N a=input() input() a def Stable(A1,A2):
    ans="Stable"
    for i in range(len(A1)):
        if A1[i][1:]==A2[i][1:] and A1[i][:1]!=A2[i][:1]:
            ans="Not stable"
    return ans def Stable(A1,A2):
    ans="Stable"
    for i in range(len(A1)):
        if A1[i][1:]==A2[i][1:] and A1[i][:1]!=A2[i][:1]:
            ans="Not stable"
    return ans Stable
#coding:UTF-8
def BS(N,A):
    for i in range(N):
        for j in reversed(range(i+1,N)):
            if int(A[j][1:])<int(A[j-1][1:]):
                swap=A[j]
                A[j]=A[j-1]
                A[j-1]=swap
    return A

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

def Stable(A1,A2):
    ans="Stable"
    for i in range(len(A1)):
        if A1[i][1:]==A2[i][1:] and A1[i][:1]!=A2[i][:1]:
            ans="Not stable"
    return ans

def SS2(N,A,B):
    A1=BS(N,A)
    print(" ".join(A1))
    print("Stable")
    A2=SS(N,B)
    print(" ".join(A2))
    print(Stable(A1,A2))

if __name__=="__main__":
    N=int(input())
    a=input()
    A=a.split(" ")
    B=a.split(" ")
    SS2(N,A,B)