import Data.Map (Map)
import qualified Data.Map as Map

import Control.Monad

countDict [] = Map.empty
countDict (x:xs) = 
  let s = countDict xs 
  in
      Map.insertWithKey (\k x y -> x + y)  (x !! 0) (x !! 1) s

getHeighest [x] = x
getHeighest (x:xs) =
    let h = getHeighest xs
    in 
        if((snd x) > (snd h) || ((snd x) == (snd h) && (fst x) < (fst h)) )
            then x 
            else h

inputLines n limit acc = 
    let 
        f n limit l 
            | n < limit = do
                let t = getHeighest . Map.toList $ countDict l
                putStrLn $ (show $ fst t) ++ " " ++ (show $ snd t)
                inputLines (n + 1) limit l
            | otherwise = return ()
    in
        do
            s <- map (read::String -> Int) . words <$> getLine
            f n limit ((acc :: [[Int]]) ++ [s])

            return ()

main = do
    [users, events] <- map read . words <$> getLine
    inputLines 0 events ([] :: [[Int]])
 
