Problem: Well, here is another math class task. In mathematics, GCD is the greatest common divisor, and it's an easy task to calculate the GCD between two positive integers.

A common divisor for two positive numbers is a number which both numbers are divisible by.

But your teacher wants to give you a harder task, in this task you have to find the greatest common divisor d between two integers a and b that is in a given range from low to high (inclusive), i.e. low ≤ d ≤ high. It is possible that there is no common divisor in the given range.

You will be given the two integers a and b, then n queries. Each query is a range from low to high and you have to answer each query.

Input

The first line contains two integers a and b, the two integers as described above (1 ≤ a, b ≤ 109). The second line contains one integer n, the number of queries (1 ≤ n ≤ 104). Then n lines follow, each line contains one query consisting of two integers, low and high (1 ≤ low ≤ high ≤ 109).

Output

Print n lines. The i-th of them should contain the result of the i-th query in the input. If there is no common divisor in the given range for any query, you should print -1 as a result for this query.

Examples

Input

9 27
3
1 5
10 11
9 11


Output

3
-1
9

Solution (in JAVA):

Solution:
import java.util.*;
public class ModifiedGCD {

    public static void main(String[] args){
        Scanner k = new Scanner(System.in);
        int num1 = k.nextInt();
        int num2 = k.nextInt();
        int query = k.nextInt();
        int [] a = new int[query];
        Arrays.fill(a, -1);
        HashMap<String,Integer> d = new HashMap<String,Integer>();
        int [] one = new int[(int)Math.ceil(Math.sqrt(num1*1.0))+((int)Math.ceil(Math.sqrt(num1*1.0))/2)];
        int [] two = new int[(int)Math.ceil(Math.sqrt(num2*1.0))+((int)Math.ceil(Math.sqrt(num2*1.0))/2)];
        int p = 0;
        for(int i=1;i*i<=num1;i++){
//          System.out.println(i + " " +p);
            if(num1%i==0 && p<one.length){
                one[p]=i;
                p++;
                if(num1/i != i && p<one.length){
//                  System.out.println(Arrays.toString(one));
//                  System.out.println(i);
                    one[p]=num1/i;
                    p++;
            }
            }
            
        }
        p=0;
        for(int i=1;i*i<=num2;i++){
            if(num2%i==0 && p<two.length){
                two[p]=i;
                p++;
                if(num2/i != i && p<two.length){
                    two[p]=num2/i;
                    p++;
                }
            }
        }
        Arrays.sort(one);
        Arrays.sort(two);
//      System.out.println(Arrays.toString(one));
//      System.out.println(Arrays.toString(two));
        p=0;
        while(query>0){
            int low = k.nextInt();
            int high = k.nextInt();
            String key = low+" "+high;
            if(high>Math.min(num1, num2))high = Math.min(one[one.length-1],two[two.length-1]);
            if(d.containsKey(key))a[p]=d.get(key);
            else if(one[one.length-2] == 1 && two[two.length-2] == 1 && low>1)a[p]=-1;
            else if(one[one.length-2] == 1 && two[two.length-2] == 1 && low==1)a[p]=1;
            else if(high == low && num1%high == 0 && num2%high == 0)a[p]=high;
            else{
                int index = 1;
            while(high>=low && a[p] == -1){
                if(Arrays.binarySearch(one, high)>=0 && Arrays.binarySearch(two, high)>=0){
                    a[p]=high;
                }
//              System.out.println("h");
                high=Math.min(Math.min(one[one.length-index], two[two.length-index]),high--);
                index++;
            }
            d.put(key, a[p]);
            }
            p++;
            query--;
//          System.out.println("i");
    }
        for(int i=0;i<a.length;i++)System.out.println(a[i]);
}
}