import Data.List
solve b f r ss = sum $ [last s | s <- ss, init s == [b, f, r]]

main = do
    _ <- getLine
    ss <- fmap (map (map read) . map words . lines) getContents
    putStrLn $ intercalate "\n" $ intersperse (replicate 20 '#') [intercalate "\n" [(' ':unwords [show $ solve b f r ss | r <-[1..10]]) | f <- [1..3]] | b <- [1..4]]


