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.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;

public class MGCD {
	public static int busqueda(int b, ArrayList<Integer> p) {
		int min = 0;
		int max = p.size() - 1;
		return busqueda(b, p, min, max, -1);
	}

	public static int busqueda(int b, ArrayList<Integer> p, int min, int max,
			int j) {
		if ((max - min) <= 1) {
			if (p.get(max) <= b) {
				return max;
			} else if (p.get(min) <= b) {
				return min;
			} else {
				return -1;
			}
		}
		int i = (min + max) / 2;
		if (p.get(i) == b) {
			return i;
		} else if (i < p.size() - 1 && (p.get(i) <= b && p.get(i + 1) >= b)) {
			return i;
		} else if (p.get(i) > b) {
			max = i;
			return Math.max(busqueda(b, p, min, max, j), j);
		} else {
			min = i;
			j = i;
			return Math.max(busqueda(b, p, min, max, j), j);
		}
	}

	public static ArrayList<Integer> divs(int x, int y) {
		ArrayList<Integer> list1=div(y);
		ArrayList<Integer> list2= div(x);
		ArrayList<Integer> lista = new ArrayList<Integer>();
		for (int i=0; i<list1.size(); i++){
			if (list2.contains(list1.get(i))){
				lista.add(list1.get(i));
			}
		}
		return lista;
	}

	public static ArrayList<Integer> div(int x) {
		ArrayList<Integer> list = new ArrayList<Integer>();
		int n = (int) Math.sqrt(x) + 1;
		for (int i = 1; i < n; i++) {
			if (x % i == 0) {
				list.add(i);
				if (x / i != i) {
					list.add(x / i);
				}
			}
		}
		Collections.sort(list);
		return list;
	}

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		String[] ab = bf.readLine().split(" ");
		int a = Integer.parseInt(ab[0]);
		int b = Integer.parseInt(ab[1]);
		int n = Integer.parseInt(bf.readLine());
		int[][] ns = new int[n][2];
		ArrayList<Integer> sol = new ArrayList<Integer>();
		int max = 0;
		int min = Integer.MAX_VALUE;
		for (int i = 0; i < n; i++) {
			String[] xy = bf.readLine().split(" ");
			ns[i][0] = Integer.parseInt(xy[0]);
			ns[i][1] = Integer.parseInt(xy[1]);
			if (ns[i][1] > max) {
				max = ns[i][1];
			}
			if (min > ns[i][0]) {
				min = ns[i][0];
			}
		}
		if (min == Math.min(a, b) || max == 1) {
			sol.add(1);
			sol.add(Math.min(a, b));
		} else if (a == b) {
			sol = div(a);
		} else {
			sol = divs(a, b);
		}
		Collections.sort(sol);
		for (int i = 0; i < n; i++) {
			int x = ns[i][0];
			int y = ns[i][1];
			if (x == y) {
				if (x == 1) {
					System.out.println(x);
				} else if (sol.contains(x)) {
					System.out.println(x);
				} else {
					System.out.println("-1");
				}
			} else {
				int j = busqueda(y, sol);
				if (j < sol.size() - 1 && sol.get(j + 1) <= y) {
					System.out.println(sol.get(j + 1));
				} else if (sol.get(j) < x) {
					System.out.println("-1");
				} else if (sol.get(j) > y && j > 0) {
					if (sol.get(j - 1) < x) {
						System.out.println("-1");
					} else {
						System.out.println(sol.get(j - 1));
					}
				} else {
					System.out.println(sol.get(j));
				}
			}
		}
	}
}