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 <- getIntN t putStrLn $ unlines $ map show $ solve ss solve :: [Integer] -> [Integer] solve ss = map solveOne ss solveOne :: Integer -> Integer solveOne number | number < 10 = number solveOne number = if ans >= 10 then solveOne 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 :: Integer -> [Integer] toNumbers num = [ numberAt (fromIntegral $ len - i) num | i <- [0 .. len-1] ] where num_s = show num len = length num_s 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 digits else digits numberAt :: Integer -> Integer -> Integer numberAt at source = (source `div` 10 ^ (at-1)) `mod` 10