import Data.List
import Data.Char

data Card = Card {
  mark :: String,
  num :: [Int],
  state :: String,
  result :: [String]
  } deriving (Show, Read)

bubble' :: Int -> Card -> Card
bubble' 14 card = Card {mark = mark card, num = sort $ num card, state = state card, result = a (concat $ reverse $ result card) (sort $ num card)}
  where a [] [] = []
        a (m:mx) (n:nx) = (m:show n) : a mx nx
bubble' n card
  | elemIndices n (num card) == [] = bubble' (n+1) card
  | otherwise = bubble' (n+1) (Card {mark = mark card, num = num card, state = state card, result = [(mark card) !! a | a <- elemIndices n (num card)] : result card})

select' :: Card -> Card
select' all@(Card {num = []}) = all
select' card
  | 0 /= getIndex card = select' (Card {mark = funcM card, num = funcN card, state = state' card, result = ((mark card !! getIndex card) : show (num card !! getIndex card)) : result card})
  | otherwise = select' (Card {mark = tail $ mark card, num = tail $ num card, state = state card, result = ((head $ mark card) : show (head $ num card)) : result card})
  where funcM c = change (mark c) (getIndex c)
        funcN c = change (num c) (getIndex c)
        getIndex c = (\(Just x) -> x) $ elemIndex (minimum $ num c) (num c)
        change l i = tail $ (take i l) ++ [head l] ++ drop (i + 1) l
        state' c = if elem (head $ num c) (tail $ take (getIndex c) (num c)) then "Not stable" else state card

main = do
  list <- fmap (tail . words) getContents
  putStrLn $ unwords $ result $ bubble' 1 $ card list
  putStrLn $ "Stable"
  putStrLn $ unwords $ reverse $ result $ select' $ card list
  putStrLn $ state $ select' $ card list
    where card l = Card {mark = [a | [a,_] <- l], num = [digitToInt b | [_,b] <- l], state = "Stable", result = []}