class card(object):
    def __init__(self, str):
        self.symbol = str[0]
        self.num = int(str[1:])
    
    def __lt__(self,obj):
        if(self.num < obj.num):
            return True
        else:
            return False
    def __gt__(self,obj):
        if(self.num > obj.num):
            return True
        else:
            return False

    def display_card(self):
        print("{0}{1}".format(self.symbol,self.num),end="")

n = int(input())
s = input()
org = list(map(card,s.split()))  

bub = list(org)
sel = list(org)

#####################################
"""
Bubble sort(bub[])
""" '''
for i in range(0,len(bub)):
    for j in range(len(bub)-1,i-1,-1):
        if(bub[j] < bub[j-1]):
            tmp = bub[j]
            bub[j] = bub[j-1]
            bub[j-1] = tmp
'''
flag = True
while(flag==True):
    flag = False
    for i in range(1,len(bub)):
        if(bub[i]<bub[i-1]):
            bub[i],bub[i-1] = bub[i-1],bub[i]
            flag = True


"""
selection sort(sel[])
"""

for i in range(0,len(sel)-1):
    minj = i
    for j in range(i,len(sel)):
        if(sel[j] < sel[minj]):
            minj = j
    sel[i],sel[minj] = sel[minj],sel[i]
       
"""
comp org(org[])
"""


# buble

for i in range(0,len(bub)):
    bub[i].display_card()
    if (i==len(bub)-1):
        print("\n",end="")
    else :
        print(" ",end="")
print("Stable")
# selec
                
for i in range(0,len(sel)):
    sel[i].display_card()
    if (i==len(sel)-1):
        print("\n",end="")
    else :
        print(" ",end="")
if(bub == sel): 
    print("Stable")
else:
    print("Not stable") class card(object):
    def __init__(self, str):
        self.symbol = str[0]
        self.num = int(str[1:])
    
    def __lt__(self,obj):
        if(self.num < obj.num):
            return True
        else:
            return False
    def __gt__(self,obj):
        if(self.num > obj.num):
            return True
        else:
            return False

    def display_card(self):
        print("{0}{1}".format(self.symbol,self.num),end="") card def __init__(self, str):
        self.symbol = str[0]
        self.num = int(str[1:])
    
     __init__ self.symbol = str[0] self.symbol self symbol str[0] str 0 self.num = int(str[1:]) self.num self num int(str[1:]) int str[1:] str 1: 1 self self str str def __lt__(self,obj):
        if(self.num < obj.num):
            return True
        else:
            return False
     __lt__ if(self.num < obj.num):
            return True
        else:
            return False
     self.num < obj.num self.num self num obj.num obj num return True True return False False self self obj obj def __gt__(self,obj):
        if(self.num > obj.num):
            return True
        else:
            return False

     __gt__ if(self.num > obj.num):
            return True
        else:
            return False

     self.num > obj.num self.num self num obj.num obj num return True True return False False self self obj obj def display_card(self):
        print("{0}{1}".format(self.symbol,self.num),end="") display_card print("{0}{1}".format(self.symbol,self.num),end="") print "{0}{1}".format(self.symbol,self.num) "{0}{1}".format "{0}{1}" format self.symbol self symbol self.num self num end="" end "" self self object n = int(input()) n int(input()) int input() input s = input() s input() input org = list(map(card,s.split())) org list(map(card,s.split())) list map(card,s.split()) map card s.split() s.split s split bub = list(org) bub list(org) list org sel = list(org) sel list(org) list org """
Bubble sort(bub[])
""" '''
for i in range(0,len(bub)):
    for j in range(len(bub)-1,i-1,-1):
        if(bub[j] < bub[j-1]):
            tmp = bub[j]
            bub[j] = bub[j-1]
            bub[j-1] = tmp
''' flag = True flag True while(flag==True):
    flag = False
    for i in range(1,len(bub)):
        if(bub[i]<bub[i-1]):
            bub[i],bub[i-1] = bub[i-1],bub[i]
            flag = True flag==True flag True flag = False flag False for i in range(1,len(bub)):
        if(bub[i]<bub[i-1]):
            bub[i],bub[i-1] = bub[i-1],bub[i]
            flag = True i range(1,len(bub)) range 1 len(bub) len bub if(bub[i]<bub[i-1]):
            bub[i],bub[i-1] = bub[i-1],bub[i]
            flag = True bub[i]<bub[i-1] bub[i] bub i bub[i-1] bub i-1 i 1 bub[i],bub[i-1] = bub[i-1],bub[i] bub[i] bub i bub[i-1] bub i-1 i 1 bub[i-1] bub i-1 i 1 bub[i] bub i flag = True flag True """
selection sort(sel[])
""" for i in range(0,len(sel)-1):
    minj = i
    for j in range(i,len(sel)):
        if(sel[j] < sel[minj]):
            minj = j
    sel[i],sel[minj] = sel[minj],sel[i]
        i range(0,len(sel)-1) range 0 len(sel)-1 len(sel) len sel 1 minj = i minj i for j in range(i,len(sel)):
        if(sel[j] < sel[minj]):
            minj = j
     j range(i,len(sel)) range i len(sel) len sel if(sel[j] < sel[minj]):
            minj = j
     sel[j] < sel[minj] sel[j] sel j sel[minj] sel minj minj = j minj j sel[i],sel[minj] = sel[minj],sel[i] sel[i] sel i sel[minj] sel minj sel[minj] sel minj sel[i] sel i """
comp org(org[])
""" for i in range(0,len(bub)):
    bub[i].display_card()
    if (i==len(bub)-1):
        print("\n",end="")
    else :
        print(" ",end="") i range(0,len(bub)) range 0 len(bub) len bub bub[i].display_card() [i].display_card [i] bub i display_card if (i==len(bub)-1):
        print("\n",end="")
    else :
        print(" ",end="") i==len(bub)-1 i len(bub)-1 len(bub) len bub 1 print("\n",end="") print "\n" end="" end "" print(" ",end="") print " " end="" end "" print("Stable") print "Stable" for i in range(0,len(sel)):
    sel[i].display_card()
    if (i==len(sel)-1):
        print("\n",end="")
    else :
        print(" ",end="") i range(0,len(sel)) range 0 len(sel) len sel sel[i].display_card() [i].display_card [i] sel i display_card if (i==len(sel)-1):
        print("\n",end="")
    else :
        print(" ",end="") i==len(sel)-1 i len(sel)-1 len(sel) len sel 1 print("\n",end="") print "\n" end="" end "" print(" ",end="") print " " end="" end "" if(bub == sel): 
    print("Stable")
