# -*- coding: utf-8 -*-

class Card:
    def __init__(self, str):
        self.suit  = str[0]
        self.value = int(str[1])

    def __str__(self):
        return self.suit + str(self.value)

def swap(A, i, j):
    tmp  = A[i]
    A[i] = A[j]
    A[j] = tmp

def BubbleSort(C, N):
    for i in range(N):
        for j in range(N-1, i, -1):
            if C[j].value < C[j-1].value:
                swap(C, j, j-1)  # C[j] と C[j-1] を交換

def SelectionSort(C, N):
    for i in range(N):
        minj = i
        for j in range(i, N):
            if C[j].value < C[minj].value:
                minj = j
        swap(C, i, minj)  # C[i] と C[minj] を交換

def isStable(A, B):
    for i in range(1, len(B)):
        if B[i-1].value == B[i].value:
            for j in range(len(A)):
                if A[j] == B[i-1]: break
                if A[j] == B[i]:   return False
    return True

N = int(input())
A = [Card(t) for t in input().split()]
if len(A) != N: raise

B = A[:]
S = A[:]
BubbleSort(B, N)
SelectionSort(S, N)
print(' '.join(map(str, B)))
print('Stable' if isStable(A, B) else 'Not stable')
print(' '.join(map(str, S)))
print('Stable' if isStable(A, S) else 'Not stable') class Card:
    def __init__(self, str):
        self.suit  = str[0]
        self.value = int(str[1])

    def __str__(self):
        return self.suit + str(self.value) Card def __init__(self, str):
        self.suit  = str[0]
        self.value = int(str[1])

     __init__ self.suit  = str[0] self.suit self suit str[0] str 0 self.value = int(str[1]) self.value self value int(str[1]) int str[1] str 1 self self str str def __str__(self):
        return self.suit + str(self.value) __str__ return self.suit + str(self.value) self.suit + str(self.value) self.suit self suit str(self.value) str self.value self value self self def swap(A, i, j):
    tmp  = A[i]
    A[i] = A[j]
    A[j] = tmp swap tmp  = A[i] tmp A[i] A i A[i] = A[j] A[i] A i A[j] A j A[j] = tmp A[j] A j tmp A A i i j j def BubbleSort(C, N):
    for i in range(N):
        for j in range(N-1, i, -1):
            if C[j].value < C[j-1].value:
                swap(C, j, j-1)  # C[j] と C[j-1] を交換 BubbleSort for i in range(N):
        for j in range(N-1, i, -1):
            if C[j].value < C[j-1].value:
                swap(C, j, j-1)  # C[j] と C[j-1] を交換 i range(N) range N for j in range(N-1, i, -1):
            if C[j].value < C[j-1].value:
                swap(C, j, j-1)  # C[j] と C[j-1] を交換 j range(N-1, i, -1) range N-1 N 1 i -1 if C[j].value < C[j-1].value:
                swap(C, j, j-1)  # C[j] と C[j-1] を交換 C[j].value < C[j-1].value C[j].value [j] C j value C[j-1].value [j-1] C j-1 j 1 value swap(C, j, j-1) swap C j j-1 j 1 C C N N def SelectionSort(C, N):
    for i in range(N):
        minj = i
        for j in range(i, N):
            if C[j].value < C[minj].value:
                minj = j
        swap(C, i, minj)  # C[i] と C[minj] を交換 SelectionSort for i in range(N):
        minj = i
        for j in range(i, N):
            if C[j].value < C[minj].value:
                minj = j
        swap(C, i, minj)  # C[i] と C[minj] を交換 i range(N) range N minj = i minj i for j in range(i, N):
            if C[j].value < C[minj].value:
                minj = j
         j range(i, N) range i N if C[j].value < C[minj].value:
                minj = j
         C[j].value < C[minj].value C[j].value [j] C j value C[minj].value [minj] C minj value minj = j minj j swap(C, i, minj) swap C i minj C C N N def isStable(A, B):
    for i in range(1, len(B)):
        if B[i-1].value == B[i].value:
            for j in range(len(A)):
                if A[j] == B[i-1]: break
                if A[j] == B[i]:   return False
    return True isStable for i in range(1, len(B)):
        if B[i-1].value == B[i].value:
            for j in range(len(A)):
                if A[j] == B[i-1]: break
                if A[j] == B[i]:   return False
     i range(1, len(B)) range 1 len(B) len B if B[i-1].value == B[i].value:
            for j in range(len(A)):
                if A[j] == B[i-1]: break
                if A[j] == B[i]:   return False
     B[i-1].value == B[i].value B[i-1].value [i-1] B i-1 i 1 value B[i].value [i] B i value for j in range(len(A)):
                if A[j] == B[i-1]: break
                if A[j] == B[i]:   return False
     j range(len(A)) range len(A) len A if A[j] == B[i-1]: break
                 A[j] == B[i-1] A[j] A j B[i-1] B i-1 i 1 if A[j] == B[i]:   return False
     A[j] == B[i] A[j] A j B[i] B i return True True A A B B N = int(input()) N int(input()) int input() input Card(t) for t in input().split() for t in input().split() t input().split() ().split () input split for t in input().split() Card(t) Card t A = [Card(t) for t in input().split()] A [Card(t) for t in input().split()] if len(A) != N: raise len(A) != N len(A) len A N B = A[:] B A[:] A : S = A[:] S A[:] A : BubbleSort(B, N) BubbleSort B N SelectionSort(S, N) SelectionSort S N print(' '.join(map(str, B))) print ' '.join(map(str, B)) ' '.join ' ' join map(str, B) map str B print('Stable' if isStable(A, B) else 'Not stable') print 'Stable' if isStable(A, B) else 'Not stable' isStable(A, B) isStable A B 'Stable' 'Not stable' print(' '.join(map(str, S))) print ' '.join(map(str, S)) ' '.join ' ' join map(str, S) map str S print('Stable' if isStable(A, S) else 'Not stable') print 'Stable' if isStable(A, S) else 'Not stable' isStable(A, S) isStable A S 'Stable' 'Not stable' def BubbleSort(C, N):
    for i in range(N):
        for j in range(N-1, i, -1):
            if C[j].value < C[j-1].value:
                swap(C, j, j-1)  # C[j] と C[j-1] を交換 def BubbleSort(C, N):
    for i in range(N):
        for j in range(N-1, i, -1):
            if C[j].value < C[j-1].value:
                swap(C, j, j-1)  # C[j] と C[j-1] を交換 BubbleSort class Card:
    def __init__(self, str):
        self.suit  = str[0]
        self.value = int(str[1])

    def __str__(self):
        return self.suit + str(self.value) class Card:
    def __init__(self, str):
        self.suit  = str[0]
        self.value = int(str[1])

    def __str__(self):
        return self.suit + str(self.value) Card B = A[:] A[:] B def SelectionSort(C, N):
    for i in range(N):
        minj = i
        for j in range(i, N):
            if C[j].value < C[minj].value:
                minj = j
        swap(C, i, minj)  # C[i] と C[minj] を交換 def SelectionSort(C, N):
    for i in range(N):
        minj = i
        for j in range(i, N):
            if C[j].value < C[minj].value:
                minj = j
        swap(C, i, minj)  # C[i] と C[minj] を交換 SelectionSort N = int(input()) int(input()) N def isStable(A, B):
    for i in range(1, len(B)):
        if B[i-1].value == B[i].value:
            for j in range(len(A)):
                if A[j] == B[i-1]: break
                if A[j] == B[i]:   return False
    return True def isStable(A, B):
    for i in range(1, len(B)):
        if B[i-1].value == B[i].value:
            for j in range(len(A)):
                if A[j] == B[i-1]: break
                if A[j] == B[i]:   return False
    return True isStable S = A[:] A[:] S def swap(A, i, j):
    tmp  = A[i]
    A[i] = A[j]
    A[j] = tmp def swap(A, i, j):
    tmp  = A[i]
    A[i] = A[j]
    A[j] = tmp swap A = [Card(t) for t in input().split()] [Card(t) for t in input().split()] A
