import Data.Map (Map)
import qualified Data.Map as Map
import Control.Applicative
import Control.Monad

main :: IO ()
main = do
    [users, events] <- map (read::String -> Int) . words <$> getLine
    inputLines 0 events Map.empty ([]::[Int])
 
addDict map (fn:sn:_) = 
    Map.insertWithKey (\k x y -> x + y)  fn sn map

getHeighest [x] = x
getHeighest xs = do
    if (length greater > 0)
        then getHeighest greater 
        else (xu, xn)
    where
        h = (length xs) `div` 2
        (xu, xn) = xs !! h
        greater = [(u, n) | (u, n) <- xs, xn < n || (xn == n && u < xu)]
            

format :: (Int,Int) -> String
format (a,b) = (show a) ++ " " ++ (show b)

inputLines n limit acc higher = do
  if n < limit
    then do
      s <- map (read::String -> Int) . words <$> getLine
      let dict = addDict acc s
      let t = f higher s dict
      putStrLn $ format t
      inputLines (n + 1) limit dict [fst t, snd t]
    else return ()
        where
            f :: [Int] -> [Int] -> Map.Map Int Int -> (Int, Int)
            f [] _ dict = getHeighest . Map.toList $ dict
            f (h:hx:_) (s:sx:_) dict =
                if (sx < 0 && h == s)
                then getHeighest . Map.toList $ dict
                else if (hx < dict Map.! s || (h == dict Map.! s && h < s))
                    then (s, dict Map.! s)
                    else (h, hx)
