def BubbleSort(A):
    #tot = 0
    flag = 1
    N = len(A)
    
    while flag:
        flag = 0
        for j in xrange(N-1,0,-1):
            if A[j][1] < A[j-1][1]:
                A[j],A[j-1] = A[j-1],A[j]
                flag = 1
                #tot += 1
    return A #,tot

def SelectionSort(A): 
    N = len(A)
    m = 0
    for i in range(N):
        minj = i
        for j in range(i,N):
            if A[j][1] < A[minj][1]:
                minj = j
        A[i],A[minj] = A[minj],A[i]
        if i != minj:
            m += 1
    return A

    
N = int(raw_input())
ls = raw_input().split()

tmp = BubbleSort(list(ls))
print ' '.join(map(str,tmp))
print 'Stable'

tmp2 = SelectionSort(list(ls))
print ' '.join(map(str,tmp2))
for k,j in zip(tmp,tmp2):
    if k != j:
        print 'Not stable'
        break
else:
    print 'Stable' def BubbleSort(A):
    #tot = 0
    flag = 1
    N = len(A)
    
    while flag:
        flag = 0
        for j in xrange(N-1,0,-1):
            if A[j][1] < A[j-1][1]:
                A[j],A[j-1] = A[j-1],A[j]
                flag = 1
                #tot += 1
    return A #,tot BubbleSort flag = 1 flag 1 N = len(A) N len(A) len A while flag:
        flag = 0
        for j in xrange(N-1,0,-1):
            if A[j][1] < A[j-1][1]:
                A[j],A[j-1] = A[j-1],A[j]
                flag = 1
                #tot += 1
     flag flag = 0 flag 0 for j in xrange(N-1,0,-1):
            if A[j][1] < A[j-1][1]:
                A[j],A[j-1] = A[j-1],A[j]
                flag = 1
                #tot += 1
     j xrange(N-1,0,-1) xrange N-1 N 1 0 -1 if A[j][1] < A[j-1][1]:
                A[j],A[j-1] = A[j-1],A[j]
                flag = 1
                #tot += 1
     A[j][1] < A[j-1][1] A[j][1] [j] A j 1 A[j-1][1] [j-1] A j-1 j 1 1 A[j],A[j-1] = A[j-1],A[j] A[j] A j A[j-1] A j-1 j 1 A[j-1] A j-1 j 1 A[j] A j flag = 1 flag 1 return A A A A def SelectionSort(A): 
    N = len(A)
    m = 0
    for i in range(N):
        minj = i
        for j in range(i,N):
            if A[j][1] < A[minj][1]:
                minj = j
        A[i],A[minj] = A[minj],A[i]
        if i != minj:
            m += 1
    return A

     SelectionSort N = len(A) N len(A) len A m = 0 m 0 for i in range(N):
        minj = i
        for j in range(i,N):
            if A[j][1] < A[minj][1]:
                minj = j
        A[i],A[minj] = A[minj],A[i]
        if i != minj:
            m += 1
     i range(N) range N minj = i minj i for j in range(i,N):
            if A[j][1] < A[minj][1]:
                minj = j
         j range(i,N) range i N if A[j][1] < A[minj][1]:
                minj = j
         A[j][1] < A[minj][1] A[j][1] [j] A j 1 A[minj][1] [minj] A minj 1 minj = j minj j A[i],A[minj] = A[minj],A[i] A[i] A i A[minj] A minj A[minj] A minj A[i] A i if i != minj:
            m += 1
     i != minj i minj m += 1 m 1 return A A A A N = int(raw_input()) N int(raw_input()) int raw_input() raw_input ls = raw_input().split() ls raw_input().split() ().split () raw_input split tmp = BubbleSort(list(ls)) tmp BubbleSort(list(ls)) BubbleSort list(ls) list ls print ' '.join(map(str,tmp)) ' '.join ' ' join map(str,tmp) map str tmp print tmp2 = SelectionSort(list(ls)) tmp2 SelectionSort(list(ls)) SelectionSort list(ls) list ls print ' '.join(map(str,tmp2)) ' '.join ' ' join map(str,tmp2) map str tmp2 for k,j in zip(tmp,tmp2):
    if k != j:
        print 'Not stable'
        break
else:
    print 'Stable' k j zip(tmp,tmp2) zip tmp tmp2 if k != j:
        print 'Not stable'
        break k != j k j print break print def BubbleSort(A):
    #tot = 0
    flag = 1
    N = len(A)
    
    while flag:
        flag = 0
        for j in xrange(N-1,0,-1):
            if A[j][1] < A[j-1][1]:
                A[j],A[j-1] = A[j-1],A[j]
                flag = 1
                #tot += 1
    return A #,tot def BubbleSort(A):
    #tot = 0
    flag = 1
    N = len(A)
    
    while flag:
        flag = 0
        for j in xrange(N-1,0,-1):
            if A[j][1] < A[j-1][1]:
                A[j],A[j-1] = A[j-1],A[j]
                flag = 1
                #tot += 1
    return A #,tot BubbleSort tmp2 = SelectionSort(list(ls)) SelectionSort(list(ls)) tmp2 N = int(raw_input()) int(raw_input()) N tmp = BubbleSort(list(ls)) BubbleSort(list(ls)) tmp def SelectionSort(A): 
    N = len(A)
    m = 0
    for i in range(N):
        minj = i
        for j in range(i,N):
            if A[j][1] < A[minj][1]:
                minj = j
        A[i],A[minj] = A[minj],A[i]
        if i != minj:
            m += 1
    return A

     def SelectionSort(A): 
    N = len(A)
    m = 0
    for i in range(N):
        minj = i
        for j in range(i,N):
            if A[j][1] < A[minj][1]:
                minj = j
        A[i],A[minj] = A[minj],A[i]
        if i != minj:
            m += 1
    return A

     SelectionSort ls = raw_input().split() raw_input().split() ls
def BubbleSort(A):
    #tot = 0
    flag = 1
    N = len(A)
    
    while flag:
        flag = 0
        for j in xrange(N-1,0,-1):
            if A[j][1] < A[j-1][1]:
                A[j],A[j-1] = A[j-1],A[j]
                flag = 1
                #tot += 1
    return A #,tot

def SelectionSort(A): 
    N = len(A)
    m = 0
    for i in range(N):
        minj = i
        for j in range(i,N):
            if A[j][1] < A[minj][1]:
                minj = j
        A[i],A[minj] = A[minj],A[i]
        if i != minj:
            m += 1
    return A

    
N = int(raw_input())
ls = raw_input().split()

tmp = BubbleSort(list(ls))
print ' '.join(map(str,tmp))
print 'Stable'

tmp2 = SelectionSort(list(ls))
print ' '.join(map(str,tmp2))
for k,j in zip(tmp,tmp2):
    if k != j:
        print 'Not stable'
        break
else:
    print 'Stable'