#P8249. 模法问题

模法问题

Description

You have two positive integers a,ba,b.

You also have a positive integer qq, and you need to process qq queries.

For each query, you are given two positive integers l,rl,r.

You need to find max{(imoda)+(imodb)}\max\{(i \bmod a)+(i \bmod b)\}, where ii is a positive integer and i[l,r]i \in [l,r].

Input Format

The first line contains two positive integers a,ba,b.

The second line contains one positive integer qq.

The next qq lines each contain two positive integers l,rl,r.

Output Format

For each query, output one line containing one integer, which is the answer.

5 7
4
1 5
5 7
33 34
34 35
8
7
10
10

Hint

For 30%30\% of the testdata, 1q1031 \le q \le 10^3 and 1lr1031 \le l \le r \le 10^3.

For 70%70\% of the testdata, 1q1031 \le q \le 10^3 and 1lr1091 \le l \le r \le 10^9.

For 100%100\% of the testdata, 1a,b1031 \le a,b \le 10^3, 1q1051 \le q \le 10^5, and 1lr1091 \le l \le r \le 10^9.

Translated by ChatGPT 5