import copy

class Card:
    def __init__(self, suit, number):
        self.suit = suit
        self.number = number
        self.view = self.suit + str(self.number)

def bubble_sort(A):
    count = 0
    while True:
        swapped = False
        for i in range(len(A)-1):
            if A[i+1].number < A[i].number:
                A[i+1], A[i] = A[i], A[i+1]
                count += 1
                swapped = True
        if not swapped:
            return count

def selection_sort(A):
    count = 0
    for i in range(len(A)):
        min_value = A[i].number
        min_value_index = i
        # print('- i:', i, 'A[i]', A[i], '-')
        for j in range(i, len(A)):
            # print('j:', j, 'A[j]:', A[j])
            
            if A[j].number < min_value:
                min_value = A[j].number
                min_value_index = j
        # print('min_value', min_value, 'min_value_index', min_value_index)
        if i != min_value_index:
            count += 1
            A[i], A[min_value_index] = A[min_value_index], A[i]
            # print('swap!', A)
        
    return count

n = int(input())
A = []
for row in input().split():
    suit, number = list(row)
    A.append(Card(suit, int(number)))

def is_stable(A, B):
    N = len(A)
    for i_A in range(N-1):
        for j_A in range(i_A+1, N):
            for i_B in range(N-1):
                for j_B in range(i_B+1, N):
                    if A[i_A].number == A[j_A].number and A[i_A].view == B[j_B].view and A[j_A].view == B[i_B].view:
                        return False

    return B

bubble_sort_A = copy.deepcopy(A)
selection_sort_A = copy.deepcopy(A)

bubble_sort(bubble_sort_A)
print(*[elem.view for elem in bubble_sort_A])
if is_stable(A, bubble_sort_A):
    print('Stable')
else:
    print('Not stable')

selection_sort(selection_sort_A)
print(*[elem.view for elem in selection_sort_A])

if is_stable(A, selection_sort_A):
    print('Stable')
