Ciri and GCD Problem

0

0 votes
Easy
Problem

Ciri has recently learnt how to calculate GCD (greatest common divisor) of two numbers. To test her understanding, Geralt asks her to calculate GCD of 2 numbers a and b. Ciri easily calculates the answer. Now to make the problem harder, he has asked q questions in which each one requires to calcuate greatest number in range [l,r] which divides both a and b. Ciri won't be able to solve all questions in time. So, she has asked you to solve the questions for her.

Input Format--

First line consists of three integers, a, b and q.

Each of next q lines consists of two integers l and r, denoting the given range.

Output Format--

Output q lines giving answer to each of the corresponding range.

If there is no valid number in given range, output "-1"(without quotes).

Constraints--

1a,b109

1q106

1lr109

Sample Input
12 16
5
1 20
1 4
1 3
10 20
10 30
Sample Output
4
4
2
-1
-1
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For first question, 4 is the largest number in range [1, 20] which divides both 12 and 16.

For second question, 4 is the largest number in range [1, 4] which divides both 12 and 16.

For third question, 2 is the largest number in range [1, 3] which divides both 12 and 16.

For fourth question, there is no number in range [10, 20] which divides both 12 and 16.

For second question, there is no number in range [10, 30] which divides both 12 and 16.

Editor Image

?