import copy

def bubble_sort(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 selection_sort(C, N):
    for i in range(N):
        minj = i
        for j in range(i+1, N):
            if C[j][1] < C[minj][1]:
                minj = j
        C[i], C[minj] = C[minj], C[i]

def is_stable(in0, out0):
    for m in '123456789':
        in1 = list(filter(lambda e:e[1]==m, in0))
        out1 = list(filter(lambda e:e[1]==m, out0))
        #print(in1,out1)
        if in1 != out1:
            return 'Not stable'
    return 'Stable'

N = int(input())
C = list(input().split())
C1 = copy.deepcopy(C)
C2 = copy.deepcopy(C)
bubble_sort(C1, N)
selection_sort(C2, N)
print(*C1)
print(is_stable(C, C1))
print(*C2)
print(is_stable(C, C2))
 import copy copy def bubble_sort(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] bubble_sort 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 selection_sort(C, N):
    for i in range(N):
        minj = i
        for j in range(i+1, N):
            if C[j][1] < C[minj][1]:
                minj = j
        C[i], C[minj] = C[minj], C[i] selection_sort for i in range(N):
        minj = i
        for j in range(i+1, N):
            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+1, N):
            if C[j][1] < C[minj][1]:
                minj = j
         j range(i+1, N) range i+1 i 1 N 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 is_stable(in0, out0):
    for m in '123456789':
        in1 = list(filter(lambda e:e[1]==m, in0))
        out1 = list(filter(lambda e:e[1]==m, out0))
        #print(in1,out1)
        if in1 != out1:
            return 'Not stable'
    return 'Stable' is_stable for m in '123456789':
        in1 = list(filter(lambda e:e[1]==m, in0))
        out1 = list(filter(lambda e:e[1]==m, out0))
        #print(in1,out1)
        if in1 != out1:
            return 'Not stable'
     m '123456789' in1 = list(filter(lambda e:e[1]==m, in0)) in1 list(filter(lambda e:e[1]==m, in0)) list filter(lambda e:e[1]==m, in0) filter lambda e:e[1]==m e[1]==m e[1] e 1 m e in0 out1 = list(filter(lambda e:e[1]==m, out0)) out1 list(filter(lambda e:e[1]==m, out0)) list filter(lambda e:e[1]==m, out0) filter lambda e:e[1]==m e[1]==m e[1] e 1 m e out0 if in1 != out1:
            return 'Not stable'
     in1 != out1 in1 out1 return 'Not stable' 'Not stable' return 'Stable' 'Stable' in0 in0 out0 out0 N = int(input()) N int(input()) int input() input C = list(input().split()) C list(input().split()) list input().split() ().split () input split C1 = copy.deepcopy(C) C1 copy.deepcopy(C) copy.deepcopy copy deepcopy C C2 = copy.deepcopy(C) C2 copy.deepcopy(C) copy.deepcopy copy deepcopy C bubble_sort(C1, N) bubble_sort C1 N selection_sort(C2, N) selection_sort C2 N print(*C1) print *C1 C1 print(is_stable(C, C1)) print is_stable(C, C1) is_stable C C1 print(*C2) print *C2 C2 print(is_stable(C, C2)) print is_stable(C, C2) is_stable C C2 def selection_sort(C, N):
    for i in range(N):
        minj = i
        for j in range(i+1, N):
            if C[j][1] < C[minj][1]:
                minj = j
        C[i], C[minj] = C[minj], C[i] def selection_sort(C, N):
    for i in range(N):
        minj = i
        for j in range(i+1, N):
            if C[j][1] < C[minj][1]:
                minj = j
        C[i], C[minj] = C[minj], C[i] selection_sort def bubble_sort(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 bubble_sort(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] bubble_sort C1 = copy.deepcopy(C) copy.deepcopy(C) C1 C = list(input().split()) list(input().split()) C def is_stable(in0, out0):
    for m in '123456789':
        in1 = list(filter(lambda e:e[1]==m, in0))
        out1 = list(filter(lambda e:e[1]==m, out0))
        #print(in1,out1)
        if in1 != out1:
            return 'Not stable'
    return 'Stable' def is_stable(in0, out0):
    for m in '123456789':
        in1 = list(filter(lambda e:e[1]==m, in0))
        out1 = list(filter(lambda e:e[1]==m, out0))
        #print(in1,out1)
        if in1 != out1:
            return 'Not stable'
    return 'Stable' is_stable C2 = copy.deepcopy(C) copy.deepcopy(C) C2 N = int(input()) int(input()) N
import copy

def bubble_sort(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 selection_sort(C, N):
    for i in range(N):
        minj = i
        for j in range(i+1, N):
            if C[j][1] < C[minj][1]:
                minj = j
        C[i], C[minj] = C[minj], C[i]

def is_stable(in0, out0):
    for m in '123456789':
        in1 = list(filter(lambda e:e[1]==m, in0))
        out1 = list(filter(lambda e:e[1]==m, out0))
        #print(in1,out1)
        if in1 != out1:
            return 'Not stable'
    return 'Stable'

N = int(input())
C = list(input().split())
C1 = copy.deepcopy(C)
C2 = copy.deepcopy(C)
bubble_sort(C1, N)
selection_sort(C2, N)
print(*C1)
print(is_stable(C, C1))
print(*C2)
print(is_stable(C, C2))

