#愚直な判定
def JudgeStable(inC, outC, N):
    for i in range(N):
        for j in range(i+1, N):
            for a in range(N):
                for b in range(a+1, N):
                    if inC[i][1] == inC[j][1] and inC[i] == outC[b] and inC[j] == outC[a]:
                        return False
    return True

def PrintJudge(judge):
    if judge:
        print('Stable')
    else:
        print('Not stable')
        
def BubbleSort(C, N):
    for i in range(N):
        for j in reversed(range(i+1, N)):
            if C[j-1][1] > C[j][1]:
                C[j-1], C[j] = C[j], C[j-1]
                
def SelectionSort(C, N):
    for i in range(N):
        minj = i
        for j in range(i+1, N):
            if C[minj][1] > C[j][1]:
                minj = j
        C[minj], C[i] = C[i], C[minj]
                
def PrintC(C):
    print(' '.join([''.join([c[0], str(c[1])]) for c in C]))

N = int(input())
C = [[c[0], int(c[1])] for c in input().split()]
C1 = C.copy()
C2 = C.copy()

BubbleSort(C1, N)
SelectionSort(C2, N)

PrintC(C1)
PrintJudge(JudgeStable(C, C1, N))

PrintC(C2)
PrintJudge(JudgeStable(C, C2, N)) def JudgeStable(inC, outC, N):
    for i in range(N):
        for j in range(i+1, N):
            for a in range(N):
                for b in range(a+1, N):
                    if inC[i][1] == inC[j][1] and inC[i] == outC[b] and inC[j] == outC[a]:
                        return False
    return True JudgeStable for i in range(N):
        for j in range(i+1, N):
            for a in range(N):
                for b in range(a+1, N):
                    if inC[i][1] == inC[j][1] and inC[i] == outC[b] and inC[j] == outC[a]:
                        return False
     i range(N) range N for j in range(i+1, N):
            for a in range(N):
                for b in range(a+1, N):
                    if inC[i][1] == inC[j][1] and inC[i] == outC[b] and inC[j] == outC[a]:
                        return False
     j range(i+1, N) range i+1 i 1 N for a in range(N):
                for b in range(a+1, N):
                    if inC[i][1] == inC[j][1] and inC[i] == outC[b] and inC[j] == outC[a]:
                        return False
     a range(N) range N for b in range(a+1, N):
                    if inC[i][1] == inC[j][1] and inC[i] == outC[b] and inC[j] == outC[a]:
                        return False
     b range(a+1, N) range a+1 a 1 N if inC[i][1] == inC[j][1] and inC[i] == outC[b] and inC[j] == outC[a]:
                        return False
     inC[i][1] == inC[j][1] and inC[i] == outC[b] and inC[j] == outC[a] inC[i][1] == inC[j][1] and inC[i] == outC[b] inC[i][1] == inC[j][1] inC[i][1] [i] inC i 1 inC[j][1] [j] inC j 1 inC[i] == outC[b] inC[i] inC i outC[b] outC b inC[j] == outC[a] inC[j] inC j outC[a] outC a return False False return True True inC inC outC outC N N def PrintJudge(judge):
    if judge:
        print('Stable')
    else:
        print('Not stable')
         PrintJudge if judge:
        print('Stable')
    else:
        print('Not stable')
         judge print('Stable') print 'Stable' print('Not stable') print 'Not stable' judge judge def BubbleSort(C, N):
    for i in range(N):
        for j in reversed(range(i+1, N)):
            if C[j-1][1] > C[j][1]:
                C[j-1], C[j] = C[j], C[j-1]
                 BubbleSort for i in range(N):
        for j in reversed(range(i+1, N)):
            if C[j-1][1] > C[j][1]:
                C[j-1], C[j] = C[j], C[j-1]
                 i range(N) range N for j in reversed(range(i+1, N)):
            if C[j-1][1] > C[j][1]:
                C[j-1], C[j] = C[j], C[j-1]
                 j reversed(range(i+1, N)) reversed range(i+1, N) range i+1 i 1 N if C[j-1][1] > C[j][1]:
                C[j-1], C[j] = C[j], C[j-1]
                 C[j-1][1] > C[j][1] C[j-1][1] [j-1] C j-1 j 1 1 C[j][1] [j] C j 1 C[j-1], C[j] = C[j], C[j-1] C[j-1] C j-1 j 1 C[j] C j C[j] C j C[j-1] C j-1 j 1 C C N N def SelectionSort(C, N):
    for i in range(N):
        minj = i
        for j in range(i+1, N):
            if C[minj][1] > C[j][1]:
                minj = j
        C[minj], C[i] = C[i], C[minj]
                 SelectionSort for i in range(N):
        minj = i
        for j in range(i+1, N):
            if C[minj][1] > C[j][1]:
                minj = j
        C[minj], C[i] = C[i], C[minj]
                 i range(N) range N minj = i minj i for j in range(i+1, N):
            if C[minj][1] > C[j][1]:
                minj = j
         j range(i+1, N) range i+1 i 1 N if C[minj][1] > C[j][1]:
                minj = j
         C[minj][1] > C[j][1] C[minj][1] [minj] C minj 1 C[j][1] [j] C j 1 minj = j minj j C[minj], C[i] = C[i], C[minj] C[minj] C minj C[i] C i C[i] C i C[minj] C minj C C N N def PrintC(C):
    print(' '.join([''.join([c[0], str(c[1])]) for c in C])) PrintC ''.join([c[0], str(c[1])]) for c in C for c in C c C for c in C ''.join([c[0], str(c[1])]) ''.join '' join [c[0], str(c[1])] c[0] c 0 str(c[1]) str c[1] c 1 print(' '.join([''.join([c[0], str(c[1])]) for c in C])) print ' '.join([''.join([c[0], str(c[1])]) for c in C]) ' '.join ' ' join [''.join([c[0], str(c[1])]) for c in C] C C N = int(input()) N int(input()) int input() input [c[0], int(c[1])] for c in input().split() for c in input().split() c input().split() ().split () input split for c in input().split() [c[0], int(c[1])] c[0] c 0 int(c[1]) int c[1] c 1 C = [[c[0], int(c[1])] for c in input().split()] C [[c[0], int(c[1])] for c in input().split()] C1 = C.copy() C1 C.copy() C.copy C copy C2 = C.copy() C2 C.copy() C.copy C copy BubbleSort(C1, N) BubbleSort C1 N SelectionSort(C2, N) SelectionSort C2 N PrintC(C1) PrintC C1 PrintJudge(JudgeStable(C, C1, N)) PrintJudge JudgeStable(C, C1, N) JudgeStable C C1 N PrintC(C2) PrintC C2 PrintJudge(JudgeStable(C, C2, N)) PrintJudge JudgeStable(C, C2, N) JudgeStable C C2 N def SelectionSort(C, N):
    for i in range(N):
        minj = i
        for j in range(i+1, N):
            if C[minj][1] > C[j][1]:
                minj = j
        C[minj], C[i] = C[i], C[minj]
                 def SelectionSort(C, N):
    for i in range(N):
        minj = i
        for j in range(i+1, N):
            if C[minj][1] > C[j][1]:
                minj = j
        C[minj], C[i] = C[i], C[minj]
                 SelectionSort def JudgeStable(inC, outC, N):
    for i in range(N):
        for j in range(i+1, N):
            for a in range(N):
                for b in range(a+1, N):
                    if inC[i][1] == inC[j][1] and inC[i] == outC[b] and inC[j] == outC[a]:
                        return False
    return True def JudgeStable(inC, outC, N):
    for i in range(N):
        for j in range(i+1, N):
            for a in range(N):
                for b in range(a+1, N):
                    if inC[i][1] == inC[j][1] and inC[i] == outC[b] and inC[j] == outC[a]:
                        return False
    return True JudgeStable def PrintJudge(judge):
    if judge:
        print('Stable')
    else:
        print('Not stable')
         def PrintJudge(judge):
    if judge:
        print('Stable')
    else:
        print('Not stable')
         PrintJudge def BubbleSort(C, N):
    for i in range(N):
        for j in reversed(range(i+1, N)):
            if C[j-1][1] > C[j][1]:
                C[j-1], C[j] = C[j], C[j-1]
                 def BubbleSort(C, N):
    for i in range(N):
        for j in reversed(range(i+1, N)):
            if C[j-1][1] > C[j][1]:
                C[j-1], C[j] = C[j], C[j-1]
                 BubbleSort C1 = C.copy() C.copy() C1 C2 = C.copy() C.copy() C2 def PrintC(C):
    print(' '.join([''.join([c[0], str(c[1])]) for c in C])) def PrintC(C):
    print(' '.join([''.join([c[0], str(c[1])]) for c in C])) PrintC C = [[c[0], int(c[1])] for c in input().split()] [[c[0], int(c[1])] for c in input().split()] C N = int(input()) int(input()) N
