# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_2_C
# Stable Sort
# Result:
# Modifications
# - modify inner loop start value of bubble_sort
#   This was the cause of the previous wrong answer.
# - improve performance of is_stable function
import sys

ary_len = int(sys.stdin.readline())
ary_str = sys.stdin.readline().rstrip().split(' ')
ary = map(lambda s: (int(s[1]), s), ary_str)

# a is an array of tupples whose format is like (4, 'C4').
def bubble_sort(a):
    a_len = len(a)
    for i in range(0, a_len):
        for j in reversed(range(i + 1, a_len)):
            if a[j][0] < a[j - 1][0]:
                v = a[j]
                a[j] = a[j - 1]
                a[j - 1] = v

# a is an array of tupples whose format is like (4, 'C4').
def selection_sort(a):
    a_len = len(a)
    for i in range(0, a_len):
        minj = i;
        for j in range(i, a_len):
            if a[j][0] < a[minj][0]:
                minj = j
        if minj != i:
            v = a[i]
            a[i] = a[minj]
            a[minj] = v

def print_ary(a):
    vals = map(lambda s: s[1], a)
    print ' '.join(vals)

def is_stable(before, after):
    length = len(before)
    for i in range(0, length):
        for j in range(i + 1, length):
            if before[i][0] == before[j][0]:
                v1 = before[i][1]
                v2 = before[j][1]
                for k in range(0, length):
                    if after[k][1] == v1:
                        v1_idx_after = k
                        break
                for k in range(0, length):
                    if after[k][1] == v2:
                        v2_idx_after = k
                        break
                if v1_idx_after > v2_idx_after:
                    return False
    return True

def run_sort(ary, sort_fn):
    ary1 = list(ary)
    sort_fn(ary1)
    print_ary(ary1)
    if is_stable(ary, ary1):
        print 'Stable'
    else:
        print 'Not stable'

### main
run_sort(ary, bubble_sort)
run_sort(ary, selection_sort) import sys sys ary_len = int(sys.stdin.readline()) ary_len int(sys.stdin.readline()) int sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline ary_str = sys.stdin.readline().rstrip().split(' ') ary_str sys.stdin.readline().rstrip().split(' ') sys.stdin.readline().rstrip().split sys.stdin.readline().rstrip() sys.stdin.readline().rstrip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline rstrip split ' ' ary = map(lambda s: (int(s[1]), s), ary_str) ary map(lambda s: (int(s[1]), s), ary_str) map lambda s: (int(s[1]), s) (int(s[1]), s) int(s[1]) int s[1] s 1 s s ary_str def bubble_sort(a):
    a_len = len(a)
    for i in range(0, a_len):
        for j in reversed(range(i + 1, a_len)):
            if a[j][0] < a[j - 1][0]:
                v = a[j]
                a[j] = a[j - 1]
                a[j - 1] = v

# a is an array of tupples whose format is like (4, 'C4'). bubble_sort a_len = len(a) a_len len(a) len a for i in range(0, a_len):
        for j in reversed(range(i + 1, a_len)):
            if a[j][0] < a[j - 1][0]:
                v = a[j]
                a[j] = a[j - 1]
                a[j - 1] = v

# a is an array of tupples whose format is like (4, 'C4'). i range(0, a_len) range 0 a_len for j in reversed(range(i + 1, a_len)):
            if a[j][0] < a[j - 1][0]:
                v = a[j]
                a[j] = a[j - 1]
                a[j - 1] = v

# a is an array of tupples whose format is like (4, 'C4'). j reversed(range(i + 1, a_len)) reversed range(i + 1, a_len) range i + 1 i 1 a_len if a[j][0] < a[j - 1][0]:
                v = a[j]
                a[j] = a[j - 1]
                a[j - 1] = v

