(defun get-conditions (num)
  ;; 条件を取得するLoop
  (loop as i 
        below num 
        collect (list (read) (read))))

(defun get-list (s e)
  (loop as i
        from s
        to e
        collect (write-to-string i)))

(defun get-candidates (n)
  (case n
    (1 (get-list 0 9))
    (2 (get-list 10 99))
    (3 (get-list 100 999))))

(defun is-pass (n r v) ; str int int
  (string= (subseq n (- r 1) r) (write-to-string v)))

(defun candidate-filter (candidates r v)
  (loop as candidate 
        in candidates
        when (is-pass candidate r v)
        collect candidate))

(defun apply-conditions (candidates conditions)
  (if (null conditions)
      (car candidates)
      (apply-conditions (candidate-filter candidates 
                                          (car (car conditions)) 
                                          (cadr (car conditions)))
                        (cdr conditions))))

(let ((n (read))
      (conditions (get-conditions (read)))
      )
  (princ (apply-conditions (get-candidates n) conditions)))