main = interact $ unlines . map (show' . pablo_squarson) . read' . lines
 where
  read' ("0":_) = []
  read' (m:xs) = map (map read . words) ps : read' xs'
   where (ps,xs') = splitAt (read m - 1) xs
  show' (x,y) = show x ++ " " ++ show y

pablo_squarson ps = pos [(1,1)] ps
 where
  pos acc [] = (maximum fs - minimum fs + 1, maximum ss - minimum ss + 1)
   where (fs,ss) = (map fst acc, map snd acc)
  pos acc ([n,d]:ps') = pos (acc++[(x,y)]) ps'
   where
    (x,y) = add d (acc!!n)
    add d (x,y) = case d of {0->(x-1,y); 1->(x,y+1); 2->(x+1,y); 3->(x,y-1)}