def bubble(x):
    x = x + []
    n = len(x)
    for i in range(n):
        for j in range(i+1,n)[::-1]:
            if x[j] % 10 < x[j-1] % 10:
                x[j], x[j-1] = x[j-1], x[j]
    return x

def selection(x):
    x = x + []
    n = len(x)
    for i in range(n):
        mini = i
        for j in range(i, n):
            if x[j] % 10 < x[mini] % 10:
                mini = j
        if i != mini:
            x[i], x[mini] = x[mini], x[i]
    return x
    
def order(x):
    y = []
    for i in range(1,9):
        y.extend([e for e in x if e % 10 == i])
    return y

def output(x):
    for e in x:
        print 'SHDC'[e/10]+str(e % 10),
    print

def check(x, y):
    output(x)
    if y == order(x): 
        print 'Stable'
    else:
        print 'Not stable'
    return

n = input()
x = []
for s in raw_input().split():
    x.append('SHDC'.index(s[0])*10 + int(s[1]))
x0 = order(x)
check(bubble(x), x0)
check(selection(x), x0) def bubble(x):
    x = x + []
    n = len(x)
    for i in range(n):
        for j in range(i+1,n)[::-1]:
            if x[j] % 10 < x[j-1] % 10:
                x[j], x[j-1] = x[j-1], x[j]
    return x bubble x = x + [] x x + [] x [] n = len(x) n len(x) len x for i in range(n):
        for j in range(i+1,n)[::-1]:
            if x[j] % 10 < x[j-1] % 10:
                x[j], x[j-1] = x[j-1], x[j]
     i range(n) range n for j in range(i+1,n)[::-1]:
            if x[j] % 10 < x[j-1] % 10:
                x[j], x[j-1] = x[j-1], x[j]
     j range(i+1,n)[::-1] (i+1,n) range i+1 i 1 n ::-1 -1 if x[j] % 10 < x[j-1] % 10:
                x[j], x[j-1] = x[j-1], x[j]
     x[j] % 10 < x[j-1] % 10 x[j] % 10 x[j] x j 10 x[j-1] % 10 x[j-1] x j-1 j 1 10 x[j], x[j-1] = x[j-1], x[j] x[j] x j x[j-1] x j-1 j 1 x[j-1] x j-1 j 1 x[j] x j return x x x x def selection(x):
    x = x + []
    n = len(x)
    for i in range(n):
        mini = i
        for j in range(i, n):
            if x[j] % 10 < x[mini] % 10:
                mini = j
        if i != mini:
            x[i], x[mini] = x[mini], x[i]
    return x
     selection x = x + [] x x + [] x [] n = len(x) n len(x) len x for i in range(n):
        mini = i
        for j in range(i, n):
            if x[j] % 10 < x[mini] % 10:
                mini = j
        if i != mini:
            x[i], x[mini] = x[mini], x[i]
     i range(n) range n mini = i mini i for j in range(i, n):
            if x[j] % 10 < x[mini] % 10:
                mini = j
         j range(i, n) range i n if x[j] % 10 < x[mini] % 10:
                mini = j
         x[j] % 10 < x[mini] % 10 x[j] % 10 x[j] x j 10 x[mini] % 10 x[mini] x mini 10 mini = j mini j if i != mini:
            x[i], x[mini] = x[mini], x[i]
     i != mini i mini x[i], x[mini] = x[mini], x[i] x[i] x i x[mini] x mini x[mini] x mini x[i] x i return x x x x def order(x):
    y = []
    for i in range(1,9):
        y.extend([e for e in x if e % 10 == i])
    return y order y = [] y [] for i in range(1,9):
        y.extend([e for e in x if e % 10 == i])
     i range(1,9) range 1 9 e for e in x if e % 10 == i for e in x if e % 10 == i e x e % 10 == i e % 10 e 10 i if e % 10 == i e y.extend([e for e in x if e % 10 == i]) y.extend y extend [e for e in x if e % 10 == i] return y y x x def output(x):
    for e in x:
        print 'SHDC'[e/10]+str(e % 10),
    print output for e in x:
        print 'SHDC'[e/10]+str(e % 10),
     e x print 'SHDC'[e/10]+str(e % 10) 'SHDC'[e/10] 'SHDC' e/10 e 10 str(e % 10) str e % 10 e 10 print x x def check(x, y):
    output(x)
    if y == order(x): 
        print 'Stable'
    else:
        print 'Not stable'
    return check output(x) output x if y == order(x): 
        print 'Stable'
    else:
        print 'Not stable'
     y == order(x) y order(x) order x print print return x x y y n = input() n input() input x = [] x [] for s in raw_input().split():
    x.append('SHDC'.index(s[0])*10 + int(s[1])) s raw_input().split() ().split () raw_input split x.append('SHDC'.index(s[0])*10 + int(s[1])) x.append x append 'SHDC'.index(s[0])*10 + int(s[1]) 'SHDC'.index(s[0])*10 'SHDC'.index(s[0]) 'SHDC'.index 'SHDC' index s[0] s 0 10 int(s[1]) int s[1] s 1 x0 = order(x) x0 order(x) order x check(bubble(x), x0) check bubble(x) bubble x x0 check(selection(x), x0) check selection(x) selection x x0 def check(x, y):
    output(x)
    if y == order(x): 
        print 'Stable'
    else:
        print 'Not stable'
    return def check(x, y):
    output(x)
    if y == order(x): 
        print 'Stable'
    else:
        print 'Not stable'
    return check x0 = order(x) order(x) x0 n = input() input() n def output(x):
    for e in x:
        print 'SHDC'[e/10]+str(e % 10),
    print def output(x):
    for e in x:
        print 'SHDC'[e/10]+str(e % 10),
    print output def bubble(x):
    x = x + []
    n = len(x)
    for i in range(n):
        for j in range(i+1,n)[::-1]:
            if x[j] % 10 < x[j-1] % 10:
                x[j], x[j-1] = x[j-1], x[j]
    return x def bubble(x):
    x = x + []
    n = len(x)
    for i in range(n):
        for j in range(i+1,n)[::-1]:
            if x[j] % 10 < x[j-1] % 10:
                x[j], x[j-1] = x[j-1], x[j]
    return x bubble x = [] [] x def order(x):
    y = []
    for i in range(1,9):
        y.extend([e for e in x if e % 10 == i])
    return y def order(x):
    y = []
    for i in range(1,9):
        y.extend([e for e in x if e % 10 == i])
    return y order def selection(x):
    x = x + []
    n = len(x)
    for i in range(n):
        mini = i
        for j in range(i, n):
            if x[j] % 10 < x[mini] % 10:
                mini = j
        if i != mini:
            x[i], x[mini] = x[mini], x[i]
    return x
     def selection(x):
    x = x + []
    n = len(x)
    for i in range(n):
        mini = i
        for j in range(i, n):
            if x[j] % 10 < x[mini] % 10:
                mini = j
        if i != mini:
            x[i], x[mini] = x[mini], x[i]
    return x
     selection
def bubble(x):
    x = x + []
    n = len(x)
    for i in range(n):
        for j in range(i+1,n)[::-1]:
            if x[j] % 10 < x[j-1] % 10:
                x[j], x[j-1] = x[j-1], x[j]
    return x

def selection(x):
    x = x + []
    n = len(x)
    for i in range(n):
        mini = i
        for j in range(i, n):
            if x[j] % 10 < x[mini] % 10:
                mini = j
        if i != mini:
            x[i], x[mini] = x[mini], x[i]
    return x
    
def order(x):
    y = []
    for i in range(1,9):
        y.extend([e for e in x if e % 10 == i])
    return y

def output(x):
    for e in x:
        print 'SHDC'[e/10]+str(e % 10),
    print

def check(x, y):
    output(x)
    if y == order(x): 
        print 'Stable'
    else:
        print 'Not stable'
    return

n = input()
x = []
for s in raw_input().split():
    x.append('SHDC'.index(s[0])*10 + int(s[1]))
x0 = order(x)
check(bubble(x), x0)
check(selection(x), x0)