def number(c) # H8 -> 8
  c[1, c.size-1].to_i
end

def bubble_sort(a, n)
  0.upto(n-2){|i|
    (n-1).downto(i+1){|j|
      if number(a[j]) < number(a[j-1])
        tmp = a[j]
        a[j] = a[j-1]
        a[j-1] = tmp
      end
    }
  }
  a
end

def selection_sort(a, n)
  n.times{|i|
    m=i
    i.upto(n-1){|j|number(a[j]) < number(a[m]) && m=j}
    t=a[i];a[i]=a[m];a[m]=t
  }
  a
end

n=gets.to_i
a=gets.split

b=bubble_sort(a, n)
puts b*" "
puts "Stable"

s=selection_sort(a, n)
puts s*" "
puts (b==s ? "Stable" : "Not stable")


