solve :: [Int] -> [Int]	-> [Int]
solve list (a:b:c:d:xs)	
  | null xs = list
  | otherwise = solve (take (i-1) list ++ [d] ++ drop i list) xs
  where i = (a-1)*30 + (b-1)*10 + c

mklist :: Int -> [Int] -> [String]
mklist n (x:xs)
  | null xs = []
  | mod n 30 == 0 = " " : shows x "\n" : (replicate 20 '#') : "\n" : mklist (n+1) xs
  | mod n 10 == 0 = " " : shows x "\n" : mklist (n+1) xs
  | otherwise = " " : show x : mklist (n+1) xs

main = getContents >>= putStr . concat . mklist 1 . (++[1]) . solve (replicate 120 0) . (++[1,1,1,1]) . map read . tail . words