#ALDS1_2-C Sort 1 - Stable Sort
def selectionSort(A,N):
    c=0
    for i in range(N):
        minj = i
        j=i
        while j<N:
            if(int(A[j][1])<int(A[minj][1])):
                minj = j
            j+=1
        if(i!=minj):
            A[i],A[minj]=A[minj],A[i]
            c+=1
    return c

def bubbleSort(A,N):
    c=0
    flag=1
    while flag:
        flag=0
        j=N-1
        while j>0:
            if(int(A[j][1])<int(A[j-1][1])):
                c+=1
                A[j],A[j-1]=A[j-1],A[j]
                flag=1
            j-=1
    return c

N=int(input())
A=input().split()
checkA=[""]*9
for i in range(N):
    checkA[int(A[i][1])-1]+=A[i][0]

B=[]+A
C=[]+A

bubbleSort(B,N)
checkB=[""]*9
for i in range(N):
    checkB[int(B[i][1])-1]+=B[i][0]
S=""
for i in range(N-1):
    S+=B[i]+" "
S+=str(B[-1])
print(S)
if(checkB==checkA):
    print("Stable")
else:
    print("Not stable")
    
selectionSort(C,N)
checkC=[""]*9
for i in range(N):
    checkC[int(C[i][1])-1]+=C[i][0]
S=""
for i in range(N-1):
    S+=C[i]+" "
S+=str(C[-1])
print(S)
if(checkC==checkA):
    print("Stable")
else:
    print("Not stable") def selectionSort(A,N):
    c=0
    for i in range(N):
        minj = i
        j=i
        while j<N:
            if(int(A[j][1])<int(A[minj][1])):
                minj = j
            j+=1
        if(i!=minj):
            A[i],A[minj]=A[minj],A[i]
            c+=1
    return c selectionSort c=0 c 0 for i in range(N):
        minj = i
        j=i
        while j<N:
            if(int(A[j][1])<int(A[minj][1])):
                minj = j
            j+=1
        if(i!=minj):
            A[i],A[minj]=A[minj],A[i]
            c+=1
     i range(N) range N minj = i minj i j=i j i while j<N:
            if(int(A[j][1])<int(A[minj][1])):
                minj = j
            j+=1
         j<N j 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 int(A[minj][1]) int A[minj][1] [minj] A minj 1 minj = j minj j j+=1 j 1 if(i!=minj):
            A[i],A[minj]=A[minj],A[i]
            c+=1
     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 c+=1 c 1 return c c A A N N def bubbleSort(A,N):
    c=0
    flag=1
    while flag:
        flag=0
        j=N-1
        while j>0:
            if(int(A[j][1])<int(A[j-1][1])):
                c+=1
                A[j],A[j-1]=A[j-1],A[j]
                flag=1
            j-=1
    return c bubbleSort c=0 c 0 flag=1 flag 1 while flag:
        flag=0
        j=N-1
        while j>0:
            if(int(A[j][1])<int(A[j-1][1])):
                c+=1
                A[j],A[j-1]=A[j-1],A[j]
                flag=1
            j-=1
     flag flag=0 flag 0 j=N-1 j N-1 N 1 while j>0:
            if(int(A[j][1])<int(A[j-1][1])):
                c+=1
                A[j],A[j-1]=A[j-1],A[j]
                flag=1
            j-=1
     j>0 j 0 if(int(A[j][1])<int(A[j-1][1])):
                c+=1
                A[j],A[j-1]=A[j-1],A[j]
                flag=1
             int(A[j][1])<int(A[j-1][1]) int(A[j][1]) int A[j][1] [j] A j 1 int(A[j-1][1]) int A[j-1][1] [j-1] A j-1 j 1 1 c+=1 c 1 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 j-=1 j 1 return c c A A N N N=int(input()) N int(input()) int input() input A=input().split() A input().split() ().split () input split checkA=[""]*9 checkA [""]*9 [""] "" 9 for i in range(N):
    checkA[int(A[i][1])-1]+=A[i][0] i range(N) range N checkA[int(A[i][1])-1]+=A[i][0] checkA[int(A[i][1])-1] checkA int(A[i][1])-1 int(A[i][1]) int A[i][1] [i] A i 1 1 A[i][0] [i] A i 0 B=[]+A B []+A [] A C=[]+A C []+A [] A bubbleSort(B,N) bubbleSort B N checkB=[""]*9 checkB [""]*9 [""] "" 9 for i in range(N):
    checkB[int(B[i][1])-1]+=B[i][0] i range(N) range N checkB[int(B[i][1])-1]+=B[i][0] checkB[int(B[i][1])-1] checkB int(B[i][1])-1 int(B[i][1]) int B[i][1] [i] B i 1 1 B[i][0] [i] B i 0 S="" S "" for i in range(N-1):
    S+=B[i]+" " i range(N-1) range N-1 N 1 S+=B[i]+" " S B[i]+" " B[i] B i " " S+=str(B[-1]) S str(B[-1]) str B[-1] B -1 print(S) print S if(checkB==checkA):
    print("Stable")
