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.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.function.Function;

public class P75C {

    static boolean multipleIndependent = false;

    void run() {
        int gcd = gcd(in.nextInt(), in.nextInt());
        TreeSet<Integer> divisors = divisors(gcd);
        int n = in.nextInt();
        while (n-- > 0) {
            int lo = in.nextInt();
            int hi = in.nextInt();
            Integer d = divisors.floor(hi);
            System.out.println(d != null && d >= lo ? d : "-1");
        }
    }

    private TreeSet<Integer> divisors(int n) {
        double sqrt = Math.sqrt(n);
        TreeSet<Integer> divisors = new TreeSet<>();
        TreeMap<Integer, Integer> factors = factors(n);
        calcDivisors(divisors, new ArrayList<>(factors.entrySet()), 0, 1);
        return divisors;
    }

    private void calcDivisors(TreeSet<Integer> divisors, ArrayList<Map.Entry<Integer, Integer>> arrayList, int index, int n) {
        if (index == arrayList.size()) {
            divisors.add(n);
        } else {
            int p = 1;
            for (int i = 0; i <= arrayList.get(index).getValue(); i++) {
                calcDivisors(divisors, arrayList, index + 1, n * p);
                p *= arrayList.get(index).getKey();
            }
        }
    }

    private TreeMap<Integer, Integer> factors(int n) {
        double sqrt = Math.sqrt(n);
        TreeMap<Integer, Integer> factors = new TreeMap<>();
        for (int i = 2; n > 1; i++) {
            int c = 0;
            while (n % i == 0) {
                n /= i;
                c++;
            }
            if (c > 0) {
                factors.put(i, c);
            }
        }
        return factors;
    }

    private int gcd(int a, int b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

    /* -----: Template :----- */
    static InputReader in = new InputReader(System.in);
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

    public static void main(String[] args) {
        int q = multipleIndependent ? in.nextInt() : 1;
        while (q-- > 0) {
            new P75C().run();
        }
        out.flush();
    }

    static class InputReader {

        private final BufferedReader reader;
        private StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            int bufferSize = 32 * 1024;
            reader = new BufferedReader(new InputStreamReader(stream), bufferSize);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

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

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

        public int[] nextIntArray(int n) {
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextInt();
            }
            return arr;
        }

        public <T> T[] nextIntArrayWithFunction(int n, Function<Integer, T> function, Class<T> c) {
            T[] arr = (T[]) Array.newInstance(c, n);
            for (int i = 0; i < n; i++) {
                arr[i] = function.apply(nextInt());
            }
            return arr;
        }

        public long[] nextLongArray(int n) {
            long[] arr = new long[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextLong();
            }
            return arr;
        }

        public <T> T[] nextLongArrayWithFunction(int n, Function<Long, T> function, Class<T> c) {
            T[] arr = (T[]) Array.newInstance(c, n);
            for (int i = 0; i < n; i++) {
                arr[i] = function.apply(nextLong());
            }
            return arr;
        }

        public int[][] nextIntMap(int n, int m) {
            int[][] map = new int[n][m];
            for (int i = 0; i < n; i++) {
                map[i] = nextIntArray(m);
            }
            return map;
        }

        public long[][] nextLongMap(int n, int m) {
            long[][] map = new long[n][m];
            for (int i = 0; i < n; i++) {
                map[i] = nextLongArray(m);
            }
            return map;
        }

        public char[][] nextCharMap(int n) {
            char[][] map = new char[n][];
            for (int i = 0; i < n; i++) {
                map[i] = next().toCharArray();
            }
            return map;
        }
    }

    static <R> long binarySearch(long lowerBound, long upperBound,
            R value, Function<Long, R> generatorFunction, Comparator<R> comparator) {
        if (lowerBound <= upperBound) {
            long mid = (lowerBound + upperBound) / 2;
            int compare = comparator.compare(generatorFunction.apply(mid), value);
            if (compare == 0) {
                return mid;
            } else if (compare < 0) {
                return binarySearch(mid + 1, upperBound, value, generatorFunction, comparator);
            } else {
                return binarySearch(lowerBound, mid - 1, value, generatorFunction, comparator);
            }
        } else {
            return -1;
        }
    }

