def bubble_sort(a):
    n = len(a)
    for j in range(n):
        m = n-1-j # n-1 to 0
        for i in range(m):
            if a[i][1] > a[i+1][1]:
                a[i],a[i+1] = a[i+1],a[i]
    return a


def selection_sort(a):
    MAXINT = 10
    n = len(a)
    for j in range(n):
        minv = MAXINT
        minp = -1
        for i in range(j,n):
            if int(a[i][1]) < minv:
                minv = int(a[i][1])
                minp = i
        a[minp],a[j]=a[j],a[minp]
    return a

def stable(a,f):
    b = f(a[:])
    print ' '.join(b)
    p = 1
    while p < 10:
        c = []
        d = []
        for i in range(len(a)):
            if a[i][1] == str(p):
                c.append(a[i])
            if b[i][1] == str(p):
                d.append(b[i])
        for i in range(len(c)):
            if c[i] != d[i]:
                print "Not stable"
                return
        p += 1
    print "Stable"

n = int(raw_input())
cards = raw_input().split(' ')

stable(cards[:],bubble_sort)
stable(cards[:],selection_sort) def bubble_sort(a):
    n = len(a)
    for j in range(n):
        m = n-1-j # n-1 to 0
        for i in range(m):
            if a[i][1] > a[i+1][1]:
                a[i],a[i+1] = a[i+1],a[i]
    return a bubble_sort n = len(a) n len(a) len a for j in range(n):
        m = n-1-j # n-1 to 0
        for i in range(m):
            if a[i][1] > a[i+1][1]:
                a[i],a[i+1] = a[i+1],a[i]
     j range(n) range n m = n-1-j m n-1-j n-1 n 1 j for i in range(m):
            if a[i][1] > a[i+1][1]:
                a[i],a[i+1] = a[i+1],a[i]
     i range(m) range m if a[i][1] > a[i+1][1]:
                a[i],a[i+1] = a[i+1],a[i]
     a[i][1] > a[i+1][1] a[i][1] [i] a i 1 a[i+1][1] [i+1] a i+1 i 1 1 a[i],a[i+1] = a[i+1],a[i] a[i] a i a[i+1] a i+1 i 1 a[i+1] a i+1 i 1 a[i] a i return a a a a def selection_sort(a):
    MAXINT = 10
    n = len(a)
    for j in range(n):
        minv = MAXINT
        minp = -1
        for i in range(j,n):
            if int(a[i][1]) < minv:
                minv = int(a[i][1])
                minp = i
        a[minp],a[j]=a[j],a[minp]
    return a selection_sort MAXINT = 10 MAXINT 10 n = len(a) n len(a) len a for j in range(n):
        minv = MAXINT
        minp = -1
        for i in range(j,n):
            if int(a[i][1]) < minv:
                minv = int(a[i][1])
                minp = i
        a[minp],a[j]=a[j],a[minp]
     j range(n) range n minv = MAXINT minv MAXINT minp = -1 minp -1 for i in range(j,n):
            if int(a[i][1]) < minv:
                minv = int(a[i][1])
                minp = i
         i range(j,n) range j n if int(a[i][1]) < minv:
                minv = int(a[i][1])
                minp = i
         int(a[i][1]) < minv int(a[i][1]) int a[i][1] [i] a i 1 minv minv = int(a[i][1]) minv int(a[i][1]) int a[i][1] [i] a i 1 minp = i minp i a[minp],a[j]=a[j],a[minp] a[minp] a minp a[j] a j a[j] a j a[minp] a minp return a a a a def stable(a,f):
    b = f(a[:])
    print ' '.join(b)
    p = 1
    while p < 10:
        c = []
        d = []
        for i in range(len(a)):
            if a[i][1] == str(p):
                c.append(a[i])
            if b[i][1] == str(p):
                d.append(b[i])
        for i in range(len(c)):
            if c[i] != d[i]:
                print "Not stable"
                return
        p += 1
    print "Stable" stable b = f(a[:]) b f(a[:]) f a[:] a : print ' '.join(b) ' '.join ' ' join b p = 1 p 1 while p < 10:
        c = []
        d = []
        for i in range(len(a)):
            if a[i][1] == str(p):
                c.append(a[i])
            if b[i][1] == str(p):
                d.append(b[i])
        for i in range(len(c)):
            if c[i] != d[i]:
                print "Not stable"
                return
        p += 1
     p < 10 p 10 c = [] c [] d = [] d [] for i in range(len(a)):
            if a[i][1] == str(p):
                c.append(a[i])
            if b[i][1] == str(p):
                d.append(b[i])
         i range(len(a)) range len(a) len a if a[i][1] == str(p):
                c.append(a[i])
             a[i][1] == str(p) a[i][1] [i] a i 1 str(p) str p c.append(a[i]) c.append c append a[i] a i if b[i][1] == str(p):
                d.append(b[i])
         b[i][1] == str(p) b[i][1] [i] b i 1 str(p) str p d.append(b[i]) d.append d append b[i] b i for i in range(len(c)):
            if c[i] != d[i]:
                print "Not stable"
                return
         i range(len(c)) range len(c) len c if c[i] != d[i]:
                print "Not stable"
                return
         c[i] != d[i] c[i] c i d[i] d i print return p += 1 p 1 print a a f f n = int(raw_input()) n int(raw_input()) int raw_input() raw_input cards = raw_input().split(' ') cards raw_input().split(' ') ().split () raw_input split ' ' stable(cards[:],bubble_sort) stable cards[:] cards : bubble_sort stable(cards[:],selection_sort) stable cards[:] cards : selection_sort def selection_sort(a):
    MAXINT = 10
    n = len(a)
    for j in range(n):
        minv = MAXINT
        minp = -1
        for i in range(j,n):
            if int(a[i][1]) < minv:
                minv = int(a[i][1])
                minp = i
        a[minp],a[j]=a[j],a[minp]
    return a def selection_sort(a):
    MAXINT = 10
    n = len(a)
    for j in range(n):
        minv = MAXINT
        minp = -1
        for i in range(j,n):
            if int(a[i][1]) < minv:
                minv = int(a[i][1])
                minp = i
        a[minp],a[j]=a[j],a[minp]
    return a selection_sort def stable(a,f):
    b = f(a[:])
    print ' '.join(b)
    p = 1
    while p < 10:
        c = []
        d = []
        for i in range(len(a)):
            if a[i][1] == str(p):
                c.append(a[i])
            if b[i][1] == str(p):
                d.append(b[i])
        for i in range(len(c)):
            if c[i] != d[i]:
                print "Not stable"
                return
        p += 1
    print "Stable" def stable(a,f):
    b = f(a[:])
    print ' '.join(b)
    p = 1
    while p < 10:
        c = []
        d = []
        for i in range(len(a)):
            if a[i][1] == str(p):
                c.append(a[i])
            if b[i][1] == str(p):
                d.append(b[i])
        for i in range(len(c)):
            if c[i] != d[i]:
                print "Not stable"
                return
        p += 1
    print "Stable" stable cards = raw_input().split(' ') raw_input().split(' ') cards def bubble_sort(a):
    n = len(a)
    for j in range(n):
        m = n-1-j # n-1 to 0
        for i in range(m):
            if a[i][1] > a[i+1][1]:
                a[i],a[i+1] = a[i+1],a[i]
    return a def bubble_sort(a):
    n = len(a)
    for j in range(n):
        m = n-1-j # n-1 to 0
        for i in range(m):
            if a[i][1] > a[i+1][1]:
                a[i],a[i+1] = a[i+1],a[i]
    return a bubble_sort n = int(raw_input()) int(raw_input()) n
def bubble_sort(a):
    n = len(a)
    for j in range(n):
        m = n-1-j # n-1 to 0
        for i in range(m):
            if a[i][1] > a[i+1][1]:
                a[i],a[i+1] = a[i+1],a[i]
    return a


def selection_sort(a):
    MAXINT = 10
    n = len(a)
    for j in range(n):
        minv = MAXINT
        minp = -1
        for i in range(j,n):
            if int(a[i][1]) < minv:
                minv = int(a[i][1])
                minp = i
        a[minp],a[j]=a[j],a[minp]
    return a

def stable(a,f):
    b = f(a[:])
    print ' '.join(b)
    p = 1
    while p < 10:
        c = []
        d = []
        for i in range(len(a)):
            if a[i][1] == str(p):
                c.append(a[i])
            if b[i][1] == str(p):
                d.append(b[i])
        for i in range(len(c)):
            if c[i] != d[i]:
                print "Not stable"
                return
        p += 1
    print "Stable"

n = int(raw_input())
cards = raw_input().split(' ')

stable(cards[:],bubble_sort)
stable(cards[:],selection_sort)