def stablesort(a)
   arr=Array.new(10){Array.new}
   a.each{|v|
     idx=v[1].to_i
     arr[idx].push(v[0])
   }
   res=Array.new
   arr.each_with_index{|x,i|
      if x!=nil then
         x.each{|y|
            res.push(y+i.to_s)
         }
      end
   }
   return res.join(" ")
end

def BubbleSort(a)
    n=a.length
    (0..n-1).each do |i|
        if i+1 > n-1 then next end
        (i+1..n-1).reverse_each do |j|
            if a[j][1] < a[j-1][1] then
               a[j],a[j-1]=a[j-1],a[j]
            end
        end
    end
    return a.join(" ")
end

def SelectionSort(a)
    n=a.length
    (0..n-1).each do |i|
        mini=i
        (i..n-1).each do |j|
            if a[j][1] < a[mini][1] then
               mini=j
            end
        end
        if mini!=i then
           a[i],a[mini]=a[mini],a[i]
        end
    end
    return a.join(" ")
end

def printst(r1,r2)
    if r1!=r2 then
       printf("Not stable\n")
    else
       printf("Stable\n")
    end
end

n=gets.to_i
indata1=gets.split(' ')
indata2=indata1.clone
indata3=indata1.clone
r1=stablesort(indata1)
r2=BubbleSort(indata2)
r3=SelectionSort(indata3)
printf("%s\n",r2)
printst(r1,r2)
printf("%s\n",r3)
printst(r1,r3)