import qualified Control.Monad as M import qualified Data.Char as C main = do t <- readLn s <- M.replicateM t getLine let s' = map (\x -> map C.digitToInt x) s putStr $ unlines $ map (show . solve) s' solve :: [Int] -> Int solve s = case length s of 1 -> head s otherwise -> solve (f s) f :: [Int] -> [Int] f s = map toDigit s' where s' = map (\x -> fst x + snd x) $ zip (init s) (tail s) toDigit :: Int -> Int toDigit n = if n >= 10 then toDigit $ sum $ map C.digitToInt $ show n else n