This page is still under construction.

Parts of this page are still being built. What you see may change.

Pictionary

Time limit1.5sMemory limit64 MB

Summary
Cities get roads on day i joining all pairs whose gcd is M-i+1; for each query, find the first day two given cities become connected.
Level

Hard8 of 10

Topics
Union-find, Number theory, Graph, Math
Solved
No attempts yet

Problem

In an undiscovered corner of the universe there is a planet with a country where only mathematicians live. The country has NN mathematicians, and each one lives in a city of their own. The cities are numbered 11 to NN. Mathematicians talk online or read each other's papers, so at the start no two cities are connected by a road.

Life was fine until one mathematician wrote a paper on a smartphone. Autocorrect changed the word "self-evident" to "Pictionary", and the paper was published that way. The whole country soon heard about Pictionary and wanted to meet and play, so construction of roads between the cities began.

The construction lasts MM days and follows this schedule. On the first day, roads are built between every pair of cities whose greatest common divisor is MM. On the second day, roads are built between every pair whose greatest common divisor is M−1M-1, and so on, until day MM, when roads are built between every pair of coprime cities. Formally, on day ii a road is built between cities aa and bb if gcd⁡(a,b)=M−i+1\gcd(a, b) = M - i + 1.

Two mathematicians can meet once their cities can reach each other along roads, passing through other cities if needed. The mathematicians are busy with the construction, so they ask you for the minimum number of days after which a given pair can play Pictionary together.

Input

The first line contains three positive integers NN, MM, and QQ (1≤N,Q≤100 0001 \le N, Q \le 100\,000, 1≤M≤N1 \le M \le N): the number of cities, the number of days the construction takes, and the number of queries.

Each of the next QQ lines contains two distinct positive integers AA and BB (1≤A,B≤N1 \le A, B \le N), the cities of the two mathematicians who want to know how many days it takes before they can play Pictionary together.

Output

Print QQ lines. Line ii contains the minimum number of days after which the two mathematicians of query ii can play Pictionary together.

Hint

Explanation of the first sample. On day 11 the road (3,6)(3, 6) is built, so the answer to the second query is 11. On day 22 the roads (2,4)(2, 4), (2,6)(2, 6), (2,8)(2, 8), (4,6)(4, 6), and (6,8)(6, 8) are built, and cities 44 and 88 become connected through city 66. On day 33 roads are built between coprime cities, so cities 22 and 55 become connected.

Explanation of the second sample. The road (20,15)(20, 15) is built on day 22 and the road (15,9)(15, 9) on day 44. After day 44, cities 2020 and 99 are connected through city 1515.

Examples3

  1. Example 1

    Input
    8 3 3
    2 5
    3 6
    4 8
    
    Expected output
    3
    1
    2
    
  2. Example 2

    Input
    25 6 1
    20 9
    
    Expected output
    4
    
  3. Example 3

    Input
    9999 2222 2
    1025 2405
    3154 8949
    
    Expected output
    1980
    2160