class Cards:
    def __init__(self,shape,value):
        self.shape = shape
        self.value = value

n = int(input())
s = input().split(" ")
cards1 = []
for i in range(n):
    cards1.append(Cards(s[i][0],int(s[i][1])))
cards2 = list(cards1)
for i in range(n):
    for j in range(n-1,i,-1):
        if cards1[j].value < cards1[j-1].value:
            w = cards1[j]
            cards1[j] = cards1[j-1]
            cards1[j-1] = w
for i in range(n):
    if i == n-1:
        print("{0}{1}".format(cards1[i].shape,cards1[i].value))
    else:
        print("{0}{1}".format(cards1[i].shape,cards1[i].value),end=" ")
print("Stable")

for i in range(n):
    minj = i
    for j in range(i+1,n):
        if cards2[j].value < cards2[minj].value:
            minj = j
    w = cards2[i]
    cards2[i] = cards2[minj]
    cards2[minj] = w
for i in range(n):
    if i == n-1:
        print("{0}{1}".format(cards2[i].shape,cards2[i].value))
    else:
        print("{0}{1}".format(cards2[i].shape,cards2[i].value),end=" ")
for i in range(n):
    if cards1[i].shape != cards2[i].shape:
        print("Not stable")
        break
    if i == n-1:
        print("Stable") class Cards:
    def __init__(self,shape,value):
        self.shape = shape
        self.value = value Cards def __init__(self,shape,value):
        self.shape = shape
        self.value = value __init__ self.shape = shape self.shape self shape shape self.value = value self.value self value value self self shape shape value value n = int(input()) n int(input()) int input() input s = input().split(" ") s input().split(" ") ().split () input split " " cards1 = [] cards1 [] for i in range(n):
    cards1.append(Cards(s[i][0],int(s[i][1]))) i range(n) range n cards1.append(Cards(s[i][0],int(s[i][1]))) cards1.append cards1 append Cards(s[i][0],int(s[i][1])) Cards s[i][0] [i] s i 0 int(s[i][1]) int s[i][1] [i] s i 1 cards2 = list(cards1) cards2 list(cards1) list cards1 for i in range(n):
    for j in range(n-1,i,-1):
        if cards1[j].value < cards1[j-1].value:
            w = cards1[j]
            cards1[j] = cards1[j-1]
            cards1[j-1] = w i range(n) range n for j in range(n-1,i,-1):
        if cards1[j].value < cards1[j-1].value:
            w = cards1[j]
            cards1[j] = cards1[j-1]
            cards1[j-1] = w j range(n-1,i,-1) range n-1 n 1 i -1 if cards1[j].value < cards1[j-1].value:
            w = cards1[j]
            cards1[j] = cards1[j-1]
            cards1[j-1] = w cards1[j].value < cards1[j-1].value cards1[j].value [j] cards1 j value cards1[j-1].value [j-1] cards1 j-1 j 1 value w = cards1[j] w cards1[j] cards1 j cards1[j] = cards1[j-1] cards1[j] cards1 j cards1[j-1] cards1 j-1 j 1 cards1[j-1] = w cards1[j-1] cards1 j-1 j 1 w for i in range(n):
    if i == n-1:
        print("{0}{1}".format(cards1[i].shape,cards1[i].value))
    else:
        print("{0}{1}".format(cards1[i].shape,cards1[i].value),end=" ") i range(n) range n if i == n-1:
        print("{0}{1}".format(cards1[i].shape,cards1[i].value))
    else:
        print("{0}{1}".format(cards1[i].shape,cards1[i].value),end=" ") i == n-1 i n-1 n 1 print("{0}{1}".format(cards1[i].shape,cards1[i].value)) print "{0}{1}".format(cards1[i].shape,cards1[i].value) "{0}{1}".format "{0}{1}" format cards1[i].shape [i] cards1 i shape cards1[i].value [i] cards1 i value print("{0}{1}".format(cards1[i].shape,cards1[i].value),end=" ") print "{0}{1}".format(cards1[i].shape,cards1[i].value) "{0}{1}".format "{0}{1}" format cards1[i].shape [i] cards1 i shape cards1[i].value [i] cards1 i value end=" " end " " print("Stable") print "Stable" for i in range(n):
    minj = i
    for j in range(i+1,n):
        if cards2[j].value < cards2[minj].value:
            minj = j
    w = cards2[i]
    cards2[i] = cards2[minj]
    cards2[minj] = w i range(n) range n minj = i minj i for j in range(i+1,n):
        if cards2[j].value < cards2[minj].value:
            minj = j
     j range(i+1,n) range i+1 i 1 n if cards2[j].value < cards2[minj].value:
            minj = j
     cards2[j].value < cards2[minj].value cards2[j].value [j] cards2 j value cards2[minj].value [minj] cards2 minj value minj = j minj j w = cards2[i] w cards2[i] cards2 i cards2[i] = cards2[minj] cards2[i] cards2 i cards2[minj] cards2 minj cards2[minj] = w cards2[minj] cards2 minj w for i in range(n):
    if i == n-1:
        print("{0}{1}".format(cards2[i].shape,cards2[i].value))
    else:
        print("{0}{1}".format(cards2[i].shape,cards2[i].value),end=" ") i range(n) range n if i == n-1:
        print("{0}{1}".format(cards2[i].shape,cards2[i].value))
    else:
        print("{0}{1}".format(cards2[i].shape,cards2[i].value),end=" ") i == n-1 i n-1 n 1 print("{0}{1}".format(cards2[i].shape,cards2[i].value)) print "{0}{1}".format(cards2[i].shape,cards2[i].value) "{0}{1}".format "{0}{1}" format cards2[i].shape [i] cards2 i shape cards2[i].value [i] cards2 i value print("{0}{1}".format(cards2[i].shape,cards2[i].value),end=" ") print "{0}{1}".format(cards2[i].shape,cards2[i].value) "{0}{1}".format "{0}{1}" format cards2[i].shape [i] cards2 i shape cards2[i].value [i] cards2 i value end=" " end " " for i in range(n):
    if cards1[i].shape != cards2[i].shape:
        print("Not stable")
        break
    if i == n-1:
        print("Stable") i range(n) range n if cards1[i].shape != cards2[i].shape:
        print("Not stable")
        break
     cards1[i].shape != cards2[i].shape cards1[i].shape [i] cards1 i shape cards2[i].shape [i] cards2 i shape print("Not stable") print "Not stable" break if i == n-1:
        print("Stable") i == n-1 i n-1 n 1 print("Stable") print "Stable" s = input().split(" ") input().split(" ") s w = cards1[j] cards1[j] w minj = j j minj cards1 = [] [] cards1 n = int(input()) int(input()) n class Cards:
    def __init__(self,shape,value):
        self.shape = shape
        self.value = value class Cards:
    def __init__(self,shape,value):
        self.shape = shape
        self.value = value Cards cards2 = list(cards1) list(cards1) cards2 minj = i i minj w = cards2[i] cards2[i] w
