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


public final class Main15 {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskA solver = new TaskA();
        int t = 1;
        solver.solve(t, in, out);
        out.close();
    }
}


class TaskA {

	int get_gcd(int a, int b) {
		int temp = 0;
		while (b != 0) {
			temp = a%b;
			a = b;
			b = temp;
		}
		return a;
	}

	int getSquareRoot(int num) {
		int a = 1;
		while (a * 1L * a <= 1L * num) {
			a++;
		}
		return a--;
	}

	int binarySearch(ArrayList<Integer> divisors, int s, int e, int start, int end) {
		if (s > e) {
			return -1;
		}
		int mid = s + (e - s + 1) / 2;
		if (divisors.get(mid) >= start && divisors.get(mid) <= end) {
			if (e == s)
				return s;
			return binarySearch(divisors, mid, e, start, end);
		} else if (divisors.get(mid) < start) {
			return binarySearch(divisors, mid+1, e, start, end); 
		} else if (divisors.get(mid) > end) {
			return binarySearch(divisors, s, mid-1, start, end);
		}
		return -1;
	}

    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int a = in.nextInt();
        int b = in.nextInt();
        int gcd = get_gcd(a, b);
        ArrayList<Integer> divisors = new ArrayList<>();
        Stack<Integer> backward = new Stack<>();
        int root = getSquareRoot(gcd);
        for (int i = 1; i < root; i++) {
        	if (gcd % i == 0) {
        		divisors.add(i);
        		backward.push(gcd/i);
        	}
        }
        if (gcd % root == 0) {
        	divisors.add(root);
        }
        while (!backward.isEmpty()) {
        	divisors.add(backward.pop());
        }
        int ranges = in.nextInt();
       	while (ranges-- > 0) {
       		int start = in.nextInt();
       		int end = in.nextInt();
       		int index = binarySearch(divisors, 0, divisors.size() - 1, start, end);
       		if (index == -1)
       			out.println("-1");
       		else
       			out.println(divisors.get(index));
       	}
    }
}

class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream), 32768);
        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());
    }
}