import qualified Data.ByteString.Char8 as B import Data.Maybe import Control.Monad readInt = fst . fromJust . B.readInteger getInt = readInt <$> B.getLine getIntN n = map readInt <$> replicateM (fromIntegral n) B.getLine main :: IO () main = do t <- read <$> getLine ss <- replicateM (fromIntegral t) getLine putStr $ unlines $ map show $ solve ss solve :: [String] -> [Integer] solve ss = map solveOne ss solveOne :: String -> Integer solveOne number | read number < 10 = read number solveOne number = if ans >= 10 then solveOne $ show ans else ans where numbers = toNumbers number ns = map (\(a,b) -> a + b) $ zip (take ((length numbers)-1) numbers) (drop 1 numbers) ns2 = map sumAllNumber ns ans = toNumber ns2 toNumbers :: String -> [Integer] toNumbers num = [ read [i] | i <- num ] toNumber :: [Integer] -> Integer toNumber nums = sum $ map (\(idx, num) -> num * 10 ^ (len-idx)) $ zip [1 ..] nums where len = length nums sumAllNumber :: Integer -> Integer sumAllNumber digits = if digits >= 10 then sum $ toNumbers $ show digits else digits numberAt :: Integer -> Integer -> Integer numberAt at source = (source `div` 10 ^ (at-1)) `mod` 10