# a is an array of tupples whose format is like (4, 'C4'). a[j][0] < a[j - 1][0] a[j][0] [j] a j 0 a[j - 1][0] [j - 1] a j - 1 j 1 0 v = a[j] v a[j] a j a[j] = a[j - 1] a[j] a j a[j - 1] a j - 1 j 1 a[j - 1] = v a[j - 1] a j - 1 j 1 v a a def selection_sort(a):
    a_len = len(a)
    for i in range(0, a_len):
        minj = i;
        for j in range(i, a_len):
            if a[j][0] < a[minj][0]:
                minj = j
        if minj != i:
            v = a[i]
            a[i] = a[minj]
            a[minj] = v selection_sort a_len = len(a) a_len len(a) len a for i in range(0, a_len):
        minj = i;
        for j in range(i, a_len):
            if a[j][0] < a[minj][0]:
                minj = j
        if minj != i:
            v = a[i]
            a[i] = a[minj]
            a[minj] = v i range(0, a_len) range 0 a_len minj = i minj i for j in range(i, a_len):
            if a[j][0] < a[minj][0]:
                minj = j
         j range(i, a_len) range i a_len if a[j][0] < a[minj][0]:
                minj = j
         a[j][0] < a[minj][0] a[j][0] [j] a j 0 a[minj][0] [minj] a minj 0 minj = j minj j if minj != i:
            v = a[i]
            a[i] = a[minj]
            a[minj] = v minj != i minj i v = a[i] v a[i] a i a[i] = a[minj] a[i] a i a[minj] a minj a[minj] = v a[minj] a minj v a a def print_ary(a):
    vals = map(lambda s: s[1], a)
    print ' '.join(vals) print_ary vals = map(lambda s: s[1], a) vals map(lambda s: s[1], a) map lambda s: s[1] s[1] s 1 s a print ' '.join(vals) ' '.join ' ' join vals a a def is_stable(before, after):
    length = len(before)
    for i in range(0, length):
        for j in range(i + 1, length):
            if before[i][0] == before[j][0]:
                v1 = before[i][1]
                v2 = before[j][1]
                for k in range(0, length):
                    if after[k][1] == v1:
                        v1_idx_after = k
                        break
                for k in range(0, length):
                    if after[k][1] == v2:
                        v2_idx_after = k
                        break
                if v1_idx_after > v2_idx_after:
                    return False
    return True is_stable length = len(before) length len(before) len before for i in range(0, length):
        for j in range(i + 1, length):
            if before[i][0] == before[j][0]:
                v1 = before[i][1]
                v2 = before[j][1]
                for k in range(0, length):
                    if after[k][1] == v1:
                        v1_idx_after = k
                        break
                for k in range(0, length):
                    if after[k][1] == v2:
                        v2_idx_after = k
                        break
                if v1_idx_after > v2_idx_after:
                    return False
     i range(0, length) range 0 length for j in range(i + 1, length):
            if before[i][0] == before[j][0]:
                v1 = before[i][1]
                v2 = before[j][1]
                for k in range(0, length):
                    if after[k][1] == v1:
                        v1_idx_after = k
                        break
                for k in range(0, length):
                    if after[k][1] == v2:
                        v2_idx_after = k
                        break
                if v1_idx_after > v2_idx_after:
                    return False
     j range(i + 1, length) range i + 1 i 1 length if before[i][0] == before[j][0]:
                v1 = before[i][1]
                v2 = before[j][1]
                for k in range(0, length):
                    if after[k][1] == v1:
                        v1_idx_after = k
                        break
                for k in range(0, length):
                    if after[k][1] == v2:
                        v2_idx_after = k
                        break
                if v1_idx_after > v2_idx_after:
                    return False
     before[i][0] == before[j][0] before[i][0] [i] before i 0 before[j][0] [j] before j 0 v1 = before[i][1] v1 before[i][1] [i] before i 1 v2 = before[j][1] v2 before[j][1] [j] before j 1 for k in range(0, length):
                    if after[k][1] == v1:
                        v1_idx_after = k
                        break
                 k range(0, length) range 0 length if after[k][1] == v1:
                        v1_idx_after = k
                        break
                 after[k][1] == v1 after[k][1] [k] after k 1 v1 v1_idx_after = k v1_idx_after k break for k in range(0, length):
                    if after[k][1] == v2:
                        v2_idx_after = k
                        break
                 k range(0, length) range 0 length if after[k][1] == v2:
                        v2_idx_after = k
                        break
                 after[k][1] == v2 after[k][1] [k] after k 1 v2 v2_idx_after = k v2_idx_after k break if v1_idx_after > v2_idx_after:
                    return False
     v1_idx_after > v2_idx_after v1_idx_after v2_idx_after return False False return True True before before after after def run_sort(ary, sort_fn):
    ary1 = list(ary)
    sort_fn(ary1)
    print_ary(ary1)
    if is_stable(ary, ary1):
        print 'Stable'
    else:
        print 'Not stable'

### main run_sort ary1 = list(ary) ary1 list(ary) list ary sort_fn(ary1) sort_fn ary1 print_ary(ary1) print_ary ary1 if is_stable(ary, ary1):
        print 'Stable'
    else:
        print 'Not stable'

