from copy import  deepcopy
def bubble_sort(lst):
    size=len(lst)
    for i in xrange(size):
        for j in reversed(range(i+1,size)):
            if lst[j].num<lst[j-1].num:
                tmp=lst[j]
                lst[j]=lst[j-1]
                lst[j-1]=tmp
def selection_sort(lst):
    size=len(lst)
    for i in xrange(size):
        mn=i
        for j in xrange(i+1,size):
            if lst[mn].num>lst[j].num:
                mn=j
        tmp=lst[i]
        lst[i]=lst[mn]
        lst[mn]=tmp
class pock:
    def __init__(self,val):
        self.val=val
        self.num=int(val[1:])
    def __gt__(self,other):
        return self.num>other.num


num=raw_input().strip()
inp=raw_input().strip().split()
arr=[]
for ii in inp:
    arr.append(pock(ii))
bubb=deepcopy(arr)
bubble_sort(bubb)
insec=deepcopy(arr)
selection_sort(insec)
print " ".join(pp.val for pp in bubb)
print "Stable"

print " ".join(pp.val for pp in insec)
ok=1
ln=len(arr)
for i  in xrange(ln):
    if(bubb[i].val!=insec[i].val):
        ok=0
        break
if ok==1:
    print "Stable"
else:
    print "Not stable"
 from copy import  deepcopy def bubble_sort(lst):
    size=len(lst)
    for i in xrange(size):
        for j in reversed(range(i+1,size)):
            if lst[j].num<lst[j-1].num:
                tmp=lst[j]
                lst[j]=lst[j-1]
                lst[j-1]=tmp bubble_sort size=len(lst) size len(lst) len lst for i in xrange(size):
        for j in reversed(range(i+1,size)):
            if lst[j].num<lst[j-1].num:
                tmp=lst[j]
                lst[j]=lst[j-1]
                lst[j-1]=tmp i xrange(size) xrange size for j in reversed(range(i+1,size)):
            if lst[j].num<lst[j-1].num:
                tmp=lst[j]
                lst[j]=lst[j-1]
                lst[j-1]=tmp j reversed(range(i+1,size)) reversed range(i+1,size) range i+1 i 1 size if lst[j].num<lst[j-1].num:
                tmp=lst[j]
                lst[j]=lst[j-1]
                lst[j-1]=tmp lst[j].num<lst[j-1].num lst[j].num [j] lst j num lst[j-1].num [j-1] lst j-1 j 1 num tmp=lst[j] tmp lst[j] lst j lst[j]=lst[j-1] lst[j] lst j lst[j-1] lst j-1 j 1 lst[j-1]=tmp lst[j-1] lst j-1 j 1 tmp lst lst def selection_sort(lst):
    size=len(lst)
    for i in xrange(size):
        mn=i
        for j in xrange(i+1,size):
            if lst[mn].num>lst[j].num:
                mn=j
        tmp=lst[i]
        lst[i]=lst[mn]
        lst[mn]=tmp selection_sort size=len(lst) size len(lst) len lst for i in xrange(size):
        mn=i
        for j in xrange(i+1,size):
            if lst[mn].num>lst[j].num:
                mn=j
        tmp=lst[i]
        lst[i]=lst[mn]
        lst[mn]=tmp i xrange(size) xrange size mn=i mn i for j in xrange(i+1,size):
            if lst[mn].num>lst[j].num:
                mn=j
         j xrange(i+1,size) xrange i+1 i 1 size if lst[mn].num>lst[j].num:
                mn=j
         lst[mn].num>lst[j].num lst[mn].num [mn] lst mn num lst[j].num [j] lst j num mn=j mn j tmp=lst[i] tmp lst[i] lst i lst[i]=lst[mn] lst[i] lst i lst[mn] lst mn lst[mn]=tmp lst[mn] lst mn tmp lst lst class pock:
    def __init__(self,val):
        self.val=val
        self.num=int(val[1:])
    def __gt__(self,other):
        return self.num>other.num pock def __init__(self,val):
        self.val=val
        self.num=int(val[1:])
     __init__ self.val=val self.val self val val self.num=int(val[1:]) self.num self num int(val[1:]) int val[1:] val 1: 1 self self val val def __gt__(self,other):
        return self.num>other.num __gt__ return self.num>other.num self.num>other.num self.num self num other.num other num self self other other num=raw_input().strip() num raw_input().strip() ().strip () raw_input strip inp=raw_input().strip().split() inp raw_input().strip().split() ().strip().split ().strip() ().strip () raw_input strip split arr=[] arr [] for ii in inp:
    arr.append(pock(ii)) ii inp arr.append(pock(ii)) arr.append arr append pock(ii) pock ii bubb=deepcopy(arr) bubb deepcopy(arr) deepcopy arr bubble_sort(bubb) bubble_sort bubb insec=deepcopy(arr) insec deepcopy(arr) deepcopy arr selection_sort(insec) selection_sort insec print " ".join(pp.val for pp in bubb) " ".join " " join pp.val pp val print print " ".join(pp.val for pp in insec) " ".join " " join pp.val pp val ok=1 ok 1 ln=len(arr) ln len(arr) len arr for i  in xrange(ln):
    if(bubb[i].val!=insec[i].val):
        ok=0
        break i xrange(ln) xrange ln if(bubb[i].val!=insec[i].val):
        ok=0
        break bubb[i].val!=insec[i].val bubb[i].val [i] bubb i val insec[i].val [i] insec i val ok=0 ok 0 break if ok==1:
    print "Stable"
