import qualified Data.IntSet as IS
main = mapM_ (print . solve) . analyze . map read . lines =<< getContents
analyze (n:cs) = let (ss,bs) = splitAt n cs in (n,ss):analyze bs
analyze [] = []
solve::(Int,[Int])->Int
solve (_,ss) = IS.foldr g 0 . foldr f (IS.singleton 0) $ ss
f::Int->IS.IntSet->IS.IntSet
f s vs = IS.union vs . IS.map (+s) $ vs
g::Int->Int->Int
g v m = if v`mod`10/=0 then max v m else m