### main is_stable(ary, ary1) is_stable ary ary1 print print ary ary sort_fn sort_fn run_sort(ary, bubble_sort) run_sort ary bubble_sort run_sort(ary, selection_sort) run_sort ary selection_sort ary_len = int(sys.stdin.readline()) int(sys.stdin.readline()) ary_len def selection_sort(a):
    a_len = len(a)
    for i in range(0, a_len):
        minj = i;
        for j in range(i, a_len):
            if a[j][0] < a[minj][0]:
                minj = j
        if minj != i:
            v = a[i]
            a[i] = a[minj]
            a[minj] = v def selection_sort(a):
    a_len = len(a)
    for i in range(0, a_len):
        minj = i;
        for j in range(i, a_len):
            if a[j][0] < a[minj][0]:
                minj = j
        if minj != i:
            v = a[i]
            a[i] = a[minj]
            a[minj] = v selection_sort def print_ary(a):
    vals = map(lambda s: s[1], a)
    print ' '.join(vals) def print_ary(a):
    vals = map(lambda s: s[1], a)
    print ' '.join(vals) print_ary def is_stable(before, after):
    length = len(before)
    for i in range(0, length):
        for j in range(i + 1, length):
            if before[i][0] == before[j][0]:
                v1 = before[i][1]
                v2 = before[j][1]
                for k in range(0, length):
                    if after[k][1] == v1:
                        v1_idx_after = k
                        break
                for k in range(0, length):
                    if after[k][1] == v2:
                        v2_idx_after = k
                        break
                if v1_idx_after > v2_idx_after:
                    return False
    return True def is_stable(before, after):
    length = len(before)
    for i in range(0, length):
        for j in range(i + 1, length):
            if before[i][0] == before[j][0]:
                v1 = before[i][1]
                v2 = before[j][1]
                for k in range(0, length):
                    if after[k][1] == v1:
                        v1_idx_after = k
                        break
                for k in range(0, length):
                    if after[k][1] == v2:
                        v2_idx_after = k
                        break
                if v1_idx_after > v2_idx_after:
                    return False
    return True is_stable ary_str = sys.stdin.readline().rstrip().split(' ') sys.stdin.readline().rstrip().split(' ') ary_str def run_sort(ary, sort_fn):
    ary1 = list(ary)
    sort_fn(ary1)
    print_ary(ary1)
    if is_stable(ary, ary1):
        print 'Stable'
    else:
        print 'Not stable'

### main def run_sort(ary, sort_fn):
    ary1 = list(ary)
    sort_fn(ary1)
    print_ary(ary1)
    if is_stable(ary, ary1):
        print 'Stable'
    else:
        print 'Not stable'

### main run_sort def bubble_sort(a):
    a_len = len(a)
    for i in range(0, a_len):
        for j in reversed(range(i + 1, a_len)):
            if a[j][0] < a[j - 1][0]:
                v = a[j]
                a[j] = a[j - 1]
                a[j - 1] = v

# a is an array of tupples whose format is like (4, 'C4'). def bubble_sort(a):
    a_len = len(a)
    for i in range(0, a_len):
        for j in reversed(range(i + 1, a_len)):
            if a[j][0] < a[j - 1][0]:
                v = a[j]
                a[j] = a[j - 1]
                a[j - 1] = v

# a is an array of tupples whose format is like (4, 'C4'). bubble_sort ary = map(lambda s: (int(s[1]), s), ary_str) map(lambda s: (int(s[1]), s), ary_str) ary
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_2_C
# Stable Sort
# Result:
# Modifications
# - modify inner loop start value of bubble_sort
#   This was the cause of the previous wrong answer.
# - improve performance of is_stable function
import sys

ary_len = int(sys.stdin.readline())
ary_str = sys.stdin.readline().rstrip().split(' ')
ary = map(lambda s: (int(s[1]), s), ary_str)

# a is an array of tupples whose format is like (4, 'C4').
def bubble_sort(a):
    a_len = len(a)
    for i in range(0, a_len):
        for j in reversed(range(i + 1, a_len)):
            if a[j][0] < a[j - 1][0]:
                v = a[j]
                a[j] = a[j - 1]
                a[j - 1] = v

# a is an array of tupples whose format is like (4, 'C4').
def selection_sort(a):
    a_len = len(a)
    for i in range(0, a_len):
        minj = i;
        for j in range(i, a_len):
            if a[j][0] < a[minj][0]:
                minj = j
        if minj != i:
            v = a[i]
            a[i] = a[minj]
            a[minj] = v

def print_ary(a):
    vals = map(lambda s: s[1], a)
    print ' '.join(vals)

def is_stable(before, after):
    length = len(before)
    for i in range(0, length):
        for j in range(i + 1, length):
            if before[i][0] == before[j][0]:
                v1 = before[i][1]
                v2 = before[j][1]
                for k in range(0, length):
                    if after[k][1] == v1:
                        v1_idx_after = k
                        break
                for k in range(0, length):
                    if after[k][1] == v2:
                        v2_idx_after = k
                        break
                if v1_idx_after > v2_idx_after:
                    return False
    return True

def run_sort(ary, sort_fn):
    ary1 = list(ary)
    sort_fn(ary1)
    print_ary(ary1)
    if is_stable(ary, ary1):
        print 'Stable'
    else:
        print 'Not stable'

### main
run_sort(ary, bubble_sort)
run_sort(ary, selection_sort)