else:
    print "Not stable" ok==1 ok 1 print print ln=len(arr) len(arr) ln arr=[] [] arr def selection_sort(lst):
    size=len(lst)
    for i in xrange(size):
        mn=i
        for j in xrange(i+1,size):
            if lst[mn].num>lst[j].num:
                mn=j
        tmp=lst[i]
        lst[i]=lst[mn]
        lst[mn]=tmp def selection_sort(lst):
    size=len(lst)
    for i in xrange(size):
        mn=i
        for j in xrange(i+1,size):
            if lst[mn].num>lst[j].num:
                mn=j
        tmp=lst[i]
        lst[i]=lst[mn]
        lst[mn]=tmp selection_sort class pock:
    def __init__(self,val):
        self.val=val
        self.num=int(val[1:])
    def __gt__(self,other):
        return self.num>other.num class pock:
    def __init__(self,val):
        self.val=val
        self.num=int(val[1:])
    def __gt__(self,other):
        return self.num>other.num pock ok=1 1 ok num=raw_input().strip() raw_input().strip() num bubb=deepcopy(arr) deepcopy(arr) bubb ok=0 0 ok insec=deepcopy(arr) deepcopy(arr) insec def bubble_sort(lst):
    size=len(lst)
    for i in xrange(size):
        for j in reversed(range(i+1,size)):
            if lst[j].num<lst[j-1].num:
                tmp=lst[j]
                lst[j]=lst[j-1]
                lst[j-1]=tmp def bubble_sort(lst):
    size=len(lst)
    for i in xrange(size):
        for j in reversed(range(i+1,size)):
            if lst[j].num<lst[j-1].num:
                tmp=lst[j]
                lst[j]=lst[j-1]
                lst[j-1]=tmp bubble_sort inp=raw_input().strip().split() raw_input().strip().split() inp
from copy import  deepcopy
def bubble_sort(lst):
    size=len(lst)
    for i in xrange(size):
        for j in reversed(range(i+1,size)):
            if lst[j].num<lst[j-1].num:
                tmp=lst[j]
                lst[j]=lst[j-1]
                lst[j-1]=tmp
def selection_sort(lst):
    size=len(lst)
    for i in xrange(size):
        mn=i
        for j in xrange(i+1,size):
            if lst[mn].num>lst[j].num:
                mn=j
        tmp=lst[i]
        lst[i]=lst[mn]
        lst[mn]=tmp
class pock:
    def __init__(self,val):
        self.val=val
        self.num=int(val[1:])
    def __gt__(self,other):
        return self.num>other.num


num=raw_input().strip()
inp=raw_input().strip().split()
arr=[]
for ii in inp:
    arr.append(pock(ii))
bubb=deepcopy(arr)
bubble_sort(bubb)
insec=deepcopy(arr)
selection_sort(insec)
print " ".join(pp.val for pp in bubb)
print "Stable"

print " ".join(pp.val for pp in insec)
ok=1
ln=len(arr)
for i  in xrange(ln):
    if(bubb[i].val!=insec[i].val):
        ok=0
        break
if ok==1:
    print "Stable"
else:
    print "Not stable"

