import copy

def BubbleSort(C, N):
    for i in range(0,N):
        for j in range(N-1,i,-1):
            if C[j][1:] < C[j-1][1:]:
                #  C[j] と C[j-1] を交換
                C[j] , C[j-1] = C[j-1] , C[j]
    ret = " ".join(C)
    print(ret)
    print("Stable")
    return ret

def SelectionSort(C, N):
    for i in range(0,N):
        minj = i
        for j in range(i,N):
            if C[j][1:] < C[minj][1:]:
                minj = j
        # C[i] と C[minj] を交換
        C[i] , C[minj] = C[minj] , C[i]
    ret = " ".join(C)
    print(ret)
    return ret

N = int(input())
C = input().split(" ")
ret_B = BubbleSort(copy.deepcopy(C),N)
ret_S = SelectionSort(copy.deepcopy(C),N)
if ret_B == ret_S:
    print("Stable")
else:
    print("Not stable")
 import copy copy def BubbleSort(C, N):
    for i in range(0,N):
        for j in range(N-1,i,-1):
            if C[j][1:] < C[j-1][1:]:
                #  C[j] と C[j-1] を交換
                C[j] , C[j-1] = C[j-1] , C[j]
    ret = " ".join(C)
    print(ret)
    print("Stable")
    return ret BubbleSort for i in range(0,N):
        for j in range(N-1,i,-1):
            if C[j][1:] < C[j-1][1:]:
                #  C[j] と C[j-1] を交換
                C[j] , C[j-1] = C[j-1] , C[j]
     i range(0,N) range 0 N for j in range(N-1,i,-1):
            if C[j][1:] < C[j-1][1:]:
                #  C[j] と C[j-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] , C[j-1] = C[j-1] , C[j]
     C[j][1:] < C[j-1][1:] C[j][1:] [j] C j 1: 1 C[j-1][1:] [j-1] C j-1 j 1 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 ret = " ".join(C) ret " ".join(C) " ".join " " join C print(ret) print ret print("Stable") print "Stable" return ret ret C C N N def SelectionSort(C, N):
    for i in range(0,N):
        minj = i
        for j in range(i,N):
            if C[j][1:] < C[minj][1:]:
                minj = j
        # C[i] と C[minj] を交換
        C[i] , C[minj] = C[minj] , C[i]
    ret = " ".join(C)
    print(ret)
    return ret SelectionSort for i in range(0,N):
        minj = i
        for j in range(i,N):
            if C[j][1:] < C[minj][1:]:
                minj = j
        # C[i] と C[minj] を交換
        C[i] , C[minj] = C[minj] , C[i]
     i range(0,N) range 0 N minj = i minj i for j in range(i,N):
            if C[j][1:] < C[minj][1:]:
                minj = j
        # C[i] と C[minj] を交換
         j range(i,N) range i N if C[j][1:] < C[minj][1:]:
                minj = j
        # C[i] と C[minj] を交換
         C[j][1:] < C[minj][1:] C[j][1:] [j] C j 1: 1 C[minj][1:] [minj] C minj 1: 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 ret = " ".join(C) ret " ".join(C) " ".join " " join C print(ret) print ret return ret ret C C N N N = int(input()) N int(input()) int input() input C = input().split(" ") C input().split(" ") ().split () input split " " ret_B = BubbleSort(copy.deepcopy(C),N) ret_B BubbleSort(copy.deepcopy(C),N) BubbleSort copy.deepcopy(C) copy.deepcopy copy deepcopy C N ret_S = SelectionSort(copy.deepcopy(C),N) ret_S SelectionSort(copy.deepcopy(C),N) SelectionSort copy.deepcopy(C) copy.deepcopy copy deepcopy C N if ret_B == ret_S:
    print("Stable")
else:
    print("Not stable") ret_B == ret_S ret_B ret_S print("Stable") print "Stable" print("Not stable") print "Not stable" def SelectionSort(C, N):
    for i in range(0,N):
        minj = i
        for j in range(i,N):
            if C[j][1:] < C[minj][1:]:
                minj = j
        # C[i] と C[minj] を交換
        C[i] , C[minj] = C[minj] , C[i]
    ret = " ".join(C)
    print(ret)
    return ret def SelectionSort(C, N):
    for i in range(0,N):
        minj = i
        for j in range(i,N):
            if C[j][1:] < C[minj][1:]:
                minj = j
        # C[i] と C[minj] を交換
        C[i] , C[minj] = C[minj] , C[i]
    ret = " ".join(C)
    print(ret)
    return ret SelectionSort ret_B = BubbleSort(copy.deepcopy(C),N) BubbleSort(copy.deepcopy(C),N) ret_B N = int(input()) int(input()) N ret_S = SelectionSort(copy.deepcopy(C),N) SelectionSort(copy.deepcopy(C),N) ret_S def BubbleSort(C, N):
    for i in range(0,N):
        for j in range(N-1,i,-1):
            if C[j][1:] < C[j-1][1:]:
                #  C[j] と C[j-1] を交換
                C[j] , C[j-1] = C[j-1] , C[j]
    ret = " ".join(C)
    print(ret)
    print("Stable")
    return ret def BubbleSort(C, N):
    for i in range(0,N):
        for j in range(N-1,i,-1):
            if C[j][1:] < C[j-1][1:]:
                #  C[j] と C[j-1] を交換
                C[j] , C[j-1] = C[j-1] , C[j]
    ret = " ".join(C)
    print(ret)
    print("Stable")
    return ret BubbleSort C = input().split(" ") input().split(" ") C
import copy

def BubbleSort(C, N):
    for i in range(0,N):
        for j in range(N-1,i,-1):
            if C[j][1:] < C[j-1][1:]:
                #  C[j] と C[j-1] を交換
                C[j] , C[j-1] = C[j-1] , C[j]
    ret = " ".join(C)
    print(ret)
    print("Stable")
    return ret

def SelectionSort(C, N):
    for i in range(0,N):
        minj = i
        for j in range(i,N):
            if C[j][1:] < C[minj][1:]:
                minj = j
        # C[i] と C[minj] を交換
        C[i] , C[minj] = C[minj] , C[i]
    ret = " ".join(C)
    print(ret)
    return ret

N = int(input())
C = input().split(" ")
ret_B = BubbleSort(copy.deepcopy(C),N)
ret_S = SelectionSort(copy.deepcopy(C),N)
if ret_B == ret_S:
    print("Stable")
else:
    print("Not stable")