else:
    print('Not stable')

 import copy copy class Card:
    def __init__(self, suit, number):
        self.suit = suit
        self.number = number
        self.view = self.suit + str(self.number) Card def __init__(self, suit, number):
        self.suit = suit
        self.number = number
        self.view = self.suit + str(self.number) __init__ self.suit = suit self.suit self suit suit self.number = number self.number self number number self.view = self.suit + str(self.number) self.view self view self.suit + str(self.number) self.suit self suit str(self.number) str self.number self number self self suit suit number number def bubble_sort(A):
    count = 0
    while True:
        swapped = False
        for i in range(len(A)-1):
            if A[i+1].number < A[i].number:
                A[i+1], A[i] = A[i], A[i+1]
                count += 1
                swapped = True
        if not swapped:
            return count bubble_sort count = 0 count 0 while True:
        swapped = False
        for i in range(len(A)-1):
            if A[i+1].number < A[i].number:
                A[i+1], A[i] = A[i], A[i+1]
                count += 1
                swapped = True
        if not swapped:
            return count True swapped = False swapped False for i in range(len(A)-1):
            if A[i+1].number < A[i].number:
                A[i+1], A[i] = A[i], A[i+1]
                count += 1
                swapped = True
         i range(len(A)-1) range len(A)-1 len(A) len A 1 if A[i+1].number < A[i].number:
                A[i+1], A[i] = A[i], A[i+1]
                count += 1
                swapped = True
         A[i+1].number < A[i].number A[i+1].number [i+1] A i+1 i 1 number A[i].number [i] A i number 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 count += 1 count 1 swapped = True swapped True if not swapped:
            return count not swapped swapped return count count A A def selection_sort(A):
    count = 0
    for i in range(len(A)):
        min_value = A[i].number
        min_value_index = i
        # print('- i:', i, 'A[i]', A[i], '-')
        for j in range(i, len(A)):
            # print('j:', j, 'A[j]:', A[j])
            
            if A[j].number < min_value:
                min_value = A[j].number
                min_value_index = j
        # print('min_value', min_value, 'min_value_index', min_value_index)
        if i != min_value_index:
            count += 1
            A[i], A[min_value_index] = A[min_value_index], A[i]
            # print('swap!', A)
        
    return count selection_sort count = 0 count 0 for i in range(len(A)):
        min_value = A[i].number
        min_value_index = i
        # print('- i:', i, 'A[i]', A[i], '-')
        for j in range(i, len(A)):
            # print('j:', j, 'A[j]:', A[j])
            
            if A[j].number < min_value:
                min_value = A[j].number
                min_value_index = j
        # print('min_value', min_value, 'min_value_index', min_value_index)
        if i != min_value_index:
            count += 1
            A[i], A[min_value_index] = A[min_value_index], A[i]
            # print('swap!', A)
        
     i range(len(A)) range len(A) len A min_value = A[i].number min_value A[i].number [i] A i number min_value_index = i min_value_index i for j in range(i, len(A)):
            # print('j:', j, 'A[j]:', A[j])
            
            if A[j].number < min_value:
                min_value = A[j].number
                min_value_index = j
        # print('min_value', min_value, 'min_value_index', min_value_index)
         j range(i, len(A)) range i len(A) len A if A[j].number < min_value:
                min_value = A[j].number
                min_value_index = j
        # print('min_value', min_value, 'min_value_index', min_value_index)
         A[j].number < min_value A[j].number [j] A j number min_value min_value = A[j].number min_value A[j].number [j] A j number min_value_index = j min_value_index j if i != min_value_index:
            count += 1
            A[i], A[min_value_index] = A[min_value_index], A[i]
            # print('swap!', A)
        
     i != min_value_index i min_value_index count += 1 count 1 A[i], A[min_value_index] = A[min_value_index], A[i] A[i] A i A[min_value_index] A min_value_index A[min_value_index] A min_value_index A[i] A i return count count A A n = int(input()) n int(input()) int input() input A = [] A [] for row in input().split():
    suit, number = list(row)
    A.append(Card(suit, int(number))) row input().split() ().split () input split suit, number = list(row) suit list(row) list row number A.append(Card(suit, int(number))) A.append A append Card(suit, int(number)) Card suit int(number) int number def is_stable(A, B):
    N = len(A)
    for i_A in range(N-1):
        for j_A in range(i_A+1, N):
            for i_B in range(N-1):
                for j_B in range(i_B+1, N):
                    if A[i_A].number == A[j_A].number and A[i_A].view == B[j_B].view and A[j_A].view == B[i_B].view:
                        return False

    return B is_stable N = len(A) N len(A) len A for i_A in range(N-1):
        for j_A in range(i_A+1, N):
            for i_B in range(N-1):
                for j_B in range(i_B+1, N):
                    if A[i_A].number == A[j_A].number and A[i_A].view == B[j_B].view and A[j_A].view == B[i_B].view:
                        return False

     i_A range(N-1) range N-1 N 1 for j_A in range(i_A+1, N):
            for i_B in range(N-1):
                for j_B in range(i_B+1, N):
                    if A[i_A].number == A[j_A].number and A[i_A].view == B[j_B].view and A[j_A].view == B[i_B].view:
                        return False

     j_A range(i_A+1, N) range i_A+1 i_A 1 N for i_B in range(N-1):
                for j_B in range(i_B+1, N):
                    if A[i_A].number == A[j_A].number and A[i_A].view == B[j_B].view and A[j_A].view == B[i_B].view:
                        return False

     i_B range(N-1) range N-1 N 1 for j_B in range(i_B+1, N):
                    if A[i_A].number == A[j_A].number and A[i_A].view == B[j_B].view and A[j_A].view == B[i_B].view:
                        return False

     j_B range(i_B+1, N) range i_B+1 i_B 1 N if A[i_A].number == A[j_A].number and A[i_A].view == B[j_B].view and A[j_A].view == B[i_B].view:
                        return False

     A[i_A].number == A[j_A].number and A[i_A].view == B[j_B].view and A[j_A].view == B[i_B].view A[i_A].number == A[j_A].number and A[i_A].view == B[j_B].view A[i_A].number == A[j_A].number A[i_A].number [i_A] A i_A number A[j_A].number [j_A] A j_A number A[i_A].view == B[j_B].view A[i_A].view [i_A] A i_A view B[j_B].view [j_B] B j_B view A[j_A].view == B[i_B].view A[j_A].view [j_A] A j_A view B[i_B].view [i_B] B i_B view return False False return B B A A B B bubble_sort_A = copy.deepcopy(A) bubble_sort_A copy.deepcopy(A) copy.deepcopy copy deepcopy A selection_sort_A = copy.deepcopy(A) selection_sort_A copy.deepcopy(A) copy.deepcopy copy deepcopy A bubble_sort(bubble_sort_A) bubble_sort bubble_sort_A elem.view for elem in bubble_sort_A for elem in bubble_sort_A elem bubble_sort_A for elem in bubble_sort_A elem.view elem view print(*[elem.view for elem in bubble_sort_A]) print *[elem.view for elem in bubble_sort_A] [elem.view for elem in bubble_sort_A] if is_stable(A, bubble_sort_A):
    print('Stable')
else:
    print('Not stable') is_stable(A, bubble_sort_A) is_stable A bubble_sort_A print('Stable') print 'Stable' print('Not stable') print 'Not stable' selection_sort(selection_sort_A) selection_sort selection_sort_A elem.view for elem in selection_sort_A for elem in selection_sort_A elem selection_sort_A for elem in selection_sort_A elem.view elem view print(*[elem.view for elem in selection_sort_A]) print *[elem.view for elem in selection_sort_A] [elem.view for elem in selection_sort_A] if is_stable(A, selection_sort_A):
    print('Stable')
