def BubbleSort(C, N):
    for i in xrange(0, N):
        for j in reversed(xrange(i+1, N)):
            if C[j][1] < C[j-1][1]:
                C[j], C[j-1] = C[j-1], C[j]

def SelectionSort(C, N):
    for i in xrange(N):
        minj = i
        for j in xrange(i, N):
            if C[j][1] < C[minj][1]:
                minj = j
        C[i], C[minj] = C[minj], C[i]

def isStable(C, N):
    flag = 0
    for i in xrange(1, 14):
        Ct = [C[j] for j in xrange(N) if C[j][1] == str(i)]
        if len(Ct) > 1:
            for k in xrange( len(Ct)-1 ):
                if Ct[k][2] > Ct[k+1][2]:
                    flag = 1
    if flag == 0: print "Stable"
    else:         print "Not stable"

# MAIN
N = input()
C = []
Ct = map(str, raw_input().split())
for i in xrange(N):
    C.append([Ct[i][0], Ct[i][1::], i])

Ct = list(C)
BubbleSort(Ct, N)
print " ".join([Ct[i][0]+Ct[i][1] for i in xrange(N)])
isStable(Ct, N)

Ct = list(C)
SelectionSort(Ct, N)
print " ".join([Ct[i][0]+Ct[i][1] for i in xrange(N)])
isStable(Ct, N) def BubbleSort(C, N):
    for i in xrange(0, N):
        for j in reversed(xrange(i+1, N)):
            if C[j][1] < C[j-1][1]:
                C[j], C[j-1] = C[j-1], C[j] BubbleSort for i in xrange(0, N):
        for j in reversed(xrange(i+1, N)):
            if C[j][1] < C[j-1][1]:
                C[j], C[j-1] = C[j-1], C[j] i xrange(0, N) xrange 0 N for j in reversed(xrange(i+1, N)):
            if C[j][1] < C[j-1][1]:
                C[j], C[j-1] = C[j-1], C[j] j reversed(xrange(i+1, N)) reversed xrange(i+1, N) xrange i+1 i 1 N if C[j][1] < C[j-1][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 C[j-1][1] [j-1] C j-1 j 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 C C N N def SelectionSort(C, N):
    for i in xrange(N):
        minj = i
        for j in xrange(i, N):
            if C[j][1] < C[minj][1]:
                minj = j
        C[i], C[minj] = C[minj], C[i] SelectionSort for i in xrange(N):
        minj = i
        for j in xrange(i, N):
            if C[j][1] < C[minj][1]:
                minj = j
        C[i], C[minj] = C[minj], C[i] i xrange(N) xrange N minj = i minj i for j in xrange(i, N):
            if C[j][1] < C[minj][1]:
                minj = j
         j xrange(i, N) xrange i N if C[j][1] < C[minj][1]:
                minj = j
         C[j][1] < C[minj][1] C[j][1] [j] C j 1 C[minj][1] [minj] C minj 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 C C N N def isStable(C, N):
    flag = 0
    for i in xrange(1, 14):
        Ct = [C[j] for j in xrange(N) if C[j][1] == str(i)]
        if len(Ct) > 1:
            for k in xrange( len(Ct)-1 ):
                if Ct[k][2] > Ct[k+1][2]:
                    flag = 1
    if flag == 0: print "Stable"
    else:         print "Not stable"

# MAIN isStable flag = 0 flag 0 for i in xrange(1, 14):
        Ct = [C[j] for j in xrange(N) if C[j][1] == str(i)]
        if len(Ct) > 1:
            for k in xrange( len(Ct)-1 ):
                if Ct[k][2] > Ct[k+1][2]:
                    flag = 1
     i xrange(1, 14) xrange 1 14 C[j] for j in xrange(N) if C[j][1] == str(i) for j in xrange(N) if C[j][1] == str(i) j xrange(N) xrange N C[j][1] == str(i) C[j][1] [j] C j 1 str(i) str i if C[j][1] == str(i) C[j] C j Ct = [C[j] for j in xrange(N) if C[j][1] == str(i)] Ct [C[j] for j in xrange(N) if C[j][1] == str(i)] if len(Ct) > 1:
            for k in xrange( len(Ct)-1 ):
                if Ct[k][2] > Ct[k+1][2]:
                    flag = 1
     len(Ct) > 1 len(Ct) len Ct 1 for k in xrange( len(Ct)-1 ):
                if Ct[k][2] > Ct[k+1][2]:
                    flag = 1
     k xrange( len(Ct)-1 ) xrange len(Ct)-1 len(Ct) len Ct 1 if Ct[k][2] > Ct[k+1][2]:
                    flag = 1
     Ct[k][2] > Ct[k+1][2] Ct[k][2] [k] Ct k 2 Ct[k+1][2] [k+1] Ct k+1 k 1 2 flag = 1 flag 1 if flag == 0: print "Stable"
    else:         print "Not stable"

# MAIN flag == 0 flag 0 C C N N N = input() N input() input C = [] C [] Ct = map(str, raw_input().split()) Ct map(str, raw_input().split()) map str raw_input().split() ().split () raw_input split for i in xrange(N):
    C.append([Ct[i][0], Ct[i][1::], i]) i xrange(N) xrange N C.append([Ct[i][0], Ct[i][1::], i]) C.append C append [Ct[i][0], Ct[i][1::], i] Ct[i][0] [i] Ct i 0 Ct[i][1::] [i] Ct i 1:: 1 i Ct = list(C) Ct list(C) list C BubbleSort(Ct, N) BubbleSort Ct N print Ct[i][0]+Ct[i][1] for i in xrange(N) for i in xrange(N) i xrange(N) xrange N for i in xrange(N) Ct[i][0]+Ct[i][1] Ct[i][0] [i] Ct i 0 Ct[i][1] [i] Ct i 1 " ".join([Ct[i][0]+Ct[i][1] for i in xrange(N)]) " ".join " " join [Ct[i][0]+Ct[i][1] for i in xrange(N)] isStable(Ct, N) isStable Ct N Ct = list(C) Ct list(C) list C SelectionSort(Ct, N) SelectionSort Ct N print Ct[i][0]+Ct[i][1] for i in xrange(N) for i in xrange(N) i xrange(N) xrange N for i in xrange(N) Ct[i][0]+Ct[i][1] Ct[i][0] [i] Ct i 0 Ct[i][1] [i] Ct i 1 " ".join([Ct[i][0]+Ct[i][1] for i in xrange(N)]) " ".join " " join [Ct[i][0]+Ct[i][1] for i in xrange(N)] isStable(Ct, N) isStable Ct N Ct = list(C) list(C) Ct def isStable(C, N):
    flag = 0
    for i in xrange(1, 14):
        Ct = [C[j] for j in xrange(N) if C[j][1] == str(i)]
        if len(Ct) > 1:
            for k in xrange( len(Ct)-1 ):
                if Ct[k][2] > Ct[k+1][2]:
                    flag = 1
    if flag == 0: print "Stable"
    else:         print "Not stable"

# MAIN def isStable(C, N):
    flag = 0
    for i in xrange(1, 14):
        Ct = [C[j] for j in xrange(N) if C[j][1] == str(i)]
        if len(Ct) > 1:
            for k in xrange( len(Ct)-1 ):
                if Ct[k][2] > Ct[k+1][2]:
                    flag = 1
    if flag == 0: print "Stable"
    else:         print "Not stable"

# MAIN isStable N = input() input() N C = [] [] C def BubbleSort(C, N):
    for i in xrange(0, N):
        for j in reversed(xrange(i+1, N)):
            if C[j][1] < C[j-1][1]:
                C[j], C[j-1] = C[j-1], C[j] def BubbleSort(C, N):
    for i in xrange(0, N):
        for j in reversed(xrange(i+1, N)):
            if C[j][1] < C[j-1][1]:
                C[j], C[j-1] = C[j-1], C[j] BubbleSort Ct = map(str, raw_input().split()) map(str, raw_input().split()) Ct Ct = list(C) list(C) Ct def SelectionSort(C, N):
    for i in xrange(N):
        minj = i
        for j in xrange(i, N):
            if C[j][1] < C[minj][1]:
                minj = j
        C[i], C[minj] = C[minj], C[i] def SelectionSort(C, N):
    for i in xrange(N):
        minj = i
        for j in xrange(i, N):
            if C[j][1] < C[minj][1]:
                minj = j
        C[i], C[minj] = C[minj], C[i] SelectionSort
def BubbleSort(C, N):
    for i in xrange(0, N):
        for j in reversed(xrange(i+1, N)):
            if C[j][1] < C[j-1][1]:
                C[j], C[j-1] = C[j-1], C[j]

def SelectionSort(C, N):
    for i in xrange(N):
        minj = i
        for j in xrange(i, N):
            if C[j][1] < C[minj][1]:
                minj = j
        C[i], C[minj] = C[minj], C[i]

def isStable(C, N):
    flag = 0
    for i in xrange(1, 14):
        Ct = [C[j] for j in xrange(N) if C[j][1] == str(i)]
        if len(Ct) > 1:
            for k in xrange( len(Ct)-1 ):
                if Ct[k][2] > Ct[k+1][2]:
                    flag = 1
    if flag == 0: print "Stable"
    else:         print "Not stable"

# MAIN
N = input()
C = []
Ct = map(str, raw_input().split())
for i in xrange(N):
    C.append([Ct[i][0], Ct[i][1::], i])

Ct = list(C)
BubbleSort(Ct, N)
print " ".join([Ct[i][0]+Ct[i][1] for i in xrange(N)])
isStable(Ct, N)

Ct = list(C)
SelectionSort(Ct, N)
print " ".join([Ct[i][0]+Ct[i][1] for i in xrange(N)])
isStable(Ct, N)