#Q215. 「一本通 6.4 例 5」Strange Way to Express Integers
「一本通 6.4 例 5」Strange Way to Express Integers
Description
Original source: POJ 2891
Given positive integers and , find the smallest positive integer such that , or determine that no solution exists.
Input Format
Multiple test cases.
For each test case, the first line contains an integer ;
The next lines each contain two integers .
Output Format
For each test case, if no solution exists, output ; otherwise, output a non-negative integer. If there are multiple solutions, output the smallest valid answer.
Sample 1
2
8 7
11 9
31
Data Range and Hints
For all data, all inputs are non-negative and can be represented by a 64-bit signed integer. It is guaranteed that and .