else:
    print('Not stable') is_stable(A, selection_sort_A) is_stable A selection_sort_A print('Stable') print 'Stable' print('Not stable') print 'Not stable' suit, number = list(row) list(row) suit class Card:
    def __init__(self, suit, number):
        self.suit = suit
        self.number = number
        self.view = self.suit + str(self.number) class Card:
    def __init__(self, suit, number):
        self.suit = suit
        self.number = number
        self.view = self.suit + str(self.number) Card selection_sort_A = copy.deepcopy(A) copy.deepcopy(A) selection_sort_A number = list(row) list(row) number bubble_sort_A = copy.deepcopy(A) copy.deepcopy(A) bubble_sort_A A = [] [] A n = int(input()) int(input()) n def selection_sort(A):
    count = 0
    for i in range(len(A)):
        min_value = A[i].number
        min_value_index = i
        # print('- i:', i, 'A[i]', A[i], '-')
        for j in range(i, len(A)):
            # print('j:', j, 'A[j]:', A[j])
            
            if A[j].number < min_value:
                min_value = A[j].number
                min_value_index = j
        # print('min_value', min_value, 'min_value_index', min_value_index)
        if i != min_value_index:
            count += 1
            A[i], A[min_value_index] = A[min_value_index], A[i]
            # print('swap!', A)
        
    return count def selection_sort(A):
    count = 0
    for i in range(len(A)):
        min_value = A[i].number
        min_value_index = i
        # print('- i:', i, 'A[i]', A[i], '-')
        for j in range(i, len(A)):
            # print('j:', j, 'A[j]:', A[j])
            
            if A[j].number < min_value:
                min_value = A[j].number
                min_value_index = j
        # print('min_value', min_value, 'min_value_index', min_value_index)
        if i != min_value_index:
            count += 1
            A[i], A[min_value_index] = A[min_value_index], A[i]
            # print('swap!', A)
        
    return count selection_sort def bubble_sort(A):
    count = 0
    while True:
        swapped = False
        for i in range(len(A)-1):
            if A[i+1].number < A[i].number:
                A[i+1], A[i] = A[i], A[i+1]
                count += 1
                swapped = True
        if not swapped:
            return count def bubble_sort(A):
    count = 0
    while True:
        swapped = False
        for i in range(len(A)-1):
            if A[i+1].number < A[i].number:
                A[i+1], A[i] = A[i], A[i+1]
                count += 1
                swapped = True
        if not swapped:
            return count bubble_sort def is_stable(A, B):
    N = len(A)
    for i_A in range(N-1):
        for j_A in range(i_A+1, N):
            for i_B in range(N-1):
                for j_B in range(i_B+1, N):
                    if A[i_A].number == A[j_A].number and A[i_A].view == B[j_B].view and A[j_A].view == B[i_B].view:
                        return False

    return B def is_stable(A, B):
    N = len(A)
    for i_A in range(N-1):
        for j_A in range(i_A+1, N):
            for i_B in range(N-1):
                for j_B in range(i_B+1, N):
                    if A[i_A].number == A[j_A].number and A[i_A].view == B[j_B].view and A[j_A].view == B[i_B].view:
                        return False

    return B is_stable
import copy

class Card:
    def __init__(self, suit, number):
        self.suit = suit
        self.number = number
        self.view = self.suit + str(self.number)

def bubble_sort(A):
    count = 0
    while True:
        swapped = False
        for i in range(len(A)-1):
            if A[i+1].number < A[i].number:
                A[i+1], A[i] = A[i], A[i+1]
                count += 1
                swapped = True
        if not swapped:
            return count

def selection_sort(A):
    count = 0
    for i in range(len(A)):
        min_value = A[i].number
        min_value_index = i
        # print('- i:', i, 'A[i]', A[i], '-')
        for j in range(i, len(A)):
            # print('j:', j, 'A[j]:', A[j])
            
            if A[j].number < min_value:
                min_value = A[j].number
                min_value_index = j
        # print('min_value', min_value, 'min_value_index', min_value_index)
        if i != min_value_index:
            count += 1
            A[i], A[min_value_index] = A[min_value_index], A[i]
            # print('swap!', A)
        
    return count

n = int(input())
A = []
for row in input().split():
    suit, number = list(row)
    A.append(Card(suit, int(number)))

def is_stable(A, B):
    N = len(A)
    for i_A in range(N-1):
        for j_A in range(i_A+1, N):
            for i_B in range(N-1):
                for j_B in range(i_B+1, N):
                    if A[i_A].number == A[j_A].number and A[i_A].view == B[j_B].view and A[j_A].view == B[i_B].view:
                        return False

    return B

bubble_sort_A = copy.deepcopy(A)
selection_sort_A = copy.deepcopy(A)

bubble_sort(bubble_sort_A)
print(*[elem.view for elem in bubble_sort_A])
if is_stable(A, bubble_sort_A):
    print('Stable')
else:
    print('Not stable')

selection_sort(selection_sort_A)
print(*[elem.view for elem in selection_sort_A])

if is_stable(A, selection_sort_A):
    print('Stable')
else:
    print('Not stable')


