def str_selection_sort(a,n):
    m=0
    for i in range(0,n):
        minj=i
        for j in range(i,n):
            if(int(a[minj][1:]) > int(a[j][1:])):
                minj=j
        if(minj!=i):
            m+=1
            a[minj],a[i] = a[i],a[minj]
        
    print(" ".join(a))
    return a

def str_bubble_sort(a,n):
    m=0
    flag=True
    while(flag):
        flag=False
        for i in range(n-1,0,-1):
            if(a[i-1][1:] > a[i][1:]):
                tmp=a[i-1]
                a[i-1]=a[i]
                m+=1
                a[i]=tmp
                flag=True
                
    print(" ".join(a))
    return a

def is_stable(a,b,n):
    for i in range(1,n):
        if(b[i-1][1:]!=b[i][1:]):
            continue
        flag=False
        for j in a:
            if(j[:1]==b[i-1][:1] and (not flag)):
                flag=True
                continue
            if(j[:1]==b[i][:1]):
                if(not flag):
                    return False
                else:
                    return True
        
        if(flag):
            return False
    return True

a=[]
n=int(input())
s=input()
a=s.split()
b=str_bubble_sort(list(a),n)
print("Stable")
c=str_selection_sort(list(a),n)
if(b==c):
    print("Stable")
else:
    print("Not stable") def str_selection_sort(a,n):
    m=0
    for i in range(0,n):
        minj=i
        for j in range(i,n):
            if(int(a[minj][1:]) > int(a[j][1:])):
                minj=j
        if(minj!=i):
            m+=1
            a[minj],a[i] = a[i],a[minj]
        
    print(" ".join(a))
    return a str_selection_sort m=0 m 0 for i in range(0,n):
        minj=i
        for j in range(i,n):
            if(int(a[minj][1:]) > int(a[j][1:])):
                minj=j
        if(minj!=i):
            m+=1
            a[minj],a[i] = a[i],a[minj]
        
     i range(0,n) range 0 n minj=i minj i for j in range(i,n):
            if(int(a[minj][1:]) > int(a[j][1:])):
                minj=j
         j range(i,n) range i n if(int(a[minj][1:]) > int(a[j][1:])):
                minj=j
         int(a[minj][1:]) > int(a[j][1:]) int(a[minj][1:]) int a[minj][1:] [minj] a minj 1: 1 int(a[j][1:]) int a[j][1:] [j] a j 1: 1 minj=j minj j if(minj!=i):
            m+=1
            a[minj],a[i] = a[i],a[minj]
        
     minj!=i minj i m+=1 m 1 a[minj],a[i] = a[i],a[minj] a[minj] a minj a[i] a i a[i] a i a[minj] a minj print(" ".join(a)) print " ".join(a) " ".join " " join a return a a a a n n def str_bubble_sort(a,n):
    m=0
    flag=True
    while(flag):
        flag=False
        for i in range(n-1,0,-1):
            if(a[i-1][1:] > a[i][1:]):
                tmp=a[i-1]
                a[i-1]=a[i]
                m+=1
                a[i]=tmp
                flag=True
                
    print(" ".join(a))
    return a str_bubble_sort m=0 m 0 flag=True flag True while(flag):
        flag=False
        for i in range(n-1,0,-1):
            if(a[i-1][1:] > a[i][1:]):
                tmp=a[i-1]
                a[i-1]=a[i]
                m+=1
                a[i]=tmp
                flag=True
                
     flag flag=False flag False for i in range(n-1,0,-1):
            if(a[i-1][1:] > a[i][1:]):
                tmp=a[i-1]
                a[i-1]=a[i]
                m+=1
                a[i]=tmp
                flag=True
                
     i range(n-1,0,-1) range n-1 n 1 0 -1 if(a[i-1][1:] > a[i][1:]):
                tmp=a[i-1]
                a[i-1]=a[i]
                m+=1
                a[i]=tmp
                flag=True
                
     a[i-1][1:] > a[i][1:] a[i-1][1:] [i-1] a i-1 i 1 1: 1 a[i][1:] [i] a i 1: 1 tmp=a[i-1] tmp a[i-1] a i-1 i 1 a[i-1]=a[i] a[i-1] a i-1 i 1 a[i] a i m+=1 m 1 a[i]=tmp a[i] a i tmp flag=True flag True print(" ".join(a)) print " ".join(a) " ".join " " join a return a a a a n n def is_stable(a,b,n):
    for i in range(1,n):
        if(b[i-1][1:]!=b[i][1:]):
            continue
        flag=False
        for j in a:
            if(j[:1]==b[i-1][:1] and (not flag)):
                flag=True
                continue
            if(j[:1]==b[i][:1]):
                if(not flag):
                    return False
                else:
                    return True
        
        if(flag):
            return False
    return True is_stable for i in range(1,n):
        if(b[i-1][1:]!=b[i][1:]):
            continue
        flag=False
        for j in a:
            if(j[:1]==b[i-1][:1] and (not flag)):
                flag=True
                continue
            if(j[:1]==b[i][:1]):
                if(not flag):
                    return False
                else:
                    return True
        
        if(flag):
            return False
     i range(1,n) range 1 n if(b[i-1][1:]!=b[i][1:]):
            continue
         b[i-1][1:]!=b[i][1:] b[i-1][1:] [i-1] b i-1 i 1 1: 1 b[i][1:] [i] b i 1: 1 continue flag=False flag False for j in a:
            if(j[:1]==b[i-1][:1] and (not flag)):
                flag=True
                continue
            if(j[:1]==b[i][:1]):
                if(not flag):
                    return False
                else:
                    return True
        
         j a if(j[:1]==b[i-1][:1] and (not flag)):
                flag=True
                continue
             j[:1]==b[i-1][:1] and (not flag) j[:1]==b[i-1][:1] j[:1] j :1 1 b[i-1][:1] [i-1] b i-1 i 1 :1 1 not flag flag flag=True flag True continue if(j[:1]==b[i][:1]):
                if(not flag):
                    return False
                else:
                    return True
        
         j[:1]==b[i][:1] j[:1] j :1 1 b[i][:1] [i] b i :1 1 if(not flag):
                    return False
                else:
                    return True
        
         not flag flag return False False return True True if(flag):
            return False
     flag return False False return True True a a b b n n a=[] a [] n=int(input()) n int(input()) int input() input s=input() s input() input a=s.split() a s.split() s.split s split b=str_bubble_sort(list(a),n) b str_bubble_sort(list(a),n) str_bubble_sort list(a) list a n print("Stable") print "Stable" c=str_selection_sort(list(a),n) c str_selection_sort(list(a),n) str_selection_sort list(a) list a n if(b==c):
    print("Stable")
