Combination Lock

시간 제한2초메모리 제한1024 MB

요약
각 구간에서 수를 하나씩 골라 모든 쌍이 서로소가 되도록 하거나 불가능함을 판정한다.
난이도

보통10점 중 5점

유형
백트래킹, 정수론, 그리디
정답자
아직 제출이 없습니다

문제

Your house is protected by a combination lock containing nn rotating discs, numbered from 11 to nn. On a typical combination lock, each rotating disc has 1010 symbols, represented by integers between 00 to 99, inclusive. Since you are a mathematician, your combination lock is not typical. Instead, each rotating disc on your combination lock may have a different number of symbols. In particular, rotating disc ii has b_i−a_i+1b\_i - a\_i + 1 symbols, represented by integers between a_ia\_i to b_ib\_i, inclusive.

The combination lock is unlocked when each rotating disc displays one integer, and any pair of two integers displayed by the rotating disc are coprime. Two integers are coprime if they do not have any common positive factors other than 11.

You want to unlock the combination lock, so you want to determine what integer to be displayed on each combination lock to satisfy the requirement above. It is possible that your combination lock was sabotaged when you were gone so it might be impossible to unlock your combination lock.

입력

The first line of input contains one integer nn (2≤n≤502 ≤ n ≤ 50). Each of the next nn lines contains two integers. The ii-th line contains a_ia\_i and b_ib\_i (1≤a_i≤b_i≤501 ≤ a\_i ≤ b\_i ≤ 50).

출력

Output one line containing nn integers, where the ii-th integer represents the integer to be displayed by rotating disc ii to unlock the combination lock. If there are multiple solutions, you can output any of them. If there is no solution, output just the integer -1.

예제2

  1. 예제 1

    입력
    4
    3 9
    2 8
    1 4
    2 10
    
    예상 출력
    3 7 1 10
    
  2. 예제 2

    입력
    3
    5 6
    5 6
    2 6
    
    예상 출력
    -1