# -*- coding: utf-8 -*-
class Array
  def swap!(a, b)
    raise ArgumentError unless a.between?(0, self.count-1) && b.between?(0, self.count-1)

    self[a], self[b] = self[b], self[a]

    self
  end

  def swap(a, b)
    self.dup.swap!(a, b)
  end
end

def bubblesort(arr_in)
  n = arr_in.length
  for i in 0..n-1
    for j in n-1..i+1
      if arr_in[j][1].to_i < arr_in[j-1][1].to_i
        arr_in.swap!(j, j-1)
      end
    end
  end
  arr_in
end

def selsort(arr_in)
  n = arr_in.length
  for i in 0..n-1
    mini = i
    for j in i..n-1
      if arr_in[j][1].to_i < arr_in[mini][1].to_i
        mini = j
      end
    end
    arr_in.swap!(i,mini)
  end
  arr_in
end

if __FILE__ == $0
  while num = STDIN.gets
    tokens = STDIN.gets.chomp!.split
    b_result = bubblesort(tokens.dup)
    puts b_result.join(" ")
    p "Stable"
    s_result = selsort(tokens.dup)
    puts s_result.join(" ")
    flag = true
    for i in 0..s_result.length-1
      if b_result[i] != s_result[i]
        flag = FALSE
      end
    end
    flag ? puts "Stable" : puts "Not stable"
  end
end