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.util.*;
import java.io.*;
public class Prob7 {
	public static void main(String[] args) throws IOException {
		Reader sc = new Reader();
		PrintWriter out = new PrintWriter(System.out);
		int a = sc.nextInt(), b = sc.nextInt(), ans = 0;
		while(a != 0 && b != 0) {
			if(a > b) a -= b;
			else b -= a;
		}
		int gcd = Math.max(a, b);
		ArrayList<Integer> c = new ArrayList<Integer>();
		for(int i = 1; i <= Math.sqrt(gcd); i++) {
			if(gcd % i == 0) {
				c.add(i);
				if(i != gcd/i) c.add(gcd/i);
			}
		}
		Collections.sort(c);
		int n = sc.nextInt();
		for(int i = 0; i < n; i++) {
			int l = sc.nextInt(), h = sc.nextInt();
			int d = Collections.binarySearch(c, h);
			if(d < 0) d = -d-2;
			if(d == -1) {
				out.println(d);
				continue;
			}
			if(c.get(d) >= l)out.println(c.get(d));
			else out.println(-1);
		}
		out.close();
	}

		static class Reader {
			final private int BUFFER_SIZE = 1 << 16;
			private DataInputStream din;
			private byte[] buffer;
			private int bufferPointer, bytesRead;

			public Reader()
			{
				din = new DataInputStream(System.in);
				buffer = new byte[BUFFER_SIZE];
				bufferPointer = bytesRead = 0;
			}

			public Reader(String file_name) throws IOException
			{
				din = new DataInputStream(
					new FileInputStream(file_name));
				buffer = new byte[BUFFER_SIZE];
				bufferPointer = bytesRead = 0;
			}

			public String readLine() throws IOException
			{
				byte[] buf = new byte[64]; // line length
				int cnt = 0, c;
				while ((c = read()) != -1) {
					if (c == '\n') {
						if (cnt != 0) {
							break;
						}
						else {
							continue;
						}
					}
					buf[cnt++] = (byte)c;
				}
				return new String(buf, 0, cnt);
			}

			public int nextInt() throws IOException
			{
				int ret = 0;
				byte c = read();
				while (c <= ' ') {
					c = read();
				}
				boolean neg = (c == '-');
				if (neg)
					c = read();
				do {
					ret = ret * 10 + c - '0';
				} while ((c = read()) >= '0' && c <= '9');

				if (neg)
					return -ret;
				return ret;
			}

			public long nextLong() throws IOException
			{
				long ret = 0;
				byte c = read();
				while (c <= ' ')
					c = read();
				boolean neg = (c == '-');
				if (neg)
					c = read();
				do {
					ret = ret * 10 + c - '0';
				} while ((c = read()) >= '0' && c <= '9');
				if (neg)
					return -ret;
				return ret;
			}

			public double nextDouble() throws IOException
			{
				double ret = 0, div = 1;
				byte c = read();
				while (c <= ' ')
					c = read();
				boolean neg = (c == '-');
				if (neg)
					c = read();

				do {
					ret = ret * 10 + c - '0';
				} while ((c = read()) >= '0' && c <= '9');

				if (c == '.') {
					while ((c = read()) >= '0' && c <= '9') {
						ret += (c - '0') / (div *= 10);
					}
				}

				if (neg)
					return -ret;
				return ret;
			}

			private void fillBuffer() throws IOException
			{
				bytesRead = din.read(buffer, bufferPointer = 0,
									BUFFER_SIZE);
				if (bytesRead == -1)
					buffer[0] = -1;
			}

			private byte read() throws IOException
			{
				if (bufferPointer == bytesRead)
					fillBuffer();
				return buffer[bufferPointer++];
			}

			public void close() throws IOException
			{
				if (din == null)
					return;
				din.close();
			}
		}
}