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.io.*;
import java.util.*;

public class Main {

    public static long gcd(int a, int b){
        return b == 0 ? a : gcd(b, a%b);
    }

    public static long upperBound(int target){
        int l = 0; int r = commonDivisors.size()-1;
        while(l < r){
            int mid = l + (r-l+1)/2;

            if(commonDivisors.get(mid) <= target){
                l = mid;
            }
            else r = mid-1;
        }
        return commonDivisors.get(l);
    }
    static ArrayList<Long> commonDivisors;
    public static void main(String[] args) throws IOException, InterruptedException {
        int l = sc.nextInt(); int r = sc.nextInt();
        commonDivisors = new ArrayList<>();
        for (long i = 1; i * i <= l+1; i++) {
            if(l % i == 0){
                if(r % i == 0) commonDivisors.add(i);
                if(r % (l/i) == 0) commonDivisors.add(l/i);
            }
        }
        commonDivisors.add(gcd(l,r));
        Collections.sort(commonDivisors);
//        pw.println(commonDivisors);

        int q = sc.nextInt();
        while(q --> 0){
            int lo = sc.nextInt(); int hi = sc.nextInt();
            long ans = upperBound(hi);
            if(ans >= lo) pw.println(ans);
            else pw.println(-1);
        }

        pw.close();
    }

    public static void pairSort(Pair[] arr) {
        ArrayList<Pair> l = new ArrayList<>();
        Collections.addAll(l, arr);
        Collections.sort(l);
        for (int i = 0; i < arr.length; i++) {
            arr[i] = l.get(i);
        }
    }
    public static void longSort(long[] arr) {
        ArrayList<Long> l = new ArrayList<>();
        for (long i : arr) l.add(i);
        Collections.sort(l);
        for (int i = 0; i < arr.length; i++) {
            arr[i] = l.get(i);
        }
    }
    public static void intSort(int[] arr) {
        ArrayList<Integer> l = new ArrayList<>();
        for (int i : arr) l.add(i);
        Collections.sort(l);
        for (int i = 0; i < arr.length; i++) {
            arr[i] = l.get(i);
        }
    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public String nextLine() throws IOException {
            return br.readLine();
        }

        public double nextDouble() throws IOException {
            String x = next();
            StringBuilder sb = new StringBuilder("0");
            double res = 0, f = 1;
            boolean dec = false, neg = false;
            int start = 0;
            if (x.charAt(0) == '-') {
                neg = true;
                start++;
            }
            for (int i = start; i < x.length(); i++)
                if (x.charAt(i) == '.') {
                    res = Long.parseLong(sb.toString());
                    sb = new StringBuilder("0");
                    dec = true;
                } else {
                    sb.append(x.charAt(i));
                    if (dec)
                        f *= 10;
                }
            res += Long.parseLong(sb.toString()) / f;
            return res * (neg ? -1 : 1);
        }

        public boolean ready() throws IOException {
            return br.ready();
        }

        public int[] nextIntArr(int n) throws IOException {
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = Integer.parseInt(next());
            }
            return arr;
        }

        public long[] nextLongArr(int n) throws IOException {
            long[] arr = new long[n];
            for (int i = 0; i < n; i++) {
                arr[i] = Long.parseLong(next());
            }
            return arr;
        }

    }
    static class Pair implements Comparable<Pair>{
        int first; int second;
        public Pair(int first, int second){
            this.first = first; this.second = second;
        }

        @Override
        public int compareTo(Pair p2) {
            if(second == p2.second) return first - p2.first;
            else return second - p2.second;
        }

        @Override
        public String toString() { return "("+ first + "," + second + ')'; }
    }
    static class Triple implements Comparable<Triple> {
        double x, y, z, sum;

        Triple(double a, double b, double c) { x = a; y = b; z = c; sum = x + y + z; }


        public int compareTo(Triple t)
        {
            if(Math.abs(sum - t.sum) < 1e-9) return x > t.x ? 1 : -1;
            return sum > t.sum ? 1 : -1;
        }
        public String toString()
        {
            return x + " " + y  + " " + z;
        }
    }

    static PrintWriter pw = new PrintWriter(System.out);
    static Scanner sc = new Scanner(System.in);
    static Random random = new Random();
    static final long MOD = 1073741824;
}