import Control.Applicative ((<$>), (<*>)) import Data.ByteString.Char8 (ByteString) import qualified Data.ByteString.Char8 as B main :: IO () main = do solve <$> getl B.words <*> getl B.words >>= B.putStrLn solve :: [ByteString] -> [ByteString] -> ByteString solve [sa, pa, _] [sb, pb, _] | la > lb = sa | la < lb = sb | otherwise = f pa pb where la = B.length pa lb = B.length pb f a b | B.null a = B.pack "-1" | B.head a > B.head b = sa | B.head a < B.head b = sb | otherwise = f (B.tail a) (B.tail b) getl :: (ByteString -> a) -> IO a getl f = f <$> B.getLine