else:
    print("Not stable")
     checkB==checkA checkB checkA print("Stable") print "Stable" print("Not stable") print "Not stable" selectionSort(C,N) selectionSort C N checkC=[""]*9 checkC [""]*9 [""] "" 9 for i in range(N):
    checkC[int(C[i][1])-1]+=C[i][0] i range(N) range N checkC[int(C[i][1])-1]+=C[i][0] checkC[int(C[i][1])-1] checkC int(C[i][1])-1 int(C[i][1]) int C[i][1] [i] C i 1 1 C[i][0] [i] C i 0 S="" S "" for i in range(N-1):
    S+=C[i]+" " i range(N-1) range N-1 N 1 S+=C[i]+" " S C[i]+" " C[i] C i " " S+=str(C[-1]) S str(C[-1]) str C[-1] C -1 print(S) print S if(checkC==checkA):
    print("Stable")
else:
    print("Not stable") checkC==checkA checkC checkA print("Stable") print "Stable" print("Not stable") print "Not stable" checkC=[""]*9 [""]*9 checkC S+=str(B[-1]) str(B[-1]) S S="" "" S S="" "" S checkA=[""]*9 [""]*9 checkA checkB=[""]*9 [""]*9 checkB S+=B[i]+" " B[i]+" " S A=input().split() input().split() A C=[]+A []+A C B=[]+A []+A B def bubbleSort(A,N):
    c=0
    flag=1
    while flag:
        flag=0
        j=N-1
        while j>0:
            if(int(A[j][1])<int(A[j-1][1])):
                c+=1
                A[j],A[j-1]=A[j-1],A[j]
                flag=1
            j-=1
    return c def bubbleSort(A,N):
    c=0
    flag=1
    while flag:
        flag=0
        j=N-1
        while j>0:
            if(int(A[j][1])<int(A[j-1][1])):
                c+=1
                A[j],A[j-1]=A[j-1],A[j]
                flag=1
            j-=1
    return c bubbleSort N=int(input()) int(input()) N S+=str(C[-1]) str(C[-1]) S S+=C[i]+" " C[i]+" " S def selectionSort(A,N):
    c=0
    for i in range(N):
        minj = i
        j=i
        while j<N:
            if(int(A[j][1])<int(A[minj][1])):
                minj = j
            j+=1
        if(i!=minj):
            A[i],A[minj]=A[minj],A[i]
            c+=1
    return c def selectionSort(A,N):
    c=0
    for i in range(N):
        minj = i
        j=i
        while j<N:
            if(int(A[j][1])<int(A[minj][1])):
                minj = j
            j+=1
        if(i!=minj):
            A[i],A[minj]=A[minj],A[i]
            c+=1
    return c selectionSort
#ALDS1_2-C Sort 1 - Stable Sort
def selectionSort(A,N):
    c=0
    for i in range(N):
        minj = i
        j=i
        while j<N:
            if(int(A[j][1])<int(A[minj][1])):
                minj = j
            j+=1
        if(i!=minj):
            A[i],A[minj]=A[minj],A[i]
            c+=1
    return c

def bubbleSort(A,N):
    c=0
    flag=1
    while flag:
        flag=0
        j=N-1
        while j>0:
            if(int(A[j][1])<int(A[j-1][1])):
                c+=1
                A[j],A[j-1]=A[j-1],A[j]
                flag=1
            j-=1
    return c

N=int(input())
A=input().split()
checkA=[""]*9
for i in range(N):
    checkA[int(A[i][1])-1]+=A[i][0]

B=[]+A
C=[]+A

bubbleSort(B,N)
checkB=[""]*9
for i in range(N):
    checkB[int(B[i][1])-1]+=B[i][0]
S=""
for i in range(N-1):
    S+=B[i]+" "
S+=str(B[-1])
print(S)
if(checkB==checkA):
    print("Stable")
else:
    print("Not stable")
    
selectionSort(C,N)
checkC=[""]*9
for i in range(N):
    checkC[int(C[i][1])-1]+=C[i][0]
S=""
for i in range(N-1):
    S+=C[i]+" "
S+=str(C[-1])
print(S)
if(checkC==checkA):
    print("Stable")
else:
    print("Not stable")