# -*- coding: utf-8 -*-

class Card:
    def __init__(self, str):
        self.suit  = str[0]
        self.value = int(str[1])

    def __str__(self):
        return self.suit + str(self.value)

def swap(A, i, j):
    tmp  = A[i]
    A[i] = A[j]
    A[j] = tmp

def BubbleSort(C, N):
    for i in range(N):
        for j in range(N-1, i, -1):
            if C[j].value < C[j-1].value:
                swap(C, j, j-1)  # C[j] と C[j-1] を交換

def SelectionSort(C, N):
    for i in range(N):
        minj = i
        for j in range(i, N):
            if C[j].value < C[minj].value:
                minj = j
        swap(C, i, minj)  # C[i] と C[minj] を交換

def isStable(A, B):
    for i in range(1, len(B)):
        if B[i-1].value == B[i].value:
            for j in range(len(A)):
                if A[j] == B[i-1]: break
                if A[j] == B[i]:   return False
    return True

N = int(input())
A = [Card(t) for t in input().split()]
if len(A) != N: raise

B = A[:]
S = A[:]
BubbleSort(B, N)
SelectionSort(S, N)
print(' '.join(map(str, B)))
print('Stable' if isStable(A, B) else 'Not stable')
print(' '.join(map(str, S)))
print('Stable' if isStable(A, S) else 'Not stable')
