{-# OPTIONS_GHC -O3 -fexpose-all-unfoldings -fdicts-cheap -fdicts-cheap #-}

import Data.Int (Int32)
import Data.Array.IO
import Data.List (sortBy,minimumBy,foldl1')
import Data.Ord (comparing)

acmArray :: IOUArray Int32 Int32 -> (Int32,Int32) -> IO (IOUArray Int32 Int32)
acmArray arr (x,y) = do
  s <- readArray arr x
  writeArray arr x (s + y)
  return arr

maxsnd (a,b) (c,d)
  | b > d           = (a,b)
  | b == d && a < c = (a,b)
  | otherwise       = (c,d)

filt (a,b) (c,d) = b >= d
filt2 (a,b) (c,d) = a /= c

toPut (x,y) = show x ++ " " ++ show y

listToTuple :: [(Int32,Int32)] -> (Int32,Int32)
listToTuple [(x,y)] = (x,y)

judge :: (Int32,Int32) -> (Int32,Int32) -> [(Int32,Int32)] -> IOUArray Int32 Int32 -> IO (Int32, Int32)
judge (a,b) (x,y) xs arr = do
  if (a /= x) && ((b > y) || (b == y && a < x)) then return (a,b)
     else do
       let tes' = foldl1' maxsnd xs
       return (tes')

putresult arr xs n (a,b) = do
  s' <- getLine
  let [x,y'] = map (read :: String -> Int32) $ words s'
  acmArray arr (x,y')
  y <- readArray arr x

  let xs'  = if (length xs) > 500 then xs else take 50 (sortBy (comparing snd) xs)
  let xs'' = (x,y) : (filter (filt2 (x,y)) xs')

  (a',b') <-  judge (a,b) (x,y) xs' arr

  putStrLn $ toPut (a',b')

  if n < 2 then return ()
  else do
--    test <- getAssocs arr
    print xs'
    putresult arr xs' (n-1) (a',b')

main = do
  s <- getLine
  let [x,n] = map read $ words s
  arr <- newArray (1,x) 0 :: IO (IOUArray Int32 Int32)
  putresult arr [] n (1,0)