;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0)))
  #+swank (progn (ql:quickload '(:cl-debug-print :fiveam))
                 (shadow :run)
                 (use-package :fiveam)))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
(defmacro with-output-buffer (&body body)
  "Buffers all outputs to *STANDARD-OUTPUT* and flushes them to
*STANDARD-OUTPUT* at the end. Note that only BASE-CHAR is allowed."
  (let ((out (gensym)))
    `(let ((,out (make-string-output-stream :element-type 'base-char)))
       (let ((*standard-output* ,out))
         ,@body)
       (write-string (get-output-stream-string ,out)))))

(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  (declare #.OPT)
  (macrolet ((%read-byte ()
               `(the (unsigned-byte 8)
                     #+swank (char-code (read-char in nil #\Nul))
                     #-swank (sb-impl::ansi-stream-read-byte in nil #.(char-code #\Nul) nil))))
    (let* ((minus nil)
           (result (loop (let ((byte (%read-byte)))
                           (cond ((<= 48 byte 57)
                                  (return (- byte 48)))
                                 ((zerop byte) ; #\Nul
                                  ;; (return-from read-fixnum 0)
                                  (error "Read EOF or #\Nul."))
                                 ((= byte #.(char-code #\-))
                                  (setf minus t)))))))
      (declare ((integer 0 #.most-positive-fixnum) result))
      (loop
        (let* ((byte (%read-byte)))
          (if (<= 48 byte 57)
              (setq result (+ (- byte 48) (the fixnum (* result 10))))
              (return (if minus (- result) result))))))))

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
(define-int-types 2 4 7 8 15 16 31 32 62 63 64)

(defmacro println (obj &optional (stream '*standard-output*))
  `(let ((*read-default-float-format* 'double-float))
     (prog1 (princ ,obj ,stream) (terpri ,stream))))

;; Hauptteil

(defstruct city
  (year 0 :type uint31)
  (prefecture 0 :type uint31)
  (version 0 :type uint31))

(defun main ()
  (declare #.OPT
           (inline sort))
  (let* ((n (read))
         (m (read))
         (p-table (make-array (1+ n) :element-type 'list :initial-element nil))
         (city-table (make-array m :element-type 'list :initial-element nil)))
    (declare (uint32 n m))
    (dotimes (city-idx m)
      (let* ((prefecture (read-fixnum))
             (year (read-fixnum))
             (node (make-city :prefecture prefecture :year year)))
        (push node (aref p-table prefecture))
        (setf (aref city-table city-idx) node)))
    (loop for p-idx from 1 to n
          do (setf (aref p-table p-idx)
                   (sort (the list (aref p-table p-idx)) #'< :key #'city-year))
             (loop for node in (aref p-table p-idx)
                   for version from 1
                   do (setf (city-version node) version)))
    (with-output-buffer
      (dotimes (city-idx m)
        (format t "~6,'0D~6,'0D~%"
                (city-prefecture (aref city-table city-idx))
                (city-version (aref city-table city-idx)))))))

#-swank(main)


Measured memory_bytes: 73696