#愚直な判定
def JudgeStable(inC, outC, N):
    for i in range(N):
        for j in range(i+1, N):
            for a in range(N):
                for b in range(a+1, N):
                    if inC[i][1] == inC[j][1] and inC[i] == outC[b] and inC[j] == outC[a]:
                        return False
    return True

def PrintJudge(judge):
    if judge:
        print('Stable')
    else:
        print('Not stable')
        
def BubbleSort(C, N):
    for i in range(N):
        for j in reversed(range(i+1, N)):
            if C[j-1][1] > C[j][1]:
                C[j-1], C[j] = C[j], C[j-1]
                
def SelectionSort(C, N):
    for i in range(N):
        minj = i
        for j in range(i+1, N):
            if C[minj][1] > C[j][1]:
                minj = j
        C[minj], C[i] = C[i], C[minj]
                
def PrintC(C):
    print(' '.join([''.join([c[0], str(c[1])]) for c in C]))

N = int(input())
C = [[c[0], int(c[1])] for c in input().split()]
C1 = C.copy()
C2 = C.copy()

BubbleSort(C1, N)
SelectionSort(C2, N)

PrintC(C1)
PrintJudge(JudgeStable(C, C1, N))

PrintC(C2)
PrintJudge(JudgeStable(C, C2, N))
