import Data.List(sort)
import Data.List(groupBy)

main = do
  s <- getLine
  let n = (read :: String -> Int) s
  ss <- getContents
--       "1 1 3 8\n3 2 2 7\n4 3 8 1\n"
  let tra = map (map (read::(String->Int)) . words ) . lines $ ss
--      [[1,1,3,8],[3,2,2,7],[4,3,8,1]]
  let rst = aux (tra ++ kuu)
  putStrLn $  fmt rst [4,3,10]
  where
    kuu = [[b,f,r,0]|b<-[1..4],f<-[1..3],r<-[1..10]]

aux :: [[Int]] -> [[Int]]
aux lst =gat $ grp $ lst

grp :: [[Int]] ->[[[Int]]]   
grp lst = groupBy (\a b->(init a) == (init b)) $ sort lst

gat :: [[[Int]]] -> [[Int]]
gat v= map (\z->(init.head $ z) ++ [sum $ map (last) z]) v

--        [[1,1,1,0],[1,1,2,1],[1,1,2,2],[1,1,3,0]]
-- grp -> [[[1,1,1,0]],[[1,1,2,1],[1,1,2,2]],[[1,1,3,0]]]
-- gat -> [[1,1,1,0]],[1,1,2,3],[1,1,3,0]]

-- fmt [[1,1,1,0],[1,1,2,3],[1,1,3,0]] [4,3,10]
fmt :: [[Int]]->[Int]->String
fmt [] _ = ""
fmt (x:xs) [nb,nf,nr] =
   " " ++ (show (last x))
  ++ (if (x!!2) ==nr then "\n" else "") 
  ++ (if (x!!2) ==nr && (x!!1) ==nf then (concat $ replicate (2*nr) "#")++"\n" else "")
  ++ (fmt xs [nb,nf,nr] )