import Control.Monad
import Data.List
import qualified Data.ByteString.Char8 as B
import qualified Data.IntMap as M

main = do
    (n:m:_)<-map read.words<$>getLine::IO[Int]
    pys<-replicateM m $ unfoldr (B.readInt.B.dropWhile(==' '))<$>B.getLine::IO[[Int]]

    let mp = foldl' (\m ((p:y:_),n)->M.alter (f (y,n)) p m) M.empty $ zip pys [1..]
    --
    let ns = sort $ concatMap (\(p, m)-> (map (\((_,c),n)->(c, (p,n)) ) $ zip (M.toAscList m) [1..])) $ M.toAscList mp
    putStr $ unlines $ map g ns
--
f (y,n) Nothing = Just (M.singleton y n)
f (y,n) (Just m) = Just (M.insert y n m)
--
g (_,(p,x)) = drop (length $ show p) ("000000"++show p) ++ drop (length $ show p) ("000000"++show x)