import Data.List
toInt::String->Int
toInt = read
format::Show a=>[a]->String
format [x] = show x
format (x:xs) = show x ++ " " ++ format xs
gt::String->String->Bool
gt (x:xs) (y:ys) = xs > ys
--swapSub::Ord a=>a->[] a->[] a
swapSub x [] = [x]
swapSub x (y:ys)
  | gt x y = y:x:ys
  | otherwise = x:y:ys
--swap::Ord a=>[] a->[] a
swap xs = foldr swapSub [] xs
--bsort::Ord a=>[] a->[] a
bsort [] = []
bsort xs = y : bsort ys
  where
    (y:ys) = swap xs
--minBreak::Ord a=>[a]->([a], [a])
minBreak [x] = ([], [x])
minBreak (x:xs)
  | gt x z = (x:ys, z:zs)
  | otherwise = ([], x:ys++z:zs)
    where
      (ys, (z:zs)) = minBreak xs
--ssort::Ord a=>[a]->[a]
ssort [y] = [y]
ssort (y:ys)
  | gt y w = w : ssort (zs++[y]++ws)
  | otherwise = y : ssort (zs++[w]++ws)
    where
      (zs, (w:ws)) = minBreak ys
main = do
  getLine
  a <- getLine
  let b = (unwords.bsort.words) a
  putStrLn b
  putStrLn "Stable"
  let s = (unwords.ssort.words) a
  putStrLn s
  putStrLn (if s == b then "Stable" else "Not stable")