class Cards:
    def __init__(self,shape,value):
        self.shape = shape
        self.value = value

n = int(input())
s = input().split(" ")
cards1 = []
for i in range(n):
    cards1.append(Cards(s[i][0],int(s[i][1])))
cards2 = list(cards1)
for i in range(n):
    for j in range(n-1,i,-1):
        if cards1[j].value < cards1[j-1].value:
            w = cards1[j]
            cards1[j] = cards1[j-1]
            cards1[j-1] = w
for i in range(n):
    if i == n-1:
        print("{0}{1}".format(cards1[i].shape,cards1[i].value))
    else:
        print("{0}{1}".format(cards1[i].shape,cards1[i].value),end=" ")
print("Stable")

for i in range(n):
    minj = i
    for j in range(i+1,n):
        if cards2[j].value < cards2[minj].value:
            minj = j
    w = cards2[i]
    cards2[i] = cards2[minj]
    cards2[minj] = w
for i in range(n):
    if i == n-1:
        print("{0}{1}".format(cards2[i].shape,cards2[i].value))
    else:
        print("{0}{1}".format(cards2[i].shape,cards2[i].value),end=" ")
for i in range(n):
    if cards1[i].shape != cards2[i].shape:
        print("Not stable")
        break
    if i == n-1:
        print("Stable")