def compare(s1, s2):
    if s1[1] > s2[1]:
        return 1
    elif s1[1] < s2[1]:
        return -1

    return 0

def bubbleSort(N, A):
    flag = True
    while flag:
        flag = False
        for i in xrange(N-1, 0, -1):
            if compare(A[i-1], A[i]) > 0:
                A[i-1], A[i] = A[i], A[i-1]
                flag = True

def selectSort(N, A):
    for i in xrange(N):
        minj = i
        for j in xrange(i, N):
            if compare(A[j], A[minj]) < 0:
                minj = j
        if minj != i:
            A[i], A[minj] = A[minj], A[i]

N = int(raw_input())
A = raw_input().split()
B = list(A)
bubbleSort(N, A)
print ' '.join(A)
print 'Stable'
selectSort(N, B)
print ' '.join(B)
if A == B:
    print 'Stable'
else:
    print 'Not stable' def compare(s1, s2):
    if s1[1] > s2[1]:
        return 1
    elif s1[1] < s2[1]:
        return -1

    return 0 compare if s1[1] > s2[1]:
        return 1
    elif s1[1] < s2[1]:
        return -1

     s1[1] > s2[1] s1[1] s1 1 s2[1] s2 1 return 1 1 elif s1[1] < s2[1]:
        return -1

     s1[1] < s2[1] s1[1] s1 1 s2[1] s2 1 return -1 -1 return 0 0 s1 s1 s2 s2 def bubbleSort(N, A):
    flag = True
    while flag:
        flag = False
        for i in xrange(N-1, 0, -1):
            if compare(A[i-1], A[i]) > 0:
                A[i-1], A[i] = A[i], A[i-1]
                flag = True bubbleSort flag = True flag True while flag:
        flag = False
        for i in xrange(N-1, 0, -1):
            if compare(A[i-1], A[i]) > 0:
                A[i-1], A[i] = A[i], A[i-1]
                flag = True flag flag = False flag False for i in xrange(N-1, 0, -1):
            if compare(A[i-1], A[i]) > 0:
                A[i-1], A[i] = A[i], A[i-1]
                flag = True i xrange(N-1, 0, -1) xrange N-1 N 1 0 -1 if compare(A[i-1], A[i]) > 0:
                A[i-1], A[i] = A[i], A[i-1]
                flag = True compare(A[i-1], A[i]) > 0 compare(A[i-1], A[i]) compare A[i-1] A i-1 i 1 A[i] A i 0 A[i-1], A[i] = A[i], A[i-1] A[i-1] A i-1 i 1 A[i] A i A[i] A i A[i-1] A i-1 i 1 flag = True flag True N N A A def selectSort(N, A):
    for i in xrange(N):
        minj = i
        for j in xrange(i, N):
            if compare(A[j], A[minj]) < 0:
                minj = j
        if minj != i:
            A[i], A[minj] = A[minj], A[i] selectSort for i in xrange(N):
        minj = i
        for j in xrange(i, N):
            if compare(A[j], A[minj]) < 0:
                minj = j
        if minj != i:
            A[i], A[minj] = A[minj], A[i] i xrange(N) xrange N minj = i minj i for j in xrange(i, N):
            if compare(A[j], A[minj]) < 0:
                minj = j
         j xrange(i, N) xrange i N if compare(A[j], A[minj]) < 0:
                minj = j
         compare(A[j], A[minj]) < 0 compare(A[j], A[minj]) compare A[j] A j A[minj] A minj 0 minj = j minj j if minj != i:
            A[i], A[minj] = A[minj], A[i] minj != i minj i A[i], A[minj] = A[minj], A[i] A[i] A i A[minj] A minj A[minj] A minj A[i] A i N N A A N = int(raw_input()) N int(raw_input()) int raw_input() raw_input A = raw_input().split() A raw_input().split() ().split () raw_input split B = list(A) B list(A) list A bubbleSort(N, A) bubbleSort N A print ' '.join(A) ' '.join ' ' join A print selectSort(N, B) selectSort N B print ' '.join(B) ' '.join ' ' join B if A == B:
    print 'Stable'
else:
    print 'Not stable' A == B A B print print B = list(A) list(A) B def compare(s1, s2):
    if s1[1] > s2[1]:
        return 1
    elif s1[1] < s2[1]:
        return -1

    return 0 def compare(s1, s2):
    if s1[1] > s2[1]:
        return 1
    elif s1[1] < s2[1]:
        return -1

    return 0 compare def selectSort(N, A):
    for i in xrange(N):
        minj = i
        for j in xrange(i, N):
            if compare(A[j], A[minj]) < 0:
                minj = j
        if minj != i:
            A[i], A[minj] = A[minj], A[i] def selectSort(N, A):
    for i in xrange(N):
        minj = i
        for j in xrange(i, N):
            if compare(A[j], A[minj]) < 0:
                minj = j
        if minj != i:
            A[i], A[minj] = A[minj], A[i] selectSort def bubbleSort(N, A):
    flag = True
    while flag:
        flag = False
        for i in xrange(N-1, 0, -1):
            if compare(A[i-1], A[i]) > 0:
                A[i-1], A[i] = A[i], A[i-1]
                flag = True def bubbleSort(N, A):
    flag = True
    while flag:
        flag = False
        for i in xrange(N-1, 0, -1):
            if compare(A[i-1], A[i]) > 0:
                A[i-1], A[i] = A[i], A[i-1]
                flag = True bubbleSort N = int(raw_input()) int(raw_input()) N A = raw_input().split() raw_input().split() A
def compare(s1, s2):
    if s1[1] > s2[1]:
        return 1
    elif s1[1] < s2[1]:
        return -1

    return 0

def bubbleSort(N, A):
    flag = True
    while flag:
        flag = False
        for i in xrange(N-1, 0, -1):
            if compare(A[i-1], A[i]) > 0:
                A[i-1], A[i] = A[i], A[i-1]
                flag = True

def selectSort(N, A):
    for i in xrange(N):
        minj = i
        for j in xrange(i, N):
            if compare(A[j], A[minj]) < 0:
                minj = j
        if minj != i:
            A[i], A[minj] = A[minj], A[i]

N = int(raw_input())
A = raw_input().split()
B = list(A)
bubbleSort(N, A)
print ' '.join(A)
print 'Stable'
selectSort(N, B)
print ' '.join(B)
if A == B:
    print 'Stable'
else:
    print 'Not stable'