else:
    print("Not stable") bub == sel bub sel print("Stable") print "Stable" print("Not stable") print "Not stable" sel = list(org) list(org) sel minj = i i minj flag = True True flag flag = True True flag n = int(input()) int(input()) n flag = False False flag s = input() input() s class card(object):
    def __init__(self, str):
        self.symbol = str[0]
        self.num = int(str[1:])
    
    def __lt__(self,obj):
        if(self.num < obj.num):
            return True
        else:
            return False
    def __gt__(self,obj):
        if(self.num > obj.num):
            return True
        else:
            return False

    def display_card(self):
        print("{0}{1}".format(self.symbol,self.num),end="") class card(object):
    def __init__(self, str):
        self.symbol = str[0]
        self.num = int(str[1:])
    
    def __lt__(self,obj):
        if(self.num < obj.num):
            return True
        else:
            return False
    def __gt__(self,obj):
        if(self.num > obj.num):
            return True
        else:
            return False

    def display_card(self):
        print("{0}{1}".format(self.symbol,self.num),end="") card org = list(map(card,s.split())) list(map(card,s.split())) org bub = list(org) list(org) bub minj = j j minj
class card(object):
    def __init__(self, str):
        self.symbol = str[0]
        self.num = int(str[1:])
    
    def __lt__(self,obj):
        if(self.num < obj.num):
            return True
        else:
            return False
    def __gt__(self,obj):
        if(self.num > obj.num):
            return True
        else:
            return False

    def display_card(self):
        print("{0}{1}".format(self.symbol,self.num),end="")

n = int(input())
s = input()
org = list(map(card,s.split()))  

bub = list(org)
sel = list(org)

#####################################
"""
Bubble sort(bub[])
""" '''
for i in range(0,len(bub)):
    for j in range(len(bub)-1,i-1,-1):
        if(bub[j] < bub[j-1]):
            tmp = bub[j]
            bub[j] = bub[j-1]
            bub[j-1] = tmp
'''
flag = True
while(flag==True):
    flag = False
    for i in range(1,len(bub)):
        if(bub[i]<bub[i-1]):
            bub[i],bub[i-1] = bub[i-1],bub[i]
            flag = True


"""
selection sort(sel[])
"""

for i in range(0,len(sel)-1):
    minj = i
    for j in range(i,len(sel)):
        if(sel[j] < sel[minj]):
            minj = j
    sel[i],sel[minj] = sel[minj],sel[i]
       
"""
comp org(org[])
"""


# buble

for i in range(0,len(bub)):
    bub[i].display_card()
    if (i==len(bub)-1):
        print("\n",end="")
    else :
        print(" ",end="")
print("Stable")
# selec
                
for i in range(0,len(sel)):
    sel[i].display_card()
    if (i==len(sel)-1):
        print("\n",end="")
    else :
        print(" ",end="")
if(bub == sel): 
    print("Stable")
else:
    print("Not stable")