import copy

N = int(input())

def hoge(arg):
    return [arg[0], int(arg[1])]

A = list(map(hoge, map(str, input().split())))
A1 = copy.deepcopy(A)
A2 = copy.deepcopy(A)

def bsort(C, N):
    for i in range(N):
        for j in range(N-1, i, -1):
            if C[j][1] < C[j-1][1]:
                C[j], C[j-1] = C[j-1], C[j]

def ssort(C, N):
    for i in range(N):
        minj = i
        for j in range(i, N, 1):
            if C[j][1] < C[minj][1]:
                minj = j
        C[i], C[minj] = C[minj], C[i]

def judge(A, A1):
    for i in range(len(A) - 1):
        if A1[i][1] != A1[i+1][1]:
            continue
        a = A.index(A1[i])
        b = A.index(A1[i+1])
        if a > b:
            return 'Not stable'
    return 'Stable'

def toString(A):
    return A[0] + str(A[1])

bsort(A1, N)
print(' '.join(list(map(toString, A1))))
print(judge(A, A1))
ssort(A2, N)
print(' '.join(list(map(toString, A2))))
print(judge(A, A2))
 import copy copy N = int(input()) N int(input()) int input() input def hoge(arg):
    return [arg[0], int(arg[1])] hoge return [arg[0], int(arg[1])] [arg[0], int(arg[1])] arg[0] arg 0 int(arg[1]) int arg[1] arg 1 arg arg A = list(map(hoge, map(str, input().split()))) A list(map(hoge, map(str, input().split()))) list map(hoge, map(str, input().split())) map hoge map(str, input().split()) map str input().split() ().split () input split A1 = copy.deepcopy(A) A1 copy.deepcopy(A) copy.deepcopy copy deepcopy A A2 = copy.deepcopy(A) A2 copy.deepcopy(A) copy.deepcopy copy deepcopy A def bsort(C, N):
    for i in range(N):
        for j in range(N-1, i, -1):
            if C[j][1] < C[j-1][1]:
                C[j], C[j-1] = C[j-1], C[j] bsort for i in range(N):
        for j in range(N-1, i, -1):
            if C[j][1] < C[j-1][1]:
                C[j], C[j-1] = C[j-1], C[j] i range(N) range N for j in range(N-1, i, -1):
            if C[j][1] < C[j-1][1]:
                C[j], C[j-1] = C[j-1], C[j] j range(N-1, i, -1) range N-1 N 1 i -1 if C[j][1] < C[j-1][1]:
                C[j], C[j-1] = C[j-1], C[j] C[j][1] < C[j-1][1] C[j][1] [j] C j 1 C[j-1][1] [j-1] C j-1 j 1 1 C[j], C[j-1] = C[j-1], C[j] C[j] C j C[j-1] C j-1 j 1 C[j-1] C j-1 j 1 C[j] C j C C N N def ssort(C, N):
    for i in range(N):
        minj = i
        for j in range(i, N, 1):
            if C[j][1] < C[minj][1]:
                minj = j
        C[i], C[minj] = C[minj], C[i] ssort for i in range(N):
        minj = i
        for j in range(i, N, 1):
            if C[j][1] < C[minj][1]:
                minj = j
        C[i], C[minj] = C[minj], C[i] i range(N) range N minj = i minj i for j in range(i, N, 1):
            if C[j][1] < C[minj][1]:
                minj = j
         j range(i, N, 1) range i N 1 if C[j][1] < C[minj][1]:
                minj = j
         C[j][1] < C[minj][1] C[j][1] [j] C j 1 C[minj][1] [minj] C minj 1 minj = j minj j C[i], C[minj] = C[minj], C[i] C[i] C i C[minj] C minj C[minj] C minj C[i] C i C C N N def judge(A, A1):
    for i in range(len(A) - 1):
        if A1[i][1] != A1[i+1][1]:
            continue
        a = A.index(A1[i])
        b = A.index(A1[i+1])
        if a > b:
            return 'Not stable'
    return 'Stable' judge for i in range(len(A) - 1):
        if A1[i][1] != A1[i+1][1]:
            continue
        a = A.index(A1[i])
        b = A.index(A1[i+1])
        if a > b:
            return 'Not stable'
     i range(len(A) - 1) range len(A) - 1 len(A) len A 1 if A1[i][1] != A1[i+1][1]:
            continue
         A1[i][1] != A1[i+1][1] A1[i][1] [i] A1 i 1 A1[i+1][1] [i+1] A1 i+1 i 1 1 continue a = A.index(A1[i]) a A.index(A1[i]) A.index A index A1[i] A1 i b = A.index(A1[i+1]) b A.index(A1[i+1]) A.index A index A1[i+1] A1 i+1 i 1 if a > b:
            return 'Not stable'
     a > b a b return 'Not stable' 'Not stable' return 'Stable' 'Stable' A A A1 A1 def toString(A):
    return A[0] + str(A[1]) toString return A[0] + str(A[1]) A[0] + str(A[1]) A[0] A 0 str(A[1]) str A[1] A 1 A A bsort(A1, N) bsort A1 N print(' '.join(list(map(toString, A1)))) print ' '.join(list(map(toString, A1))) ' '.join ' ' join list(map(toString, A1)) list map(toString, A1) map toString A1 print(judge(A, A1)) print judge(A, A1) judge A A1 ssort(A2, N) ssort A2 N print(' '.join(list(map(toString, A2)))) print ' '.join(list(map(toString, A2))) ' '.join ' ' join list(map(toString, A2)) list map(toString, A2) map toString A2 print(judge(A, A2)) print judge(A, A2) judge A A2 def toString(A):
    return A[0] + str(A[1]) def toString(A):
    return A[0] + str(A[1]) toString A1 = copy.deepcopy(A) copy.deepcopy(A) A1 def judge(A, A1):
    for i in range(len(A) - 1):
        if A1[i][1] != A1[i+1][1]:
            continue
        a = A.index(A1[i])
        b = A.index(A1[i+1])
        if a > b:
            return 'Not stable'
    return 'Stable' def judge(A, A1):
    for i in range(len(A) - 1):
        if A1[i][1] != A1[i+1][1]:
            continue
        a = A.index(A1[i])
        b = A.index(A1[i+1])
        if a > b:
            return 'Not stable'
    return 'Stable' judge def ssort(C, N):
    for i in range(N):
        minj = i
        for j in range(i, N, 1):
            if C[j][1] < C[minj][1]:
                minj = j
        C[i], C[minj] = C[minj], C[i] def ssort(C, N):
    for i in range(N):
        minj = i
        for j in range(i, N, 1):
            if C[j][1] < C[minj][1]:
                minj = j
        C[i], C[minj] = C[minj], C[i] ssort A = list(map(hoge, map(str, input().split()))) list(map(hoge, map(str, input().split()))) A def bsort(C, N):
    for i in range(N):
        for j in range(N-1, i, -1):
            if C[j][1] < C[j-1][1]:
                C[j], C[j-1] = C[j-1], C[j] def bsort(C, N):
    for i in range(N):
        for j in range(N-1, i, -1):
            if C[j][1] < C[j-1][1]:
                C[j], C[j-1] = C[j-1], C[j] bsort N = int(input()) int(input()) N A2 = copy.deepcopy(A) copy.deepcopy(A) A2 def hoge(arg):
    return [arg[0], int(arg[1])] def hoge(arg):
    return [arg[0], int(arg[1])] hoge
import copy

N = int(input())

def hoge(arg):
    return [arg[0], int(arg[1])]

A = list(map(hoge, map(str, input().split())))
A1 = copy.deepcopy(A)
A2 = copy.deepcopy(A)

def bsort(C, N):
    for i in range(N):
        for j in range(N-1, i, -1):
            if C[j][1] < C[j-1][1]:
                C[j], C[j-1] = C[j-1], C[j]

def ssort(C, N):
    for i in range(N):
        minj = i
        for j in range(i, N, 1):
            if C[j][1] < C[minj][1]:
                minj = j
        C[i], C[minj] = C[minj], C[i]

def judge(A, A1):
    for i in range(len(A) - 1):
        if A1[i][1] != A1[i+1][1]:
            continue
        a = A.index(A1[i])
        b = A.index(A1[i+1])
        if a > b:
            return 'Not stable'
    return 'Stable'

def toString(A):
    return A[0] + str(A[1])

bsort(A1, N)
print(' '.join(list(map(toString, A1))))
print(judge(A, A1))
ssort(A2, N)
print(' '.join(list(map(toString, A2))))
print(judge(A, A2))

