#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
??????????????????
?????????????????????????????´?????????????????????
????????§?????????????????????(S, H, C, D)??¨???????????°???(1, 2, ..., 9)??????
?§??????????????¨? 36 ????????????????????¨????????????
????????°??????????????? 8 ???"H8"??????????????? 1 ???"D1"??¨??¨????????????

????????????????????????????????????????????¢?????´??????????????¨?????????
??????????????? N ????????????????????????????????°???????????????
???????????´?????????????????°????????????????????????????????????
??¢?????´??????????????????????????\????????????????????????????????????????????¨????????????
??°??????????´???? 0 ??????????????§?¨???°?????????????????????

1 BubbleSort(C, N)
2   for i = 0 to N-1
3     for j = N-1 downto i+1
4       if C[j].value < C[j-1].value
5         C[j] ??¨ C[j-1] ?????????
6
7 SelectionSort(C, N)
8   for i = 0 to N-1
9     minj = i
10    for j = i to N-1
11      if C[j].value < C[minj].value
12        minj = j
13    C[i] ??¨ C[minj] ?????????

??????????????¢?????´?????????????????????????????????????????\??????????????????????????????????????£??????????????±????????????????????????

"""
import math

def swap(A, x, y):
    """ ??????A???x????????¨y?????????????´??????\???????????? """
    t = A[x]
    A[x] = A[y]
    A[y] = t

def value(c):
    """ ??????????????°?????¨???????????? """
    return int(c[1:])

def suits(A):
    """ ????????????????????????????????¨???????????????????????? """
    s = ""
    for c in A:
        s = s + c[0]
    return s

def bSort(A, N):
    """ ?????????????????? """
    for i in range(0, N-1):
        for j in range(N-1, i, -1):
            if value(A[j]) < value(A[j-1]):
                swap(A,j,j-1)

def sSort(A, N):
    """ ?????????????????? """
    for i in range(0, N-1):
        minj = i
        for j in range(i,N):
            if value(A[j]) < value(A[minj]):
                minj = j
        if minj != i:
            swap(A,i,minj)

# ?????????
N = int(input().strip())
A = input().strip().split()
bA = A[:]
bSort(bA,N)
b1 = suits(bA)
print(" ".join(map(str,bA)))
print("Stable")

sA = A[:]
sSort(sA,N)
s1 = suits(sA)
print(" ".join(map(str,sA)))
if s1 == b1:
    print("Stable")
else:
    print("Not stable") """
??????????????????
?????????????????????????????´?????????????????????
????????§?????????????????????(S, H, C, D)??¨???????????°???(1, 2, ..., 9)??????
?§??????????????¨? 36 ????????????????????¨????????????
????????°??????????????? 8 ???"H8"??????????????? 1 ???"D1"??¨??¨????????????

????????????????????????????????????????????¢?????´??????????????¨?????????
??????????????? N ????????????????????????????????°???????????????
???????????´?????????????????°????????????????????????????????????
??¢?????´??????????????????????????\????????????????????????????????????????????¨????????????
??°??????????´???? 0 ??????????????§?¨???°?????????????????????

1 BubbleSort(C, N)
2   for i = 0 to N-1
3     for j = N-1 downto i+1
4       if C[j].value < C[j-1].value
5         C[j] ??¨ C[j-1] ?????????
6
7 SelectionSort(C, N)
8   for i = 0 to N-1
9     minj = i
10    for j = i to N-1
11      if C[j].value < C[minj].value
12        minj = j
13    C[i] ??¨ C[minj] ?????????

??????????????¢?????´?????????????????????????????????????????\??????????????????????????????????????£??????????????±????????????????????????