else:
    print("Not stable") b==c b c print("Stable") print "Stable" print("Not stable") print "Not stable" def is_stable(a,b,n):
    for i in range(1,n):
        if(b[i-1][1:]!=b[i][1:]):
            continue
        flag=False
        for j in a:
            if(j[:1]==b[i-1][:1] and (not flag)):
                flag=True
                continue
            if(j[:1]==b[i][:1]):
                if(not flag):
                    return False
                else:
                    return True
        
        if(flag):
            return False
    return True def is_stable(a,b,n):
    for i in range(1,n):
        if(b[i-1][1:]!=b[i][1:]):
            continue
        flag=False
        for j in a:
            if(j[:1]==b[i-1][:1] and (not flag)):
                flag=True
                continue
            if(j[:1]==b[i][:1]):
                if(not flag):
                    return False
                else:
                    return True
        
        if(flag):
            return False
    return True is_stable c=str_selection_sort(list(a),n) str_selection_sort(list(a),n) c def str_selection_sort(a,n):
    m=0
    for i in range(0,n):
        minj=i
        for j in range(i,n):
            if(int(a[minj][1:]) > int(a[j][1:])):
                minj=j
        if(minj!=i):
            m+=1
            a[minj],a[i] = a[i],a[minj]
        
    print(" ".join(a))
    return a def str_selection_sort(a,n):
    m=0
    for i in range(0,n):
        minj=i
        for j in range(i,n):
            if(int(a[minj][1:]) > int(a[j][1:])):
                minj=j
        if(minj!=i):
            m+=1
            a[minj],a[i] = a[i],a[minj]
        
    print(" ".join(a))
    return a str_selection_sort b=str_bubble_sort(list(a),n) str_bubble_sort(list(a),n) b n=int(input()) int(input()) n def str_bubble_sort(a,n):
    m=0
    flag=True
    while(flag):
        flag=False
        for i in range(n-1,0,-1):
            if(a[i-1][1:] > a[i][1:]):
                tmp=a[i-1]
                a[i-1]=a[i]
                m+=1
                a[i]=tmp
                flag=True
                
    print(" ".join(a))
    return a def str_bubble_sort(a,n):
    m=0
    flag=True
    while(flag):
        flag=False
        for i in range(n-1,0,-1):
            if(a[i-1][1:] > a[i][1:]):
                tmp=a[i-1]
                a[i-1]=a[i]
                m+=1
                a[i]=tmp
                flag=True
                
    print(" ".join(a))
    return a str_bubble_sort s=input() input() s a=[] [] a a=s.split() s.split() a
def str_selection_sort(a,n):
    m=0
    for i in range(0,n):
        minj=i
        for j in range(i,n):
            if(int(a[minj][1:]) > int(a[j][1:])):
                minj=j
        if(minj!=i):
            m+=1
            a[minj],a[i] = a[i],a[minj]
        
    print(" ".join(a))
    return a

def str_bubble_sort(a,n):
    m=0
    flag=True
    while(flag):
        flag=False
        for i in range(n-1,0,-1):
            if(a[i-1][1:] > a[i][1:]):
                tmp=a[i-1]
                a[i-1]=a[i]
                m+=1
                a[i]=tmp
                flag=True
                
    print(" ".join(a))
    return a

def is_stable(a,b,n):
    for i in range(1,n):
        if(b[i-1][1:]!=b[i][1:]):
            continue
        flag=False
        for j in a:
            if(j[:1]==b[i-1][:1] and (not flag)):
                flag=True
                continue
            if(j[:1]==b[i][:1]):
                if(not flag):
                    return False
                else:
                    return True
        
        if(flag):
            return False
    return True

a=[]
n=int(input())
s=input()
a=s.split()
b=str_bubble_sort(list(a),n)
print("Stable")
c=str_selection_sort(list(a),n)
if(b==c):
    print("Stable")
else:
    print("Not stable")