    static <T> Integer[] sortSimultaneously(T[] key, Comparator<T> comparator,
            Object[]... moreArrays) {
        int n = key.length;
        for (Object[] array : moreArrays) {
            if (array.length != n) {
                throw new RuntimeException("Arrays must have equals lengths");
            }
        }
        Integer[] indices = new Integer[n];
        for (int i = 0; i < n; i++) {
            indices[i] = i;
        }
        Comparator<Integer> delegatingComparator = (a, b) -> {
            return comparator.compare(key[a], key[b]);
        };
        Arrays.sort(indices, delegatingComparator);
        reorder(indices, key);
        for (Object[] array : moreArrays) {
            reorder(indices, array);
        }
        return indices;
    }

    static void reorder(Integer[] indices, Object[] arr) {
        if (indices.length != arr.length) {
            throw new RuntimeException("Arrays must have equals lengths");
        }
        int n = arr.length;
        Object[] copy = new Object[n];
        for (int i = 0; i < n; i++) {
            copy[i] = arr[indices[i]];
        }
        System.arraycopy(copy, 0, arr, 0, n);
    }

    static int prodMod(int a, int b, int mod) {
        return (int) (((long) a) * b % mod);
    }

    static long prodMod(long a, long b, long mod) {
        long res = 0;
        a %= mod;
        b %= mod;
        while (b > 0) {
            if ((b & 1) > 0) {
                res = (res + a) % mod;
            }
            a = (a << 1) % mod;
            b >>= 1;
        }
        return res;
    }

    static long sumMod(int[] b, long mod) {
        long res = 0;
        for (int i = 0; i < b.length; i++) {
            res = (res + b[i] % mod) % mod;
        }
        return res;
    }

    static long sumMod(long[] a, long mod) {
        long res = 0;
        for (int i = 0; i < a.length; i++) {
            res = (res + a[i] % mod) % mod;
        }
        return res;
    }

    static long sumProdMod(int[] a, long b, long mod) {
        long res = sumMod(a, mod);
        return prodMod(res, b, mod);
    }

    static long sumProdMod(long[] a, long b, long mod) {
        long res = sumMod(a, mod);
        return prodMod(res, b, mod);
    }

    static long sumProdMod(int[] a, int[] b, long mod) {
        if (a.length != b.length) {
            throw new RuntimeException("Arrays must have equals lengths");
        }
        long res = 0;
        for (int i = 0; i < a.length; i++) {
            res = (res + prodMod(a[i], b[i], mod)) % mod;
        }
        return res;
    }

    static long sumProdMod(long[] a, long[] b, long mod) {
        if (a.length != b.length) {
            throw new RuntimeException("Arrays must have equals lengths");
        }
        long res = 0;
        for (int i = 0; i < a.length; i++) {
            res = (res + prodMod(a[i], b[i], mod)) % mod;
        }
        return res;
    }

    static int[] toPrimitive(Integer[] arr) {
        int[] res = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            res[i] = arr[i];
        }
        return res;
    }

    static int[][] toPrimitive(Integer[][] arr) {
        int[][] res = new int[arr.length][];
        for (int i = 0; i < arr.length; i++) {
            res[i] = toPrimitive(arr[i]);
        }
        return res;
    }

    static long[] toPrimitive(Long[] arr) {
        long[] res = new long[arr.length];
        for (int i = 0; i < arr.length; i++) {
            res[i] = arr[i];
        }
        return res;
    }

    static long[][] toPrimitive(Long[][] arr) {
        long[][] res = new long[arr.length][];
        for (int i = 0; i < arr.length; i++) {
            res[i] = toPrimitive(arr[i]);
        }
        return res;
    }

    static Integer[] toWrapper(int[] arr) {
        Integer[] res = new Integer[arr.length];
        for (int i = 0; i < arr.length; i++) {
            res[i] = arr[i];
        }
        return res;
    }

    static Integer[][] toWrapper(int[][] arr) {
        Integer[][] res = new Integer[arr.length][];
        for (int i = 0; i < arr.length; i++) {
            res[i] = toWrapper(arr[i]);
        }
        return res;
    }

    static Long[] toWrapper(long[] arr) {
        Long[] res = new Long[arr.length];
        for (int i = 0; i < arr.length; i++) {
            res[i] = arr[i];
        }
        return res;
    }

    static Long[][] toWrapper(long[][] arr) {
        Long[][] res = new Long[arr.length][];
        for (int i = 0; i < arr.length; i++) {
            res[i] = toWrapper(arr[i]);
        }
        return res;
    }
}