""" import math math def swap(A, x, y):
    """ ??????A???x????????¨y?????????????´??????\???????????? """
    t = A[x]
    A[x] = A[y]
    A[y] = t swap """ ??????A???x????????¨y?????????????´??????\???????????? """ t = A[x] t A[x] A x A[x] = A[y] A[x] A x A[y] A y A[y] = t A[y] A y t A A x x y y def value(c):
    """ ??????????????°?????¨???????????? """
    return int(c[1:]) value """ ??????????????°?????¨???????????? """ return int(c[1:]) int(c[1:]) int c[1:] c 1: 1 c c def suits(A):
    """ ????????????????????????????????¨???????????????????????? """
    s = ""
    for c in A:
        s = s + c[0]
    return s suits """ ????????????????????????????????¨???????????????????????? """ s = "" s "" for c in A:
        s = s + c[0]
     c A s = s + c[0] s s + c[0] s c[0] c 0 return s s A A def bSort(A, N):
    """ ?????????????????? """
    for i in range(0, N-1):
        for j in range(N-1, i, -1):
            if value(A[j]) < value(A[j-1]):
                swap(A,j,j-1) bSort """ ?????????????????? """ for i in range(0, N-1):
        for j in range(N-1, i, -1):
            if value(A[j]) < value(A[j-1]):
                swap(A,j,j-1) i range(0, N-1) range 0 N-1 N 1 for j in range(N-1, i, -1):
            if value(A[j]) < value(A[j-1]):
                swap(A,j,j-1) j range(N-1, i, -1) range N-1 N 1 i -1 if value(A[j]) < value(A[j-1]):
                swap(A,j,j-1) value(A[j]) < value(A[j-1]) value(A[j]) value A[j] A j value(A[j-1]) value A[j-1] A j-1 j 1 swap(A,j,j-1) swap A j j-1 j 1 A A N N def sSort(A, N):
    """ ?????????????????? """
    for i in range(0, N-1):
        minj = i
        for j in range(i,N):
            if value(A[j]) < value(A[minj]):
                minj = j
        if minj != i:
            swap(A,i,minj)

# ????????? sSort """ ?????????????????? """ for i in range(0, N-1):
        minj = i
        for j in range(i,N):
            if value(A[j]) < value(A[minj]):
                minj = j
        if minj != i:
            swap(A,i,minj)

# ????????? i range(0, N-1) range 0 N-1 N 1 minj = i minj i for j in range(i,N):
            if value(A[j]) < value(A[minj]):
                minj = j
         j range(i,N) range i N if value(A[j]) < value(A[minj]):
                minj = j
         value(A[j]) < value(A[minj]) value(A[j]) value A[j] A j value(A[minj]) value A[minj] A minj minj = j minj j if minj != i:
            swap(A,i,minj)

# ????????? minj != i minj i swap(A,i,minj) swap A i minj A A N N N = int(input().strip()) N int(input().strip()) int input().strip() ().strip () input strip A = input().strip().split() A input().strip().split() ().strip().split ().strip() ().strip () input strip split bA = A[:] bA A[:] A : bSort(bA,N) bSort bA N b1 = suits(bA) b1 suits(bA) suits bA print(" ".join(map(str,bA))) print " ".join(map(str,bA)) " ".join " " join map(str,bA) map str bA print("Stable") print "Stable" sA = A[:] sA A[:] A : sSort(sA,N) sSort sA N s1 = suits(sA) s1 suits(sA) suits sA print(" ".join(map(str,sA))) print " ".join(map(str,sA)) " ".join " " join map(str,sA) map str sA if s1 == b1:
    print("Stable")
else:
    print("Not stable") s1 == b1 s1 b1 print("Stable") print "Stable" print("Not stable") print "Not stable" def swap(A, x, y):
    """ ??????A???x????????¨y?????????????´??????\???????????? """
    t = A[x]
    A[x] = A[y]
    A[y] = t def swap(A, x, y):
    """ ??????A???x????????¨y?????????????´??????\???????????? """
    t = A[x]
    A[x] = A[y]
    A[y] = t swap def value(c):
    """ ??????????????°?????¨???????????? """
    return int(c[1:]) def value(c):
    """ ??????????????°?????¨???????????? """
    return int(c[1:]) value s1 = suits(sA) suits(sA) s1 sA = A[:] A[:] sA def bSort(A, N):
    """ ?????????????????? """
    for i in range(0, N-1):
        for j in range(N-1, i, -1):
            if value(A[j]) < value(A[j-1]):
                swap(A,j,j-1) def bSort(A, N):
    """ ?????????????????? """
    for i in range(0, N-1):
        for j in range(N-1, i, -1):
            if value(A[j]) < value(A[j-1]):
                swap(A,j,j-1) bSort A = input().strip().split() input().strip().split() A def sSort(A, N):
    """ ?????????????????? """
    for i in range(0, N-1):
        minj = i
        for j in range(i,N):
            if value(A[j]) < value(A[minj]):
                minj = j
        if minj != i:
            swap(A,i,minj)

# ????????? def sSort(A, N):
    """ ?????????????????? """
    for i in range(0, N-1):
        minj = i
        for j in range(i,N):
            if value(A[j]) < value(A[minj]):
                minj = j
        if minj != i:
            swap(A,i,minj)

# ????????? sSort bA = A[:] A[:] bA b1 = suits(bA) suits(bA) b1 def suits(A):
    """ ????????????????????????????????¨???????????????????????? """
    s = ""
    for c in A:
        s = s + c[0]
    return s def suits(A):
    """ ????????????????????????????????¨???????????????????????? """
    s = ""
    for c in A:
        s = s + c[0]
    return s suits N = int(input().strip()) int(input().strip()) N
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
??????????????????
?????????????????????????????´?????????????????????
????????§?????????????????????(S, H, C, D)??¨???????????°???(1, 2, ..., 9)??????
?§??????????????¨? 36 ????????????????????¨????????????
????????°??????????????? 8 ???"H8"??????????????? 1 ???"D1"??¨??¨????????????

????????????????????????????????????????????¢?????´??????????????¨?????????
??????????????? N ????????????????????????????????°???????????????
???????????´?????????????????°????????????????????????????????????
??¢?????´??????????????????????????\????????????????????????????????????????????¨????????????
??°??????????´???? 0 ??????????????§?¨???°?????????????????????

1 BubbleSort(C, N)
2   for i = 0 to N-1
3     for j = N-1 downto i+1
4       if C[j].value < C[j-1].value
5         C[j] ??¨ C[j-1] ?????????
6
7 SelectionSort(C, N)
8   for i = 0 to N-1
9     minj = i
10    for j = i to N-1
11      if C[j].value < C[minj].value
12        minj = j
13    C[i] ??¨ C[minj] ?????????

??????????????¢?????´?????????????????????????????????????????\??????????????????????????????????????£??????????????±????????????????????????

"""
import math

def swap(A, x, y):
    """ ??????A???x????????¨y?????????????´??????\???????????? """
    t = A[x]
    A[x] = A[y]
    A[y] = t

def value(c):
    """ ??????????????°?????¨???????????? """
    return int(c[1:])

def suits(A):
    """ ????????????????????????????????¨???????????????????????? """
    s = ""
    for c in A:
        s = s + c[0]
    return s

def bSort(A, N):
    """ ?????????????????? """
    for i in range(0, N-1):
        for j in range(N-1, i, -1):
            if value(A[j]) < value(A[j-1]):
                swap(A,j,j-1)

def sSort(A, N):
    """ ?????????????????? """
    for i in range(0, N-1):
        minj = i
        for j in range(i,N):
            if value(A[j]) < value(A[minj]):
                minj = j
        if minj != i:
            swap(A,i,minj)

# ?????????
N = int(input().strip())
A = input().strip().split()
bA = A[:]
bSort(bA,N)
b1 = suits(bA)
print(" ".join(map(str,bA)))
print("Stable")

sA = A[:]
sSort(sA,N)
s1 = suits(sA)
print(" ".join(map(str,sA)))
if s1 == b1:
    print("Stable")
else:
    print("Not stable")