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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.util.Collections;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        ArrayList<Integer> d = new ArrayList<>();
        ArrayList<Integer> s = new ArrayList<>();
        ArrayList<Integer> nums = new ArrayList<>();

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int a = in.nextInt();
            int b = in.nextInt();
            int c = gcd(a, b);

            int t = c;
            int di = 1;
            for (int i = 2; i * i <= c; i += di, di = 2) {
                if (t % i == 0) {
                    int st = 0;
                    while (t % i == 0) {
                        st++;
                        t /= i;
                    }
                    if (st > 0) {
                        d.add(i);
                        s.add(st);
                    }
                }
            }
            if (t != 1) {
                d.add(t);
                s.add(1);
            }

            nums.add(c);
            rec(c, 0);

            Collections.sort(nums);

            int n = in.nextInt();
            for (int i = 0; i < n; i++) {
                int l = in.nextInt();
                int h = in.nextInt();
                int idx = bs(h + 1);
                if (idx == -1) {
                    out.println(-1);
                } else if (l <= nums.get(idx) && nums.get(idx) <= h) {
                    out.println(nums.get(idx));
                } else {
                    out.println(-1);
                }
            }
        }

        int bs(int x) {
            int l = -1;
            int r = nums.size();
            while (r - l > 1) {
                int m = (r + l) / 2;
                if (nums.get(m) < x)
                    l = m;
                else
                    r = m;
            }
            return l;
        }

        void rec(int k, int p) {
            if (p >= d.size())
                return;

            rec(k, p + 1);
            for (int i = 1; i <= s.get(p); i++) {
                k /= d.get(p);
                nums.add(k);
                rec(k, p + 1);
            }
        }

        int gcd(int a, int b) {
            while (b != 0) {
                int c = a % b;
                a = b;
                b = c;
            }
            return a;
        }

    }

    static class InputReader {
        StringTokenizer st;
        BufferedReader br;

        public InputReader(InputStream is) {
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            this.br = br;
        }

        public String next() {
            if (st == null || !st.hasMoreTokens()) {
                String nextLine = null;
                try {
                    nextLine = br.readLine();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                if (nextLine == null)
                    return null;
                st = new StringTokenizer(nextLine);
            }
            return st.nextToken();
        }

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

    }
}