Jon and Dany's Jump

5

1 votes
Easy
Problem

After having a romantic night at the boat, Jon Snow and our Khaleesi Dany decided to "jump" to a conclusion. They intend to implement this by literally jumping on a series of rocks in a line along the seashore. Since they're too deep in love, they'll only consider those points where they both will meet.

You are given 2 numbers A and B one for Jon and the other for Dany. Jon jumps by A steps and Dany jumps by B steps. You are also given Q queries, each containing a range [L, R]. It may be possible that L > R, in which you'll have to swap the numbers to get desired range. Find the numbers of points for each range where both Jon and Dany will meet. Both start at 0.

Input Format

First line contains 3 integers - A, B and Q. Each of the next Q lines contains 2 integers L and R.

Constraints

1 <= A,B <= 10^9

1 <= Q <= 10^5

1 <= L,R <= 10^9

Output Format

Print a number for each query denoting the number of meeting points of Jon and Dany i.e. number of common points of Jon and Dany.

Sample Input
2 3 5
11 32
11 29
12 36
12 30
2 3
Sample Output
4
3
5
4
0
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Jon jumps by 2 steps. Dany jumps by 3 steps.

  • In the range [11, 32], they both will meet at the points 12, 18, 24, 30.
  • In the range [12, 30], they both will meet at the points 12, 18, 24, 30.
  • In the range [2 ,3], they won't be meeting anywhere.
Editor Image

?