There is a planet, in a yet undiscovered part of the universe, with a country inhabited solely by mathematicians. In this country, there are a total of mathematicians, and the interesting fact is that each mathematician lives in their own city. It is also interesting that no two cities are connected with a road, because mathematicians can communicate online or by reviewing academic papers. Naturally, the cities are labeled with numbers from to .
Life was perfect until one mathematician decided to write an academic paper on their smartphone. The smartphone auto-corrected the word "self-evident" to "Pictionary" and the paper was published as such. Soon after, the entire country discovered pictionary and wanted to meet up and play, so construction work on roads between cities began shortly.
The road construction will last a total of days, according to the following schedule: on the first day, construction is done on roads between all pairs of cities that have as their greatest common divisor. On the second day, construction is done on roads between all pairs of cities that have as their greatest common divisor, and so on until the day when construction is done on roads between all pairs of cities that are co-prime. More formally, on the day, construction is done on roads between cities and if .
Since the mathematicians are busy with construction work, they've asked you to help them determine the minimal number of days before a given pair of mathematicians can play pictionary together.
Input Specification
The first line of input contains three positive integers , and , the number of cities, the number of days it takes to build the roads, and the number of queries.
Each of the following lines contains two distinct positive integers and that denote the cities of the mathematicians who want to find out the minimal number of days before they can play pictionary together.
Output Specification
The line must contain the minimal number of days before the mathematicians from the query can play pictionary together.
Scoring
In test cases worth of total points, it will hold , .
Sample Input 1
8 3 3
2 5
3 6
4 8
Sample Output 1
3
1
2
Explanation for Sample Output 1
On the first day, road is built. Therefore, the answer to the second query is .
On the second day, roads , , , and are built. Cities and are now connected (it is possible to get from the first to the second using city ).
On the third day, roads between relatively prime cities are built, so cities and are connected.
Sample Input 2
25 6 1
20 9
Sample Output 2
4
Explanation for Sample Output 2
On the second day, road is built, whereas on the fourth day, road is built. After the fourth day, cities and are connected via city .
Sample Input 3
9999 2222 2
1025 2405
3154 8949
Sample Output 3
1980
2160
Comments