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

Difficulty: MEDIUM_HARD

Solutions:
import math
from sys import *
from bisect import bisect_right
input = stdin.readline
print = stdout.write
(a, b) = map(int, input().split())
n = int(input())
gcd = math.gcd(a, b)
factors = []
i = 1
while i * i <= gcd:
	if gcd % i == 0:
		factors.append(gcd // i)
		if i * i != gcd:
			factors.append(gcd // (gcd // i))
	i += 1
factors.sort()
for _ in range(n):
	(l, h) = map(int, input().split())
	ans = -1
	index = bisect_right(factors, h)
	if factors[index - 1] >= l and factors[index - 1] <= h:
		ans = factors[index - 1]
	print(str(ans) + '\n')


def arr_inp(n):
	if n == 1:
		return [int(x) for x in stdin.readline().split()]
	elif n == 2:
		return [float(x) for x in stdin.readline().split()]
	else:
		return [str(x) for x in stdin.readline().split()]

def factorize(n):
	(c, ans) = (1, [])
	while c * c < n:
		if n % c == 0:
			ans.extend([c, n // c])
		c += 1
	if c * c == n:
		ans.append(c)
	return sorted(ans)
from sys import stdin
from math import gcd
from bisect import *
(a, b) = arr_inp(1)
arr = factorize(gcd(a, b))
for i in range(int(input())):
	(lo, hi) = arr_inp(1)
	if hi < arr[0] or lo > arr[-1]:
		print(-1)
	else:
		ixh = bisect_right(arr, hi)
		if ixh == len(arr):
			print(arr[ixh - 1])
		elif ixh > 0 and arr[ixh] > hi and (arr[ixh - 1] < lo):
			print(-1)
		elif arr[ixh] == hi:
			print(arr[ixh])
		else:
			print(arr[ixh - 1])


(a, b) = map(int, input().split())
import math
g = math.gcd(a, b)
divisor = [i for i in range(1, int(math.sqrt(g)) + 1) if g % i == 0]
for i in divisor[::-1]:
	divisor.append(g // i)
n = int(input())
s = len(divisor) - 1
for i in range(n):
	ans = 0
	L = 0
	H = s
	(l, h) = map(int, input().split())
	while L <= H:
		m = (L + H) // 2
		if divisor[m] <= h:
			ans = m
			L = m + 1
		else:
			H = m - 1
	if divisor[ans] < l:
		print(-1)
	else:
		print(divisor[ans])


def factors(n):
	i = 2
	res = list()
	while i * i <= n:
		if n % i == 0:
			cnt = 0
			while n % i == 0:
				n //= i
				cnt += 1
			res.append((i, cnt))
		i += 1
	if not n == 1:
		res.append((n, 1))
	return res

def generate_divisors(factors, ind, result, divisors):
	if ind == len(factors):
		divisors.append(result)
		return
	for i in range(factors[ind][1] + 1):
		generate_divisors(factors, ind + 1, result, divisors)
		result *= factors[ind][0]
	if ind == 0:
		return divisors

def search(s, e, x, y):
	while s < e:
		mid = s + (e - s) // 2
		if y >= common[mid]:
			s = mid + 1
		else:
			e = mid + 0
	return common[s - 1]
(a, b) = map(int, input().strip().split())
div_a = generate_divisors(factors(a), 0, 1, [])
div_b = generate_divisors(factors(b), 0, 1, [])
common = list(set(div_a).intersection(set(div_b)))
common = sorted(common)
c_size = len(common)
(c_max, c_min) = (common[c_size - 1], common[0])
n = int(input())
for _ in range(n):
	(x, y) = map(int, input().strip().split())
	if x > c_max or y < c_min:
		print(-1)
	else:
		ans = search(0, c_size, x, y)
		if ans < x or ans > y:
			print(-1)
		else:
			print(ans)


(a, b) = map(int, input().split())
import math
g = math.gcd(a, b)
lis = [i for i in range(1, int(math.sqrt(g)) + 1) if g % i == 0]
for i in lis[::-1]:
	lis.append(g // i)
n = int(input())
for _ in range(n):
	(a, b) = map(int, input().split())
	import bisect
	x = bisect.bisect(lis, b) - 1
	if lis[x] < a:
		print(-1)
	else:
		print(lis[x])


import math

def solve():
	(A, B) = map(int, input().split())
	gcdab = math.gcd(A, B)
	divisors = []
	i = 1
	while i * i <= gcdab:
		if gcdab % i == 0:
			divisors.append(i)
			divisors.append(gcdab // i)
		i += 1
	divisors = sorted(divisors)
	for _ in range(int(input())):
		(left, right) = map(int, input().split())
		res = -1
		for i in divisors:
			if left <= i and i <= right:
				res = max(res, i)
		print(res)
Testcase = 1
for _ in range(Testcase):
	solve()


import math
import bisect

def gcd(a, b):
	while a != 0:
		(a, b) = (b % a, a)
	return b

def calc_divisors(num):
	divisors = []
	for i in range(1, int(math.sqrt(num)) + 1):
		(n, f) = divmod(num, i)
		if f == 0:
			divisors.append(i)
			divisors.append(n)
	divisors.sort()
	return divisors
(a, b) = map(int, input().split())
common_divisors = calc_divisors(gcd(a, b))
size = int(input())
result = ['-1'] * size
for i in range(size):
	(low, high) = map(int, input().split())
	ie = bisect.bisect_right(common_divisors, high)
	cd = common_divisors[ie - 1]
	if low <= cd <= high:
		result[i] = str(cd)
print('\n'.join(result))


import math
(n, m) = map(int, input().split())
d = math.gcd(n, m)
k = 1
l = []
for i in range(k, int(d ** 0.5) + 1):
	if d % i == 0:
		if d // i == i:
			l.append(i)
		else:
			l.append(d // i)
			l.append(i)
l.sort()
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	m = -1
	for i in l:
		if i >= a and i <= b:
			m = max(i, m)
	print(m)


import bisect

def gcd(a, b):
	while b > 0:
		t = b
		b = a % b
		a = t
	return a
(a, b) = map(int, input().split())
g = gcd(a, b)
d = []
i = 1
while i * i < g:
	if g % i == 0:
		d.append(i)
		d.append(g // i)
	i += 1
if i * i == g:
	d.append(i)
d.sort()
n = int(input())
for i in range(0, n):
	(low, high) = map(int, input().split())
	(r, l) = (bisect.bisect_right(d, high), bisect.bisect_left(d, low))
	if r > l:
		print(d[r - 1])
	else:
		print(-1)


from sys import stdin, stdout
from math import gcd, sqrt

def getin(type):
	return type(stdin.readline().strip())

def getrin(type):
	return [type(x) for x in stdin.readline().strip().split()]

def getout(object, type):
	return stdout.write(str(type(object)) + '\n')
(a, b) = getrin(int)
JoJo = gcd(a, b)
factors = list()
for i in range(1, int(sqrt(JoJo) + 1)):
	if JoJo % i == 0:
		factors.append(i)
		if JoJo / i != i:
			factors.append(JoJo / i)
n = getin(int)
for i in range(n):
	(low, high) = getrin(int)
	Max = -1
	for factor in factors:
		if low <= factor <= high:
			Max = max(Max, factor)
	getout(Max, int)


def gcd(a, b):
	while a > 0:
		x = a
		a = b % a
		b = x
	return x

def Bsearch1(L, x):
	beg = 0
	last = len(L) - 1
	while beg <= last:
		mid = (beg + last) // 2
		if x == L[mid]:
			return mid
		elif x > L[mid]:
			beg = mid + 1
		else:
			last = mid - 1
	else:
		return beg

def Bsearch2(L, x):
	beg = 0
	last = len(L) - 1
	while beg <= last:
		mid = (beg + last) // 2
		if x == L[mid]:
			return mid
		elif x > L[mid]:
			beg = mid + 1
		else:
			last = mid - 1
	else:
		return last
(a, b) = input().split()
(a, b) = [int(a), int(b)]
x = gcd(a, b)
from functools import reduce

def factors(n):
	x = set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))
	return list(x)
div = factors(x)
div.sort()
y = int(input())
for j in range(y):
	(c, d) = input().split()
	(c, d) = [int(c), int(d)]
	f = Bsearch1(div, c)
	g = Bsearch2(div, d)
	if f > g:
		print(-1)
	else:
		print(div[g])


import sys
input = sys.stdin.readline
from math import *

def bs(a, x, y):
	l = 0
	r = len(a) - 1
	ans = -1
	while l <= r:
		mid = l + (r - l) // 2
		if x <= a[mid] <= y:
			ans = a[mid]
			l = mid + 1
		elif a[mid] > y:
			r = mid - 1
		else:
			l = mid + 1
	return ans
(a, b) = map(int, input().split())
div = []
g = gcd(a, b)
for i in range(1, int(sqrt(g)) + 1):
	if g % i == 0:
		div.append(i)
		if g // i != i:
			div.append(g // i)
div.sort()
q = int(input())
for j in range(q):
	(x, y) = map(int, input().split())
	ans = bs(div, x, y)
	if x <= ans <= y:
		print(ans)
	else:
		print(-1)


import math
(r, o) = map(int, input().split())
from bisect import *

def fact(x):
	ans = []
	for i in range(1, int(math.sqrt(int(x))) + 1):
		if x % i == 0:
			if x // i == i:
				ans.append(i)
			else:
				ans.append(x // i)
				ans.append(i)
	return ans
m = int(input())
a = math.gcd(r, o)
tem = fact(a)
tem.sort()
for i in range(m):
	(x, y) = map(int, input().split())
	r = bisect_left(tem, y)
	if r == len(tem):
		if tem[-1] < x:
			print(-1)
		else:
			print(tem[-1])
	elif tem[r] <= y:
		if tem[r] < x:
			print(-1)
		else:
			print(tem[r])
	elif r == 0:
		print(-1)
	elif tem[r - 1] < x:
		print(-1)
	else:
		print(tem[r - 1])


import bisect
import collections
import copy
import enum
import functools
import heapq
import itertools
import math
import random
import re
import sys
import time
import string
from typing import List
(a, b) = map(int, input().split())
g = math.gcd(a, b)
gs = []
it = 1
while it * it <= g:
	if g % it == 0:
		gs.append(it)
		if g // it != it:
			gs.append(g // it)
	it += 1
gs.sort()
for _ in range(int(input())):
	(low, high) = map(int, input().split())
	lt = bisect.bisect_left(gs, low)
	ht = bisect.bisect_right(gs, high)
	if ht > lt:
		print(gs[ht - 1])
	else:
		print(-1)


from bisect import bisect_left
from math import *

def BinarySearch(a, x):
	i = bisect_left(a, x)
	if i:
		return i - 1
	else:
		return -1
(a, b) = map(int, input().split())
n = int(input())
ramu = gcd(a, b)
factors = set()
for i in range(1, int(ramu ** 0.5) + 1):
	if ramu % i == 0:
		factors.add(i)
		factors.add(ramu // i)
factors = list(factors)
factors.sort()
for i in range(n):
	(low, high) = map(int, input().split())
	res = BinarySearch(factors, high + 1)
	res = factors[res]
	if res < low:
		res = -1
	print(res)


import math

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)
(a, b) = map(int, input().split())
q = int(input())
x = gcd(a, b)
l = []
for i in range(1, int(math.sqrt(x)) + 1):
	if x % i == 0:
		if i * i == x:
			l.append(i)
		else:
			l.append(i)
			l.append(x // i)
l.sort()
for _ in range(q):
	(s, e) = map(int, input().split())
	low = 0
	high = len(l) - 1
	ans = -1
	while low <= high:
		mid = (low + high) // 2
		if l[mid] == e:
			ans = l[mid]
			break
		elif l[mid] < e:
			ans = l[mid]
			low = mid + 1
		else:
			high = mid - 1
	if ans == -1 or ans < s:
		print(-1)
	else:
		print(ans)


from sys import stdin, stdout
import bisect
import math
from collections import deque
mod = 10 ** 9 + 7

def st():
	return list(stdin.readline().strip())

def inp():
	return int(stdin.readline())

def li():
	return list(map(int, stdin.readline().split()))

def mp():
	return map(int, stdin.readline().split())

def pr(n):
	stdout.write(str(n) + '\n')

def DFS(dictionary, vertex, visited):
	visited[vertex] = True
	stack = [vertex]
	print(vertex)
	while stack:
		a = stack.pop()
		for i in dictionary[a]:
			if not visited[i]:
				print(i)
				visited[i] = True
				stack.append(i)

def BFS(d, visited, i, value, dis):
	value[i] = dis
	visited[i] = True
	q = deque()
	q.append(i)
	while q:
		a = q.popleft()
		for i in d[a]:
			if not visited[i]:
				visited[i] = True
				value[i] = value[a] + 1
				q.append(i)

def soe(limit):
	l = [1] * (limit + 1)
	l[0] = 0
	l[1] = 0
	prime = []
	for i in range(2, limit + 1):
		if l[i]:
			for j in range(i * i, limit + 1, i):
				l[j] = 0
	for i in range(2, limit + 1):
		if l[i]:
			prime.append(i)
	return prime

def segsoe(low, high):
	limit = int(high ** 0.5) + 1
	prime = soe(limit)
	n = high - low + 1
	l = [0] * (n + 1)
	for i in range(len(prime)):
		lowlimit = low // prime[i] * prime[i]
		if lowlimit < low:
			lowlimit += prime[i]
		if lowlimit == prime[i]:
			lowlimit += prime[i]
		for j in range(lowlimit, high + 1, prime[i]):
			l[j - low] = 1
	for i in range(low, high + 1):
		if not l[i - low]:
			if i != 1:
				print(i)

def gcd(a, b):
	while b:
		a = a % b
		(b, a) = (a, b)
	return a

def power(a, n):
	r = 1
	while n:
		if n & 1:
			r = r * a
		a *= a
		n = n >> 1
	return r

def solve():
	(a, b) = mp()
	p = gcd(a, b)
	l = []
	i = 1
	while i * i <= p:
		if p % i == 0:
			if i == p // i:
				l.append(i)
			else:
				l.append(i)
				l.append(p // i)
		i += 1
	l.sort()
	for _ in range(inp()):
		(x, y) = mp()
		ans = -1
		for i in l:
			if i >= x and i <= y:
				ans = max(ans, i)
		pr(ans)
for _ in range(1):
	solve()


import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n
avl = AvlTree()

class SegmentTree1:

	def __init__(self, data, default='z', func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a * a + b * b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] <= key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater
(a, b) = map(int, input().split())
g = math.gcd(a, b)
a = set([1])
for i in range(2, int(g ** 0.5) + 1):
	if g % i == 0:
		a.add(i)
		a.add(g // i)
a = list(a)
a.append(g)
a.sort()
for i in range(int(input())):
	(a1, b) = map(int, input().split())
	e = binarySearchCount(a, len(a), b)
	if a1 > a[e - 1]:
		print(-1)
	else:
		print(a[e - 1])


import sys
from math import sqrt

def mcd(p, q):
	if p % q == 0:
		return q
	return mcd(q, p % q)

def divMcd(n):
	div = []
	for i in range(1, int(sqrt(n)) + 1):
		if n % i == 0:
			div.append(i)
			div.append(n // i)
	return div
(a, b) = map(int, sys.stdin.readline().split())
mcd_ = mcd(a, b)
divisores = divMcd(mcd_)
divisores = list(set(divisores))
divisores.sort()
q = int(sys.stdin.readline())
for _ in range(q):
	(l, h) = map(int, sys.stdin.readline().split())
	max_ = -1
	for i in range(len(divisores)):
		if divisores[i] >= l and divisores[i] <= h:
			max_ = max(max_, divisores[i])
	print(max_)


from sys import stdin
import bisect
input = stdin.readline
(a, b) = map(int, input().split())
p = set()
for i in range(1, int(a ** 0.5) + 1):
	if a % i == 0:
		if b % i == 0:
			p.add(i)
		if b % (a // i) == 0:
			p.add(a // i)
for i in range(1, int(b ** 0.5) + 1):
	if b % i == 0:
		if a % i == 0:
			p.add(i)
		if a % (b // i) == 0:
			p.add(b // i)
p = sorted(list(p))
p.sort()
n = int(input())
for _ in range(n):
	(l, r) = map(int, input().split())
	idx1 = bisect.bisect_left(p, l)
	idx2 = bisect.bisect_left(p, r)
	ans = -1
	for j in range(idx2 - 2, idx2 + 2):
		if 0 <= j < len(p) and l <= p[j] <= r:
			ans = p[j]
	print(ans)


from math import *

def nwd(a, b):
	while b != 0:
		c = a % b
		a = b
		b = c
	return a

def binSearch(h):
	p = 0
	k = len(dzielniki) - 1
	while p < k:
		s = int((p + k + 1) / 2)
		if dzielniki[s] > h:
			k = s - 1
		else:
			p = s
	return dzielniki[p]
(a, b) = map(int, input().split())
najwyzszy_dzielnik = nwd(a, b)
dzielniki = []
pierwiastek = int(sqrt(najwyzszy_dzielnik))
for i in range(1, pierwiastek + 1):
	if najwyzszy_dzielnik % i == 0:
		dzielniki.append(i)
		dzielniki.append(int(najwyzszy_dzielnik / i))
		if i * i == najwyzszy_dzielnik:
			dzielniki.pop()
dzielniki.sort()
t = int(input())
for i in range(t):
	(low, high) = map(int, input().split())
	wynik = binSearch(high)
	if wynik <= high and wynik >= low:
		print(wynik)
	else:
		print(-1)


from sys import stdin, stdout

def __gcd(a, b):
	if b == 0:
		return a
	else:
		return __gcd(b, a % b)

def main():
	(a, b) = map(int, stdin.readline().split())
	gcd = __gcd(a, b)
	i = 1
	factor = []
	while i * i <= gcd:
		if gcd % i == 0:
			factor.append(i)
			if i * i != gcd:
				factor.append(gcd // i)
		i += 1
	q = int(stdin.readline())
	for i in range(q):
		res = -1
		(l, r) = map(int, stdin.readline().split())
		for j in range(len(factor)):
			if factor[j] >= l and factor[j] <= r:
				res = max(res, factor[j])
		stdout.write(str(res) + '\n')
main()


import sys
import math
MAXNUM = math.inf
MINNUM = -1 * math.inf
ASCIILOWER = 97
ASCIIUPPER = 65

def getInt():
	return int(sys.stdin.readline().rstrip())

def getInts():
	return map(int, sys.stdin.readline().rstrip().split(' '))

def getString():
	return sys.stdin.readline().rstrip()

def printOutput(ans):
	for ele in ans:
		sys.stdout.write(str(ele) + '\n')

def fac(g):
	if g == 2:
		return [1, 2]
	facs = [1]
	MAX = math.sqrt(g) + 1
	cur = 2
	while cur < MAX:
		if g % cur == 0:
			facs.append(cur)
			facs.append(g // cur)
		cur += 1
	facs.append(g)
	return facs

def binsearch(lst, low, high):
	l = 0
	r = len(lst) - 1
	while l <= r:
		m = (l + r) // 2
		if lst[m] > high:
			r = m - 1
		else:
			l = m + 1
	return r

def solve(a, b, queries):
	ans = []
	g = math.gcd(a, b)
	factors = fac(g)
	factors.sort()
	for (l, r) in queries:
		res = binsearch(factors, l, r)
		if l <= factors[res] <= r:
			ans.append(factors[res])
		else:
			ans.append(-1)
	return ans

def readinput():
	(a, b) = getInts()
	q = getInt()
	queries = []
	for _ in range(q):
		queries.append(tuple(getInts()))
	printOutput(solve(a, b, queries))
readinput()


from bisect import bisect_left, bisect_right, insort
import sys
import heapq
from math import *
from collections import defaultdict as dd
from collections import deque

def data():
	return sys.stdin.readline().strip()

def mdata():
	return map(int, data().split())
(a, b) = mdata()
div = []
(a, b) = (min(a, b), max(a, b))
for i in range(1, int(a ** 0.5) + 1):
	if a % i == 0:
		if b % i == 0:
			div.append(i)
		if b % (a // i) == 0 and i != a // i:
			div.append(a // i)
div.sort()
n = int(input())
for i in range(n):
	(a, b) = mdata()
	b1 = bisect_right(div, b)
	a1 = bisect_left(div, a)
	if b1 - a1 == 0:
		print(-1)
	else:
		print(div[b1 - 1])


from sys import stdin
input = stdin.readline
from heapq import heapify, heappush, heappop, heappushpop
from collections import defaultdict as dd, deque as dq, Counter as C
from math import factorial as f, ceil, gcd, sqrt, log
from bisect import bisect_left as bl, bisect_right as br
from itertools import combinations as c, permutations as p
from math import factorial as f, ceil, gcd, sqrt, log
mp = lambda : map(int, input().split())
it = lambda : int(input())
ls = lambda : list(input().strip())

def isprime(n):
	for j in range(3, int(sqrt(n)) + 1, 2):
		if n % j == 0:
			return 0
	return 1

def factors(n):
	s = set()
	s.add(1)
	s.add(n)
	for j in range(2, int(sqrt(n)) + 1):
		if n % j == 0:
			s.add(j)
			s.add(n // j)
	return s
(a, b) = mp()
k = sorted(factors(a))
for _ in range(it()):
	(l, h) = mp()
	ans = -1
	for el in range(br(k, h) - 1, bl(k, l) - 1, -1):
		if b % k[el] == 0:
			ans = k[el]
			break
	if ans >= l and ans <= h:
		print(ans)
	else:
		print(-1)


import math
from sys import stdout

def print_fast(string):
	stdout.write(string + '\n')

def MGCD(_gcd, low, high):
	answer = -1
	botton = low
	index = min(_gcd, high)
	while index >= botton:
		if _gcd % index == 0:
			return index
		index -= 1
	return answer
_input = input().split()
a = int(_input[0])
b = int(_input[1])
_gcd = math.gcd(a, b)
divisors = []
i = 1
while i * i <= _gcd:
	if _gcd % i == 0:
		divisors.append(i)
		divisors.append(_gcd // i)
	i += 1
divisors = sorted(divisors)
testCases = int(input())
ans = []
for i in range(testCases):
	_input = input().split()
	l = int(_input[0])
	h = int(_input[1])
	answer = -1
	for i in divisors:
		if i > h:
			break
		if i >= l and i <= h:
			answer = i
	ans.append(str(answer))
print_fast('\n'.join(ans))


import math
from bisect import bisect_right
(a, b) = map(int, input().split())
g = math.gcd(a, b)
j = 1
ans = []
while j * j <= g:
	if g % j == 0:
		ans.append(j)
		ans.append(g // j)
	j = j + 1
ans.sort()
n = int(input())
for i in range(0, n):
	(low, high) = map(int, input().split())
	pos = bisect_right(ans, high) - 1
	num = ans[pos]
	if num < low:
		print(-1)
	else:
		print(num)


import math
(a, b) = map(int, input().split())
g = math.gcd(a, b)
l1 = []
for i in range(1, int(math.sqrt(g)) + 1):
	if g % i == 0:
		l1.append(i)
		l1.append(g // i)
n = int(input())
for i in range(n):
	(l, r) = map(int, input().split())
	res = -1
	for j in range(len(l1)):
		if l1[j] >= l and l1[j] <= r:
			res = max(res, l1[j])
	print(res)


from math import gcd
(a, b) = map(int, input().split())
g = gcd(a, b)
d = []
i = 1
while i * i <= g:
	if not g % i:
		d.append(i)
	i += 1
for i in range(len(d) - 1, -1, -1):
	if d[i] * d[i] != g:
		d.append(g // d[i])
for test in range(int(input())):
	(l, r) = map(int, input().split())
	L = 0
	R = len(d) - 1
	while L < R:
		middle = L + R + 1 >> 1
		if d[middle] > r:
			R = middle - 1
		else:
			L = middle
	if l <= d[L] <= r:
		print(d[L])
	else:
		print(-1)
exit(0)


from math import gcd
(a, b) = list(map(int, input().split()))
cd = gcd(a, b)
ar = []
for i in range(1, int(cd ** 0.5) + 1):
	if cd % i == 0:
		ar.append(i)
		if i * i < cd:
			ar.append(cd // i)
for _ in range(int(input())):
	(l, h) = list(map(int, input().split()))
	res = -1
	for i in ar:
		if i >= l and i <= h:
			res = max(res, i)
	print(res)


from bisect import bisect_right as br
from bisect import bisect_left as bl
import math
from functools import reduce

def fac(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))
(a, b) = map(int, input().split())
f1 = fac(a)
f2 = fac(b)
f = list(f1 & f2)
f = list(set(f))
f.sort()
n = int(input())
for i in range(n):
	(l, r) = map(int, input().split())
	ans = br(f, r)
	lo = bl(f, l)
	hi = br(f, r)
	if lo == hi:
		print(-1)
	else:
		print(f[ans - 1])


import math
(a, b) = map(int, input().split())
g = math.gcd(a, b)
l1 = []
for i in range(1, int(math.sqrt(g)) + 1):
	if g % i == 0:
		l1.append(i)
		if i * i != g:
			l1.append(g // i)
l1.sort()
n = int(input())
for i in range(n):
	(l, r) = map(int, input().split())
	res = -1
	x = 0
	h = len(l1) - 1
	while x <= h:
		m = (x + h) // 2
		if l1[m] > r:
			h = m - 1
		elif l1[m] < l:
			x = m + 1
		else:
			res = max(res, l1[m])
			x = m + 1
	print(res)


def gcd(m, n):
	while n != 0:
		t = m % n
		m = n
		n = t
	return m

def get_divisors(g):
	from math import sqrt
	rt = int(sqrt(g))
	divisors = []
	for x in range(1, rt + 1):
		if g % x == 0:
			divisors.append(x)
			divisors.append(g // x)
	if divisors[-1] == divisors[-2]:
		divisors.pop()
	divisors.sort()
	return divisors

def main():
	from sys import stdin, stdout
	from bisect import bisect_right
	it = iter(map(int, stdin.read().split()))
	(a, b) = (next(it), next(it))
	divisors = get_divisors(gcd(a, b))
	n = next(it)
	ans = []
	for _ in range(n):
		(low, high) = (next(it), next(it))
		idx = bisect_right(divisors, high)
		d = divisors[idx - 1]
		ans.append(str(d if low <= d <= high else -1))
		ans.append('\n')
	stdout.write(''.join(ans))
main()


import os
import sys
from io import BytesIO, IOBase
from collections import Counter
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return a * b / gcd(a, b)

def main():
	(a, b) = map(int, input().split())
	n = int(input())
	k = gcd(a, b)
	avls = []
	i = 1
	while i * i <= k:
		if k % i == 0:
			avls.append(i)
			if i != k // i:
				avls.append(k // i)
		i += 1
	avls.sort()
	for i in range(n):
		(l, h) = map(int, input().split())
		if l > avls[len(avls) - 1] or h < avls[0]:
			print(-1)
		else:
			lo = 0
			hi = len(avls) - 1
			ans = -1
			while lo <= hi:
				mid = (lo + hi) // 2
				if avls[mid] > h:
					hi = mid - 1
				else:
					if avls[mid] >= l:
						ans = max(ans, avls[mid])
					lo = mid + 1
			print(ans)
	return
main()


import bisect

def nod(a, b):
	while True:
		if a > b:
			if a % b == 0:
				return b
			a %= b
		else:
			if b % a == 0:
				return a
			b %= a
(a, b) = map(int, input().split())
d = nod(a, b)
ds = []
for i in range(1, int(d ** 0.5) + 1):
	if d % i == 0:
		ds.append(i)
		ds.append(d // i)
ds = sorted(list(set(ds)))
for _ in range(int(input())):
	(L, R) = map(int, input().split())
	x = bisect.bisect(ds, R) - 1
	if ds[x] < L:
		print(-1)
	else:
		print(ds[x])


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
(a, b) = map(int, input().split())
n = int(input())
from math import gcd
from bisect import bisect
g = gcd(a, b)
f = [1, g]
for i in range(2, g):
	if i * i > g:
		break
	if g % i == 0:
		f.append(i)
		if i != g // i:
			f.append(g // i)
f.sort()
for i in range(n):
	(l, r) = map(int, input().split())
	a = bisect(f, l)
	b = bisect(f, r)
	if b - a == 0:
		if f[b - 1] != l:
			print(-1)
		else:
			print(l)
	else:
		print(f[b - 1])


from sys import stdin
from collections import Counter
from bisect import bisect
test = stdin.readlines()
(a, b) = map(int, test[0].split())
n = int(test[1])
queries = [[int(c) for c in test[i].split()] for i in range(2, 2 + n)]

def common_divisors(a, b):

	def divisors(n):
		ans = set()
		i = 1
		while i * i <= n:
			if n % i == 0:
				ans.add(i)
			i += 1
		others = {n // e for e in ans}
		return ans | others
	(adiv, bdiv) = (divisors(a), divisors(b))
	return sorted(adiv & bdiv)
divisors = common_divisors(a, b)
ans = []
for (l, r) in queries:
	idx = bisect(divisors, r) - 1
	if 0 <= idx < len(divisors) and l <= divisors[idx] <= r:
		ans.append(divisors[idx])
	else:
		ans.append(-1)
print('\n'.join(map(str, ans)))


import math
import bisect
(a, b) = list(map(int, input().split()))
meragcd = math.gcd(a, b)
ans = []
p = math.sqrt(meragcd)
for i in range(2, int(p) + 1):
	g = 0
	if meragcd % i == 0:
		if meragcd % i == i:
			ans.append(i)
		else:
			ans.append(i)
			ans.append(meragcd // i)
if meragcd > 1:
	ans.append(meragcd)
ans = set(ans)
ans = list(ans)
ans.sort()
if 1 not in ans:
	ans.insert(0, 1)
q = int(input())
for i in range(q):
	(low, high) = list(map(int, input().split()))
	index1 = bisect.bisect_left(ans, low)
	index2 = bisect.bisect_right(ans, high)
	if index1 == len(ans):
		print(-1)
	elif index2 == index1 and index1 != len(ans) and (index2 != len(ans)):
		print(-1)
	else:
		print(ans[index2 - 1])


def make_divisors(n):
	divisors = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			divisors.append(i)
			if i != n // i:
				divisors.append(n // i)
	divisors.sort()
	return divisors
(a, b) = map(int, input().split())
n = int(input())
import math
g = math.gcd(a, b)
D = make_divisors(g)
import bisect
for i in range(n):
	(low, high) = map(int, input().split())
	l = bisect.bisect_left(D, low)
	r = bisect.bisect_right(D, high)
	if l != r:
		print(D[r - 1])
	else:
		print(-1)


import sys
import math

def read_line():
	return sys.stdin.readline()[:-1]

def read_int():
	return int(sys.stdin.readline())

def read_int_line():
	return [int(v) for v in sys.stdin.readline().split()]

def printDivisors(n, ls):
	i = 1
	while i <= math.sqrt(n):
		if n % i == 0:
			if n // i == i:
				ls.append(i)
			else:
				ls.append(i)
				ls.append(n // i)
		i = i + 1
	return ls

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def find(ls, l, h):
	if len(ls) == 0:
		return -1
	else:
		x = len(ls) // 2
		if l <= ls[x] <= h and x == len(ls) - 1:
			return ls[x]
		elif l <= ls[x] <= h:
			if ls[x + 1] > h:
				return ls[x]
			else:
				return find(ls[x:], l, h)
		elif ls[x] >= h:
			return find(ls[:x], l, h)
		else:
			return find(ls[x + 1:], l, h)
(a, b) = read_int_line()
g = gcd(a, b)
ls = []
ls = printDivisors(g, ls)
ls.sort()
n = read_int()
l = [0] * n
h = [0] * n
for i in range(n):
	(l[i], h[i]) = read_int_line()
for i in range(n):
	print(find(ls, l[i], h[i]))


def main():
	from math import gcd
	from bisect import bisect
	(a, b) = map(int, input().split())
	(divisors, g) = ([1], gcd(a, b))
	p = q = 1
	while not g % 2:
		g //= 2
		q *= 2
		divisors.append(q)
	while True:
		lim = int(g ** 0.5)
		if p >= lim:
			break
		for p in range(p + 2, lim + 2, 2):
			if not g % p:
				(l, q) = ([], 1)
				while not g % p:
					g //= p
					q *= p
					l.append(q)
				divisors += [p * q for p in l for q in divisors]
				break
		else:
			break
	if g != 1:
		divisors += [g * q for q in divisors]
	divisors.sort()
	res = []
	for _ in range(int(input())):
		(lo, hi) = map(int, input().split())
		d = divisors[bisect(divisors, hi) - 1]
		res.append(d if lo <= d else -1)
	print('\n'.join(map(str, res)))
main()


import bisect as bi
import math as mt
(a, b) = map(int, input().split())
n = int(input())
g = mt.gcd(a, b)
ans = []
i = 1
while i * i <= g:
	if not g % i:
		ans.append(i)
		ans.append(g // i)
	i += 1
ans.sort()
for i in range(n):
	(x, y) = map(int, input().split())
	p = bi.bisect(ans, y) - 1
	te = ans[p]
	if te < x:
		print(-1)
	else:
		print(te)


import math
import bisect

def calc_divisors(a, b):
	(a, b) = (a, b) if a < b else (b, a)
	divisors = []
	for i in range(1, int(math.sqrt(a)) + 1):
		(an, af) = divmod(a, i)
		(bn, bf) = divmod(b, i)
		if af == bf == 0:
			divisors.append(i)
		if b % an == af == 0:
			divisors.append(an)
	divisors.sort()
	return divisors
(a, b) = map(int, input().split())
common_divisors = calc_divisors(a, b)
size = int(input())
result = [0] * size
for i in range(size):
	(low, high) = map(int, input().split())
	ie = bisect.bisect_right(common_divisors, high)
	cd = common_divisors[ie - 1]
	result[i] = str(cd) if low <= cd <= high else '-1'
print('\n'.join(result))


import math

def bs(x, y, p):
	(l, r) = (0, len(p) - 1)
	z = -1
	while l <= r:
		m = (l + r) // 2
		if x <= p[m] <= y:
			z = p[m]
			l = m + 1
		elif p[m] > y:
			r = m - 1
		else:
			l = m + 1
	return z
(a, b) = map(int, input().split())
g = math.gcd(a, b)
s = int(math.sqrt(g)) + 1
p = []
for i in range(1, s):
	if not g % i:
		p.append(i)
l = len(p) - 1
l -= (s - 1) * (s - 1) == g
for i in range(l, -1, -1):
	p.append(g // p[i])
for _ in range(int(input())):
	(x, y) = map(int, input().split())
	print(bs(x, y, p))


import bisect

def divisors(a):
	(i, divs) = (1, set())
	while i * i <= a:
		if a % i == 0:
			divs.add(i)
			j = a // i
			if j != i and a % j == 0:
				divs.add(j)
		i += 1
	return divs
(a, b) = map(int, input().split())
divsa = divisors(a)
divsb = divisors(b)
divs = list(divsa.intersection(divsb))
divs.sort()
for _ in range(int(input())):
	(l, h) = map(int, input().split())
	pos = bisect.bisect_right(divs, h)
	if pos == 0 or divs[pos - 1] < l:
		print(-1)
	else:
		print(divs[pos - 1])


import os
import sys
from io import BytesIO, IOBase
import heapq as h
import bisect
from types import GeneratorType
BUFSIZE = 8192

class SortedList:

	def __init__(self, iterable=[], _load=200):
		values = sorted(iterable)
		self._len = _len = len(values)
		self._load = _load
		self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)]
		self._list_lens = [len(_list) for _list in _lists]
		self._mins = [_list[0] for _list in _lists]
		self._fen_tree = []
		self._rebuild = True

	def _fen_build(self):
		self._fen_tree[:] = self._list_lens
		_fen_tree = self._fen_tree
		for i in range(len(_fen_tree)):
			if i | i + 1 < len(_fen_tree):
				_fen_tree[i | i + 1] += _fen_tree[i]
		self._rebuild = False

	def _fen_update(self, index, value):
		if not self._rebuild:
			_fen_tree = self._fen_tree
			while index < len(_fen_tree):
				_fen_tree[index] += value
				index |= index + 1

	def _fen_query(self, end):
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		x = 0
		while end:
			x += _fen_tree[end - 1]
			end &= end - 1
		return x

	def _fen_findkth(self, k):
		_list_lens = self._list_lens
		if k < _list_lens[0]:
			return (0, k)
		if k >= self._len - _list_lens[-1]:
			return (len(_list_lens) - 1, k + _list_lens[-1] - self._len)
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		idx = -1
		for d in reversed(range(len(_fen_tree).bit_length())):
			right_idx = idx + (1 << d)
			if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]:
				idx = right_idx
				k -= _fen_tree[idx]
		return (idx + 1, k)

	def _delete(self, pos, idx):
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len -= 1
		self._fen_update(pos, -1)
		del _lists[pos][idx]
		_list_lens[pos] -= 1
		if _list_lens[pos]:
			_mins[pos] = _lists[pos][0]
		else:
			del _lists[pos]
			del _list_lens[pos]
			del _mins[pos]
			self._rebuild = True

	def _loc_left(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(lo, pos) = (-1, len(_lists) - 1)
		while lo + 1 < pos:
			mi = lo + pos >> 1
			if value <= _mins[mi]:
				pos = mi
			else:
				lo = mi
		if pos and value <= _lists[pos - 1][-1]:
			pos -= 1
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value <= _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def _loc_right(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(pos, hi) = (0, len(_lists))
		while pos + 1 < hi:
			mi = pos + hi >> 1
			if value < _mins[mi]:
				hi = mi
			else:
				pos = mi
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value < _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def add(self, value):
		_load = self._load
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len += 1
		if _lists:
			(pos, idx) = self._loc_right(value)
			self._fen_update(pos, 1)
			_list = _lists[pos]
			_list.insert(idx, value)
			_list_lens[pos] += 1
			_mins[pos] = _list[0]
			if _load + _load < len(_list):
				_lists.insert(pos + 1, _list[_load:])
				_list_lens.insert(pos + 1, len(_list) - _load)
				_mins.insert(pos + 1, _list[_load])
				_list_lens[pos] = _load
				del _list[_load:]
				self._rebuild = True
		else:
			_lists.append([value])
			_mins.append(value)
			_list_lens.append(1)
			self._rebuild = True

	def discard(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_right(value)
			if idx and _lists[pos][idx - 1] == value:
				self._delete(pos, idx - 1)

	def remove(self, value):
		_len = self._len
		self.discard(value)
		if _len == self._len:
			raise ValueError('{0!r} not in list'.format(value))

	def pop(self, index=-1):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		value = self._lists[pos][idx]
		self._delete(pos, idx)
		return value

	def bisect_left(self, value):
		(pos, idx) = self._loc_left(value)
		return self._fen_query(pos) + idx

	def bisect_right(self, value):
		(pos, idx) = self._loc_right(value)
		return self._fen_query(pos) + idx

	def count(self, value):
		return self.bisect_right(value) - self.bisect_left(value)

	def __len__(self):
		return self._len

	def __getitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		return self._lists[pos][idx]

	def __delitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		self._delete(pos, idx)

	def __contains__(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_left(value)
			return idx < len(_lists[pos]) and _lists[pos][idx] == value
		return False

	def __iter__(self):
		return (value for _list in self._lists for value in _list)

	def __reversed__(self):
		return (value for _list in reversed(self._lists) for value in reversed(_list))

	def __repr__(self):
		return 'SortedList({0})'.format(list(self))

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			self.os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
import sys
from math import ceil, log2
INT_MAX = sys.maxsize
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import collections as col
import math, string

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())

def minVal(x, y):
	return x if x < y else y

def getMid(s, e):
	return s + (e - s) // 2

def RMQUtil(st, ss, se, qs, qe, index):
	if qs <= ss and qe >= se:
		return st[index]
	if se < qs or ss > qe:
		return INT_MAX
	mid = getMid(ss, se)
	return minVal(RMQUtil(st, ss, mid, qs, qe, 2 * index + 1), RMQUtil(st, mid + 1, se, qs, qe, 2 * index + 2))

def RMQ(st, n, qs, qe):
	if qs < 0 or qe > n - 1 or qs > qe:
		print('Invalid Input')
		return -1
	return RMQUtil(st, 0, n - 1, qs, qe, 0)

def constructSTUtil(arr, ss, se, st, si):
	if ss == se:
		st[si] = arr[ss]
		return arr[ss]
	mid = getMid(ss, se)
	st[si] = minVal(constructSTUtil(arr, ss, mid, st, si * 2 + 1), constructSTUtil(arr, mid + 1, se, st, si * 2 + 2))
	return st[si]

def constructST(arr, n):
	x = int(ceil(log2(n)))
	max_size = 2 * int(2 ** x) - 1
	st = [0] * max_size
	constructSTUtil(arr, 0, n - 1, st, 0)
	return st
MOD = 10 ** 9 + 7
mod = 10 ** 9 + 7
t = 1
p = 10 ** 9 + 7

def ncr_util():
	inv[0] = inv[1] = 1
	fact[0] = fact[1] = 1
	for i in range(2, 300001):
		inv[i] = inv[i % p] * (p - p // i) % p
	for i in range(1, 300001):
		inv[i] = inv[i - 1] * inv[i] % p
		fact[i] = fact[i - 1] * i % p

def z_array(s1):
	n = len(s1)
	z = [0] * n
	(l, r, k) = (0, 0, 0)
	for i in range(1, n):
		if i > r:
			(l, r) = (i, i)
			while r < n and s1[r - l] == s1[r]:
				r += 1
			z[i] = r - l
			r -= 1
		else:
			k = i - l
			if z[k] < r - i + 1:
				z[i] = z[k]
			else:
				l = i
				while r < n and s1[r - l] == s1[r]:
					r += 1
				z[i] = r - l
				r -= 1
	return z

def primeFactors(n):
	d1 = {}
	while n % 2 == 0:
		d1[2] = d1.get(2, 0) + 1
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			d1[i] = d1.get(i, 0) + 1
			n = n // i
	if n > 2:
		d1[n] = d1.get(n, 0) + 1
	return d1

def solve():
	x = math.gcd(a, b)
	div = []
	i = 1
	while i * i <= x:
		if x % i == 0:
			if x // i == i:
				div.append(i)
			else:
				div.append(i)
				div.append(x // i)
		i += 1
	div.sort()
	for i in range(n):
		p1 = bisect.bisect_left(div, l[i][0])
		p2 = bisect.bisect_right(div, l[i][1])
		if p1 < p2 or p1 != p2:
			print(div[p2 - 1])
		else:
			print(-1)
for _ in range(t):
	(a, b) = map(int, input().split())
	n = int(input())
	l = []
	for i in range(n):
		l.append(list(map(int, input().split())))
	solve()


import sys

def fastio():
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
INF = 10 ** 20
MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
from math import gcd
from math import ceil
from collections import defaultdict as dd, Counter
from bisect import bisect_left as bl, bisect_right as br
(a, b) = I()
(q,) = I()

def factors(x):
	ans = []
	for i in range(1, int(x ** 0.5) + 1):
		if x % i:
			continue
		ans.append(i)
		ans.append(x // i)
	return set(ans)
a = factors(a)
b = factors(b)
while q:
	q -= 1
	(l, r) = I()
	ans = -1
	for i in a:
		if i in b and l <= i <= r:
			ans = max(ans, i)
	print(ans)


import sys
from array import array
from math import gcd
from bisect import bisect_right

def input():
	return sys.stdin.buffer.readline().decode('utf-8')
(a, b) = map(int, input().split())
g = gcd(a, b)
divisors = [0]
d = 1
while d * d <= g:
	if g % d == 0:
		divisors.append(d)
		if d != g // d:
			divisors.append(g // d)
	d += 1
divisors.sort()
n = int(input())
ans = [-1] * n
for (i, (low, high)) in enumerate((map(int, input().split()) for _ in range(n))):
	j = bisect_right(divisors, high)
	if low <= divisors[j - 1] <= high:
		ans[i] = divisors[j - 1]
print(*ans, sep='\n')


from math import gcd
from bisect import bisect
from functools import reduce

def factors(n):
	k = sorted(list(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5 + 1)) if n % i == 0)))))
	return k
(a, b) = map(int, input().split())
gg = gcd(a, b)
f = factors(gg)
n = int(input())
for _ in range(0, n):
	(l, h) = map(int, input().split())
	ss = bisect(f, h)
	sss = f[ss - 1]
	if sss < l:
		print(-1)
	else:
		print(sss)


import math
import bisect

def factors(n):
	fac = set()
	fac.add(1)
	fac.add(n)
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			fac.add(i)
			fac.add(n // i)
	return sorted(list(fac))
(a, b) = map(int, input().split())
g = math.gcd(a, b)
fac = factors(g)
n = int(input())
for i in range(n):
	(l, r) = map(int, input().split())
	ans = -1
	for i in range(0, len(fac)):
		if fac[i] >= l and fac[i] <= r:
			ans = max(ans, fac[i])
		if fac[i] > r:
			break
	print(ans)


import sys, math as mt
import heapq as hp
import collections as cc
import math as mt
import itertools as it
import bisect as bi
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
sys.setrecursionlimit(8 * 10 ** 5)
(a, b) = I()
g = mt.gcd(a, b)
div = set()
for i in range(2, int(g ** 0.5) + 1):
	if g % i == 0:
		div.add(i)
		div.add(g // i)
div = [1] + list(div) + [g]
div.sort()
for tc in range(int(input())):
	(x, y) = I()
	tf = 0
	temp = bi.bisect_right(div, y)
	if x <= div[temp - 1] and div[temp - 1] <= y:
		print(div[temp - 1])
	else:
		print(-1)


from math import gcd
(x, y) = map(int, input().split())
g = gcd(x, y)
div = []
for i in range(1, int(g ** 0.5) + 1):
	if g % i == 0:
		if i * i == g:
			div.append(i)
			break
		else:
			div.append(i)
			div.append(g // i)
div.sort()
for q in range(int(input())):
	ans = -1
	(l, r) = map(int, input().split())
	(lo, hi) = (0, len(div) - 1)
	while lo <= hi:
		mid = (lo + hi) // 2
		if div[mid] > r:
			hi = mid - 1
		elif div[mid] < l:
			lo = mid + 1
		else:
			ans = div[mid]
			lo = mid + 1
	print(ans)


import math
from bisect import bisect_right, bisect_left
from collections import Counter
from heapq import heappop, heappush
from itertools import accumulate
R = lambda : map(int, input().split())
(mi, mx) = R()
g = math.gcd(mi, mx)
res = []
for x in range(1, int(math.sqrt(g)) + 1):
	if g % x == 0:
		res.append(x)
		res.append(g // x)
res.sort()
for x in range(int(input())):
	(l, r) = R()
	i = bisect_right(res, r) - 1
	if l <= res[i] <= r:
		print(res[i])
	else:
		print(-1)


from math import sqrt, gcd
import bisect

def commDiv(a, b):
	n = gcd(a, b)
	result = []
	for i in range(1, int(sqrt(n)) + 1):
		if n % i == 0:
			if n / i == i:
				result.append(i)
			else:
				result.append(i)
				result.append(n // i)
	return sorted(result)
(a, b) = map(int, input().split())
res = commDiv(a, b)
q = int(input())
for _ in range(q):
	(l, r) = map(int, input().split())
	upper_index = bisect.bisect_right(res, r) - 1
	if upper_index == -1 or res[upper_index] < l:
		print(-1)
	else:
		print(res[upper_index])


import sys
import math
from collections import defaultdict
import bisect

def alele():
	return list(map(int, sys.stdin.readline().strip().split()))

def ilele():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 1000000000 + 7

def printDivisors(n):
	A = []
	i = 1
	while i <= math.sqrt(n):
		if n % i == 0:
			if n / i == i:
				A.append(i)
			else:
				A.append(i)
				A.append(n // i)
		i = i + 1
	return A
(a, b) = ilele()
c = min(a, b)
d = max(a, b)
A = printDivisors(c)
B = []
for i in A:
	if d % i == 0:
		B.append(i)
B.sort()
for _ in range(int(input())):
	(a, b) = ilele()
	x = bisect.bisect_left(B, b)
	if x < len(B) and B[x] == b:
		print(b)
	elif x == 0:
		print(-1)
	elif B[x - 1] >= a:
		print(B[x - 1])
	else:
		print(-1)


import bisect
(a, b) = map(int, input().split())

def d(n):
	q = int(n ** 0.5) + 1
	r = {1}
	for i in range(1, q):
		if n % i == 0:
			r.add(i)
			r.add(n // i)
	return r
q = sorted(d(a) & d(b))
w = set(q)
for i in range(int(input())):
	k = -1
	(l, h) = map(int, input().split())
	o = bisect.bisect_right(q, h)
	if o - bisect.bisect_left(q, l) or h in w or l in w > 1:
		k = q[o - 1]
	print(k)


import math, bisect

def factors(n):
	facts = set()
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			facts.add(i)
			facts.add(n // i)
	facts = sorted(list(facts))
	return facts

def check(index, low, facts):
	if facts[index] < low:
		return False
	return True

def solve(facts, low, high, ans):
	index = bisect.bisect(facts, low)
	if index > 0 and facts[index - 1] == low:
		index -= 1
	if index == len(facts):
		ans.append(-1)
		return
	index2 = bisect.bisect(facts, high) - 1
	if low <= facts[index2] <= high:
		ans.append(facts[index2])
	else:
		ans.append(-1)

def main():
	(a, b) = map(int, input().split())
	g = math.gcd(max(a, b), min(a, b))
	facts = factors(g)
	q = int(input())
	ans = []
	for i in range(q):
		(low, high) = map(int, input().split())
		solve(facts, low, high, ans)
	for i in ans:
		print(i)
main()


from sys import stdin, stdout
import bisect as bs
nmbr = lambda : int(stdin.readline())
lst = lambda : list(map(int, input().split()))
for i in range(1):
	(n1, n2) = lst()
	d = {}
	p = 1
	while p * p <= n1:
		if n1 % p == 0:
			if p == n1 // p:
				d[p] = d.get(p, 0) + 1
			else:
				d[p] = d.get(p, 0) + 1
				d[n1 // p] = d.get(n1 // p, 0) + 1
		p += 1
	p = 1
	while p * p <= n2:
		if n2 % p == 0:
			if p == n2 // p:
				d[p] = d.get(p, 0) - 1
			else:
				d[p] = d.get(p, 0) - 1
				d[n2 // p] = d.get(n2 // p, 0) - 1
		p += 1
	a = []
	for (k, v) in d.items():
		if v == 0:
			a += [k]
	a.sort()
	ln = len(a)
	for i in range(nmbr()):
		(l, r) = lst()
		p1 = bs.bisect_left(a, l)
		p2 = bs.bisect_left(a, r)
		if p2 >= ln or a[p2] != r:
			p2 -= 1
		if p1 <= p2:
			print(a[p2])
		else:
			print(-1)


import math
(a, b) = [int(i) for i in input().strip().split(' ')]
d = math.gcd(a, b)
divs = []
for i in range(1, int(math.sqrt(d)) + 1):
	if d % i == 0:
		divs.append(i)
k = len(divs)
divisors = divs[:]
for j in range(1, k + 1):
	divisors.append(d // divs[-j])
n = len(divisors)
q = int(input())
for j in range(q):
	(low, high) = [int(i) for i in input().strip().split(' ')]
	left = 0
	right = n - 1
	while left < right:
		mid = left + (right - left) // 2
		if divisors[mid] >= low:
			right = mid
		else:
			left = mid + 1
	if divisors[left] < low:
		d1 = -1
	else:
		d1 = divisors[left]
	left = 0
	right = n - 1
	while left < right:
		mid = left + (right - left + 1) // 2
		if divisors[mid] <= high:
			left = mid
		else:
			right = mid - 1
	if divisors[left] > high:
		d2 = -1
	else:
		d2 = divisors[left]
	if d1 < 0 or d2 < 0 or d1 > d2:
		ans = -1
	else:
		ans = d2
	print(ans)


from math import sqrt
from bisect import bisect_right

def gcd(a, b):
	if a:
		return gcd(b % a, a)
	else:
		return b
(a, b) = map(int, input().split())
d = gcd(a, b)
(u, v) = ([1], [d])
for i in range(2, int(sqrt(d)) + 1):
	if d % i == 0:
		u.append(i)
		v.append(d // i)
u += reversed(v)
for i in range(int(input())):
	(l, r) = map(int, input().split())
	k = u[bisect_right(u, r) - 1]
	print(-1 if l > k else k)


from sys import stdin
from bisect import bisect_left, bisect_right

def gcd(a, b):
	if a > b:
		(a, b) = (b, a)
	while a:
		(a, b) = (b % a, a)
	return b
(a, b) = map(int, stdin.readline().split())
g = gcd(a, b)
gfactor = [1, g]
p = 2
while p * p <= g:
	if g % p == 0:
		gfactor.append(p)
		if g // p != p:
			gfactor.append(g // p)
	p += 1
gfactor = sorted(gfactor)
n = int(stdin.readline().strip())
for i in range(n):
	(low, high) = map(int, stdin.readline().split())
	ridx = bisect_right(gfactor, high) - 1
	if gfactor[ridx] >= low:
		print(gfactor[ridx])
	else:
		print(-1)


import math
import bisect

def calc_divisors(num):
	divisors = set()
	for i in range(1, int(math.sqrt(num)) + 1):
		(n, f) = divmod(num, i)
		if f == 0:
			divisors.add(i)
			divisors.add(n)
	return divisors
(a, b) = map(int, input().split())
common_divisors = [n for n in sorted(calc_divisors(a) & calc_divisors(b))]
for _ in range(int(input())):
	(low, high) = map(int, input().split())
	ie = bisect.bisect_right(common_divisors, high)
	cd = common_divisors[ie - 1]
	if not low <= cd <= high:
		cd = -1
	print(cd)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
mod = int(pow(10, 9) + 7)
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def L():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
try:
	sys.stdin = open('input.txt', 'r')
except:
	pass
(a, b) = L()
sa = set()
sb = set()
sa.add(a)
sb.add(b)
for i in range(2, int(a ** 0.5) + 2):
	if a % i == 0:
		sa.add(i)
		sa.add(a // i)
for i in range(2, int(b ** 0.5) + 2):
	if b % i == 0:
		sb.add(i)
		sb.add(b // i)
s = sa.intersection(sb)
s.add(1)
s = sorted(list(s))
q = L()[0]
n = len(s)
for i in range(q):
	(a, b) = L()
	l = bl(s, a)
	r = br(s, b)
	if l > r:
		print(-1)
		continue
	if l == r:
		if l < len(s) and l >= 0 and (s[l] >= a) and (s[l] <= b):
			print(s[l])
		else:
			print(-1)
		continue
	while r > n:
		r -= 1
	if l <= r:
		print(s[r - 1])
	else:
		print(-1)


import math
import bisect

def gcd(a_, b_):
	if b_ == 0:
		return a_
	else:
		return gcd(b_, a_ % b_)
(a, b) = map(int, input().split())
_gcd = gcd(a, b)
n = int(input())
M = []
for i in range(1, int(math.sqrt(_gcd)) + 1):
	if not _gcd % i:
		M.append(i)
		M.append(_gcd // i)
M = sorted(M)
for i in range(n):
	(a, b) = map(int, input().split())
	m = M[bisect.bisect_right(M, b) - 1]
	if m < a:
		print(-1)
	else:
		print(m)


import math

def pgcd(a, b):
	while b != 0:
		r = a % b
		a = b
		b = r
	return a
(a, b) = map(int, input().split())
g = pgcd(a, b)
gDiv = set()
for i in range(1, int(math.sqrt(g)) + 1):
	if g % i == 0:
		gDiv.add(i)
		gDiv.add(g // i)
ens = sorted(gDiv)
div = list(ens)
lenx = len(ens)
for i in range(int(input())):
	(a, b) = list(map(int, input().split(' ')))
	lo = 0
	hi = lenx - 1
	work = -1
	while lo <= hi:
		mid = (lo + hi) // 2
		if div[mid] < a:
			lo = mid + 1
		elif div[mid] > b:
			hi = mid - 1
		else:
			work = max(work, div[mid])
			lo = mid + 1
	print(work)


import math

def main():
	(a, b) = list(map(int, input().split()))
	if b > a:
		(a, b) = (b, a)
	while b != 0:
		(b, a) = (a % b, b)
	divisors = []
	root = math.floor(math.sqrt(a))
	for divisor in range(1, root):
		if a % divisor == 0:
			divisors.append(divisor)
			divisors.append(a // divisor)
	if a % root == 0:
		divisors.append(root)
		if root ** 2 != a:
			divisors.append(a // root)
	divisors.sort()
	divisors = [0] + divisors
	length = len(divisors)
	n = int(input())
	for _ in range(n):
		(low, high) = list(map(int, input().split()))
		left = 1
		right = length
		best = 0
		while left < right:
			middle = (left + right) // 2
			if divisors[middle] > high:
				right = middle
			else:
				best = divisors[middle]
				left = middle + 1
		if best < low:
			print('-1')
		else:
			print(best)
main()


import math
(a, b) = map(int, input().split())
n = int(input())
l = []
d = math.gcd(a, b)
for i in range(1, int(d ** (1 / 2) + 1)):
	if d % i == 0:
		l.append(i)
		if i * i != d:
			l.append(d // i)
for j in range(n):
	m = []
	(x, y) = map(int, input().split())
	r = -1
	for k in l:
		if k >= x and k <= y:
			r = max(r, k)
	print(r)


from sys import stdin
from bisect import bisect

def divisors(n):
	ans = set()
	i = 1
	while i * i <= n:
		if n % i == 0:
			ans.add(i)
		i += 1
	return ans | {n // e for e in ans}
test = stdin.readlines()
(a, b) = map(int, test[0].split())
n = int(test[1])
cdiv = sorted(divisors(a) & divisors(b))
m = len(cdiv)
out = []
for i in range(2, 2 + n):
	(l, r) = map(int, test[i].split())
	idx = bisect(cdiv, r) - 1
	out.append(cdiv[idx] if 0 <= idx < m and cdiv[idx] >= l else -1)
print('\n'.join(map(str, out)))


(a, b) = list(map(int, input().split()))
c = []
for i in range(1, int(a ** 0.5) + 1):
	if a % i == b % i == 0:
		c.append(i)
	if b % (a // i) == a % (a // i) == 0 and i != a // i:
		c.append(a // i)
c.sort()
d = len(c)
for i in range(int(input())):
	(e, h) = list(map(int, input().split()))
	(l, r) = (-1, d)
	while r - l - 1:
		m = (l + r) // 2
		if c[m] > h:
			r = m
		else:
			l = m
	print(c[r - 1] if h >= c[r - 1] >= e else -1)


from bisect import bisect_right
from math import gcd

def find_le(k, x):
	i = bisect_right(k, x)
	if i:
		return i - 1
(a, b) = map(int, input().split())
n = int(input())
g = gcd(a, b)
array = []
quantity = int(g ** 0.5)
for i in range(1, quantity + 1):
	if g % i == 0:
		array.append(i)
		array.append(g // i)
array.sort()
s = len(array)
for i in range(n):
	(l, r) = map(int, input().split())
	c = find_le(array, r)
	print(array[c] if array[c] >= l else -1)


def gcd(num1, num2):
	if num2 == 0:
		return num1
	return gcd(num2, num1 % num2)
(a, b) = map(int, input().split())
n = int(input())
k = gcd(a, b)
res1 = []
i = 1
while i * i <= k:
	if k % i == 0:
		res1.append(i)
		if i != k // i:
			res1.append(k // i)
	i += 1
for _ in range(n):
	(l, r) = map(int, input().split())
	ans = -1
	for d in res1:
		if l <= d <= r:
			ans = max(ans, d)
	print(ans)


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect, fractions, statistics
from collections import deque, defaultdict, OrderedDict, Counter
from fractions import Fraction
from decimal import Decimal
from sys import stdout
from heapq import heappush, heappop, heapify, _heapify_max, _heappop_max, nsmallest, nlargest
INF = 999999999999999999999999
alphabets = 'abcdefghijklmnopqrstuvwxyz'

class SortedList:

	def __init__(self, iterable=[], _load=200):
		values = sorted(iterable)
		self._len = _len = len(values)
		self._load = _load
		self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)]
		self._list_lens = [len(_list) for _list in _lists]
		self._mins = [_list[0] for _list in _lists]
		self._fen_tree = []
		self._rebuild = True

	def _fen_build(self):
		self._fen_tree[:] = self._list_lens
		_fen_tree = self._fen_tree
		for i in range(len(_fen_tree)):
			if i | i + 1 < len(_fen_tree):
				_fen_tree[i | i + 1] += _fen_tree[i]
		self._rebuild = False

	def _fen_update(self, index, value):
		if not self._rebuild:
			_fen_tree = self._fen_tree
			while index < len(_fen_tree):
				_fen_tree[index] += value
				index |= index + 1

	def _fen_query(self, end):
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		x = 0
		while end:
			x += _fen_tree[end - 1]
			end &= end - 1
		return x

	def _fen_findkth(self, k):
		_list_lens = self._list_lens
		if k < _list_lens[0]:
			return (0, k)
		if k >= self._len - _list_lens[-1]:
			return (len(_list_lens) - 1, k + _list_lens[-1] - self._len)
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		idx = -1
		for d in reversed(range(len(_fen_tree).bit_length())):
			right_idx = idx + (1 << d)
			if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]:
				idx = right_idx
				k -= _fen_tree[idx]
		return (idx + 1, k)

	def _delete(self, pos, idx):
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len -= 1
		self._fen_update(pos, -1)
		del _lists[pos][idx]
		_list_lens[pos] -= 1
		if _list_lens[pos]:
			_mins[pos] = _lists[pos][0]
		else:
			del _lists[pos]
			del _list_lens[pos]
			del _mins[pos]
			self._rebuild = True

	def _loc_left(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(lo, pos) = (-1, len(_lists) - 1)
		while lo + 1 < pos:
			mi = lo + pos >> 1
			if value <= _mins[mi]:
				pos = mi
			else:
				lo = mi
		if pos and value <= _lists[pos - 1][-1]:
			pos -= 1
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value <= _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def _loc_right(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(pos, hi) = (0, len(_lists))
		while pos + 1 < hi:
			mi = pos + hi >> 1
			if value < _mins[mi]:
				hi = mi
			else:
				pos = mi
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value < _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def add(self, value):
		_load = self._load
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len += 1
		if _lists:
			(pos, idx) = self._loc_right(value)
			self._fen_update(pos, 1)
			_list = _lists[pos]
			_list.insert(idx, value)
			_list_lens[pos] += 1
			_mins[pos] = _list[0]
			if _load + _load < len(_list):
				_lists.insert(pos + 1, _list[_load:])
				_list_lens.insert(pos + 1, len(_list) - _load)
				_mins.insert(pos + 1, _list[_load])
				_list_lens[pos] = _load
				del _list[_load:]
				self._rebuild = True
		else:
			_lists.append([value])
			_mins.append(value)
			_list_lens.append(1)
			self._rebuild = True

	def discard(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_right(value)
			if idx and _lists[pos][idx - 1] == value:
				self._delete(pos, idx - 1)

	def remove(self, value):
		_len = self._len
		self.discard(value)
		if _len == self._len:
			raise ValueError('{0!r} not in list'.format(value))

	def pop(self, index=-1):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		value = self._lists[pos][idx]
		self._delete(pos, idx)
		return value

	def bisect_left(self, value):
		(pos, idx) = self._loc_left(value)
		return self._fen_query(pos) + idx

	def bisect_right(self, value):
		(pos, idx) = self._loc_right(value)
		return self._fen_query(pos) + idx

	def count(self, value):
		return self.bisect_right(value) - self.bisect_left(value)

	def __len__(self):
		return self._len

	def __getitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		return self._lists[pos][idx]

	def __delitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		self._delete(pos, idx)

	def __contains__(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_left(value)
			return idx < len(_lists[pos]) and _lists[pos][idx] == value
		return False

	def __iter__(self):
		return (value for _list in self._lists for value in _list)

	def __reversed__(self):
		return (value for _list in reversed(self._lists) for value in reversed(_list))

	def __repr__(self):
		return 'SortedList({0})'.format(list(self))

class SegTree:

	def __init__(self, n):
		self.N = 1 << n.bit_length()
		self.tree = [0] * (self.N << 1)

	def update(self, i, j, v):
		i += self.N
		j += self.N
		while i <= j:
			if i % 2 == 1:
				self.tree[i] += v
			if j % 2 == 0:
				self.tree[j] += v
			(i, j) = (i + 1 >> 1, j - 1 >> 1)

	def query(self, i):
		v = 0
		i += self.N
		while i > 0:
			v += self.tree[i]
			i >>= 1
		return v

def SieveOfEratosthenes(limit):
	isPrime = [True] * (limit + 1)
	isPrime[0] = isPrime[1] = False
	primes = []
	for i in range(2, limit + 1):
		if not isPrime[i]:
			continue
		primes += [i]
		for j in range(i * i, limit + 1, i):
			isPrime[j] = False
	return primes

def main():
	mod = 1000000007
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	tc = 1
	for _ in range(tc):
		(a, b) = ria()
		q = ri()
		pfa = {}
		pfb = {}
		m = 10 ** 9
		i = 1
		while i * i <= m:
			if a % i == 0:
				pfa[i] = 1
				pfa[a // i] = 1
			if b % i == 0:
				pfb[i] = 1
				pfb[b // i] = 1
			i += 1
		common_divisors = []
		for i in pfa:
			if i in pfb:
				common_divisors.append(i)
		common_divisors = sorted(common_divisors)
		for i in range(q):
			(low, high) = ria()
			pos = bisect.bisect_right(common_divisors, high) - 1
			if common_divisors[pos] >= low:
				wi(common_divisors[pos])
			else:
				wi(-1)
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

	def flush(self):
		os.write(self._fd, self.buffer.getvalue())
		(self.buffer.truncate(0), self.buffer.seek(0))

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


import math
import bisect
(a, b) = map(int, input().split())
gcd = math.gcd(a, b)
(a, b) = (a, b) if a < b else (b, a)
divisors = []
for i in range(1, int(math.sqrt(a)) + 1):
	(n, f) = divmod(a, i)
	if f == 0:
		divisors.append(i)
		divisors.append(n)
divisors.sort()
for _ in range(int(input())):
	(low, high) = map(int, input().split())
	cd = -1
	if low > gcd:
		pass
	elif low <= gcd <= high:
		cd = gcd
	else:
		ib = bisect.bisect_left(divisors, low)
		ie = bisect.bisect_right(divisors, high)
		for i in range(ib, ie):
			d = divisors[i]
			if b % d == 0:
				cd = d
	print(cd)


from math import ceil, sqrt
import bisect
(a, b) = map(int, input().split())
li1 = set()
li2 = set()
for i in range(1, ceil(sqrt(a)) + 1):
	if a % i == 0:
		li1.add(i)
		li1.add(a // i)
for i in range(1, ceil(sqrt(b)) + 1):
	if b % i == 0:
		li2.add(i)
		li2.add(b // i)
li3 = li1 & li2
li3 = list(li3)
li3.sort()
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	ans = bisect.bisect_right(li3, b)
	if ans == 0:
		print(-1)
		continue
	if li3[ans - 1] <= b and li3[ans - 1] >= a:
		print(li3[ans - 1])
	else:
		print(-1)


from functools import reduce
import io, os, sys, math

def binarySearchkeeb(data, target, low, high):
	if low > high:
		return high
	else:
		mid = (low + high) // 2
		if data[mid] <= target:
			return binarySearchkeeb(data, target, mid + 1, high)
		else:
			return binarySearchkeeb(data, target, low, mid - 1)
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def gcd(x, y):
	return math.gcd(x, y)

def allFactors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))
(a, b) = map(int, input().split())
re = sorted(allFactors(gcd(a, b)))
for _ in range(int(input())):
	(l, h) = map(int, input().split())
	ans = binarySearchkeeb(re, h, 0, len(re) - 1)
	if ans == -1:
		print(-1)
	else:
		c = re[ans]
		if l <= c:
			print(c)
		else:
			print(-1)


from sys import stdin, stdout
cin = stdin.readline
cout = stdout.write
mp = lambda : list(map(int, cin().split()))

def chars():
	s = cin()
	return list(s[:len(s) - 1])

def pl(a):
	for val in a:
		cout(val + '\n')

def binSearch(lo, hi):
	high = len(l) - 1
	low = 0
	ans = -1
	while low <= high:
		mid = (low + high) // 2
		if hi >= l[mid] and l[mid] >= lo:
			ans = l[mid]
			low = mid + 1
		elif l[mid] < lo:
			low = mid + 1
		elif l[mid] > hi:
			high = mid - 1
	return ans
(a, b) = sorted(mp())
(n,) = mp()
s = set()
for i in range(1, int(a ** 0.5) + 1):
	if not a % i and (not b % i):
		s.add(i)
	if not a % (a // i) and (not b % (a // i)):
		s.add(a // i)
l = sorted(s)
for _ in range(n):
	(lo, hi) = mp()
	ans = binSearch(lo, hi)
	cout(str(ans) + '\n')


(n, m) = [int(i) for i in input().split()]
l = []
for i in range(1, int(n ** 0.5) + 1):
	if n % i == 0 and m % i == 0:
		l.append(i)
	if n % (n // i) == 0 and m % (n // i) == 0:
		l.append(n // i)
l.sort()
n = int(input())
for i in range(n):
	(q, w) = [int(j) for j in input().split()]
	mx = -1
	for j in range(len(l) - 1, -1, -1):
		if q <= l[j] <= w:
			mx = l[j]
			break
	print(mx)


import sys
import math
input = sys.stdin.readline

def inp():
	return int(input())

def inara():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
(a, b) = invr()
g = math.gcd(a, b)
fact = []
limit = int(math.sqrt(g + 1)) + 1
for i in range(1, limit):
	if g % i == 0:
		fact.append(i)
		if i * i != g:
			fact.append(g // i)
fact.sort()
q = inp()
n = len(fact)
for _ in range(q):
	(l, r) = invr()
	hi = n - 1
	lo = 0
	ans = -1
	while hi >= lo:
		mid = (hi + lo) // 2
		if fact[mid] >= l and fact[mid] <= r:
			ans = fact[mid]
			lo = mid + 1
		elif fact[mid] < l:
			lo = mid + 1
		else:
			hi = mid - 1
	print(ans)


from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from math import sqrt, floor, factorial, gcd, log
from collections import deque, Counter, defaultdict
from itertools import permutations
from math import gcd
from bisect import bisect
input = lambda : sys.stdin.readline().rstrip('\r\n')
read = lambda : list(map(int, input().strip().split(' ')))

def common_divs(a, b):
	set_a = set()
	set_b = set()
	for i in range(1, int(sqrt(a) + 1)):
		if a % i == 0:
			set_a.add(i)
			set_a.add(a // i)
	for i in range(1, int(sqrt(b) + 1)):
		if b % i == 0:
			set_b.add(i)
			set_b.add(b // i)
	return list(set_a.intersection(set_b))

def solve():
	(a, b) = read()
	divs = common_divs(a, b)
	divs.sort()
	for _ in range(int(input())):
		(l, h) = read()
		ind = bisect(divs, h) - 1
		if l <= divs[ind] <= h:
			print(divs[ind])
		else:
			print(-1)
solve()


from math import sqrt

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def binary_search(div, num):
	(start, end) = (0, len(div))
	while start < end:
		mid = int((end - start) / 2) + start
		if div[mid] > num:
			end = mid
		else:
			start = mid + 1
	return div[start - 1]
(a, b) = map(int, input().split())
curr_gcd = gcd(a, b)
div = []
for i in range(1, int(sqrt(curr_gcd)) + 1):
	if curr_gcd % i == 0:
		div.append(i)
		if curr_gcd / i != i:
			div.append(int(curr_gcd / i))
div.sort()
n = int(input())
for i in range(n):
	(low, hi) = map(int, input().split())
	res = binary_search(div, hi)
	print(-1 if low > res else res)


from sys import stdin
from math import gcd
from bisect import bisect
input = stdin.readline
(a, b) = map(int, input().split())
g = gcd(a, b)
f = []
for i in range(1, int(g ** 0.5) + 1):
	if g % i == 0:
		f.append(i)
		if g // i != i:
			f.append(g // i)
f.sort()
for _ in range(int(input())):
	(l, h) = map(int, input().split())
	if l <= g <= h:
		print(g)
		continue
	if l > g:
		print(-1)
		continue
	if h < g:
		i = bisect(f, h)
		if f[i - 1] >= l:
			print(f[i - 1])
		else:
			print(-1)


import bisect

def m_divlist(n):
	tmp = [-1]
	for x in range(1, int(n ** 0.5 + 1)):
		if n % x == 0:
			tmp.append(x)
			tmp.append(int(n / x))
	tmp.sort()
	return tmp
(a0, b0) = map(int, input().split(' '))
a = a0
b = b0
while a != 0 and b != 0:
	if a > b:
		a = a % b
	else:
		b = b % a
mcd = a + b
mdvlst = m_divlist(mcd)
n = int(input())
for i in range(n):
	(rmin, rmax) = map(int, input().split(' '))
	k = bisect.bisect(mdvlst, rmax)
	print(mdvlst[k - 1] if mdvlst[k - 1] >= rmin else '-1')


import sys, math as mt, bisect as bi
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
(a, b) = I()
(q,) = I()
p = mt.gcd(a, b)
an = [1, p]
i = 2
while i * i <= p:
	if p % i == 0:
		an.append(i)
		if i != p // i:
			an.append(p // i)
	i += 1
an.sort()
for _ in range(q):
	(l, r) = I()
	(x, y) = (bi.bisect(an, l), bi.bisect(an, r))
	(a, b) = (an[x - 1], an[y - 1])
	if l <= b <= r:
		print(b)
	else:
		print(-1)


import math
from collections import Counter

def read_int():
	return int(input().strip())

def read_ints():
	return list(map(int, input().strip().split(' ')))

def index(array, e):
	(low, high) = (0, len(array) - 1)
	while low < high:
		mid = (low + high + 1) // 2
		if e >= array[mid]:
			low = mid
		else:
			high = mid - 1
	return low

def solve():
	(a, b) = read_ints()
	gcd = math.gcd(a, b)
	(divisors, highs) = ([], [])
	i = 2
	for i in range(1, int(math.sqrt(gcd) + 1)):
		if gcd % i == 0:
			if i != gcd // i:
				divisors.append(i)
				highs.append(gcd // i)
			else:
				divisors.append(i)
	highs.reverse()
	divisors.extend(highs)
	n = read_int()
	for _ in range(n):
		(low, high) = read_ints()
		highest_divisor = divisors[index(divisors, high)]
		if highest_divisor >= low:
			print(highest_divisor)
		else:
			print(-1)
solve()


import os
import sys
from io import BytesIO, IOBase
sys.setrecursionlimit(10 ** 4)
from math import ceil, factorial, floor, gcd, log2, log10, sqrt
from collections import defaultdict as dd, Counter as cc, deque
from itertools import permutations as perm, combinations as comb
from heapq import heapify, heappush, heappushpop, nlargest, nsmallest
from bisect import bisect_left, bisect_right, bisect as bs
inpl = lambda : list(map(int, input().split()))
lstinp = lambda : list(input().split())
inpm = lambda : map(int, input().split())
inps = lambda : int(input())
inp = lambda : input()

def Divisors(n):
	l = []
	i = 2
	while i <= sqrt(n):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
		i = i + 1
	return l

def SieveOfEratosthenes(n):
	l = []
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	for p in range(2, n + 1):
		if prime[p]:
			l.append(p)
	return l

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(i)
			n = n / i
	if n > 2:
		l.append(n)
	return l

def Factors(n):
	result = []
	for i in range(2, int(sqrt(n)) + 1):
		if n % i == 0:
			if i == n / i:
				result.append(i)
			else:
				result.append(i)
				result.append(n // i)
	result.append(1)
	result.append(n)
	return result

def maxSubArraySum(a):
	max_so_far = 0
	max_ending_here = 0
	size = len(a)
	for i in range(0, size):
		max_ending_here = max_ending_here + a[i]
		if max_so_far < max_ending_here:
			max_so_far = max_ending_here
		if max_ending_here < 0:
			max_ending_here = 0
	return max_so_far

def longestsubarray(arr, n, k):
	current_count = 0
	max_count = 0
	for i in range(0, n, 1):
		if arr[i] % k != 0:
			current_count += 1
		else:
			current_count = 0
		max_count = max(current_count, max_count)
	return max_count

def main():
	(a, b) = inpm()
	gg = gcd(a, b)
	fact = sorted(Factors(gg))
	q = inps()
	for _ in range(q):
		(l, r) = inpm()
		if l > gg:
			print(-1)
			continue
		if r > gg:
			r = gg
		x = fact[bisect_right(fact, r) - 1]
		if x in range(l, r + 1):
			print(x)
		else:
			print(-1)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from math import sqrt, gcd
import bisect

def commDiv(a, b):
	n = gcd(a, b)
	result = []
	for i in range(1, int(sqrt(n)) + 1):
		if n % i == 0:
			if n / i == i:
				result.append(i)
			else:
				result.append(i)
				result.append(n // i)
	return sorted(result)
(a, b) = map(int, input().split())
res = commDiv(a, b)
q = int(input())
for _ in range(q):
	(l, r) = map(int, input().split())
	l_index = bisect.bisect_left(res, l)
	if 0 <= l_index < len(res) and res[l_index] < l:
		l_index += 1
	r_index = bisect.bisect_left(res, r)
	if 0 <= r_index < len(res) and res[r_index] > r:
		r_index -= 1
	if l_index > r_index:
		print(-1)
	elif not 0 <= r_index < len(res) and (not 0 <= l_index < len(res)):
		print(-1)
	elif r_index >= len(res):
		print(res[-1])
	elif 0 <= r_index < len(res):
		print(res[r_index])
	elif 0 <= l_index < len(res):
		print(res[l_index])
	else:
		print(-1)


import sys
import math
from bisect import bisect_right
input = sys.stdin.buffer.readline

def solution():
	(n, m) = map(int, input().split())
	x = math.gcd(n, m)
	l = []
	for i in range(1, int(x ** 0.5) + 1):
		if x % i == 0:
			if x // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(x // i)
	l.sort()
	for _ in range(int(input())):
		(a, b) = map(int, input().split())
		x = bisect_right(l, b)
		if l[x - 1] < a:
			print(-1)
		else:
			print(l[x - 1])
solution()


(a, b) = map(int, input().split())

def divisors(n):
	div = []
	for i in range(1, int(n ** (1 / 2)) + 1):
		if n % i == 0:
			div.append(i)
			if i * i != n:
				div.append(n // i)
	return sorted(div)
d = sorted(set(divisors(a)).intersection(divisors(b)))
for _ in range(int(input())):
	(l, r) = map(int, input().split())
	from bisect import bisect_left
	t = bisect_left(d, r)
	print(d[t] if t < len(d) and d[t] == r else d[t - 1] if d[t - 1] >= l else -1)


from math import *

def gcdmax(x, y, T, S):
	bottom = 0
	top = len(S) - 1
	output = -1
	while bottom <= top:
		mid = (bottom + top) // 2
		if T[mid] < x:
			bottom = mid + 1
		elif T[mid] > y:
			top = mid - 1
		else:
			output = max(output, T[mid])
			bottom = mid + 1
	return output

def main():
	(a, b) = map(int, input().split())
	n = int(input())
	G = list()
	g = gcd(a, b)
	for i in range(1, int(sqrt(g)) + 1):
		if g % i == 0:
			G.append(i)
			G.append(g // i)
	setG = set(G)
	G = list(setG)
	G.sort()
	for i in range(n):
		(x, y) = map(int, input().split())
		print(gcdmax(x, y, G, setG))
main()


(a, b) = map(int, input().split())
arr = []
arr_len = 0
cnt = 0
(a, b) = (min(a, b), max(a, b))
for x in range(1, int(a ** 0.5) + 1):
	if a % x == 0:
		arr.append(x)
		arr.append(a // x)
		arr_len += 2
for x in range(arr_len):
	if b % arr[x] != 0:
		arr[x] = -1
		cnt += 1
		arr_len -= 1
arr = list(set(sorted(arr)[cnt:]))
for i in range(int(input())):
	res = -1
	(l, r) = map(int, input().split())
	for d in arr:
		if d >= l and d <= r:
			res = max(res, d)
	print(res)


def R():
	return map(int, input().split())

def I():
	return int(input())

def S():
	return str(input())

def L():
	return list(R())
from collections import Counter
import math
import sys
from itertools import permutations
(a, b) = R()
import bisect
d = math.gcd(a, b)
i = 1
s = []
while i * i <= d:
	if d % i == 0:
		s.append(i)
		s.append(d // i)
	i += 1
s.sort()
for _ in range(I()):
	(l, h) = R()
	ind = bisect.bisect_right(s, h)
	ind -= 1
	if ind > len(s) or s[ind] > h or s[ind] < l:
		print('-1')
	else:
		print(s[ind])


from math import sqrt

def bin_ser(co_szuk):
	(start, koniec) = (0, len(dzielniki) - 1)
	while start < koniec:
		srodek = (koniec + start + 1) // 2
		if dzielniki[srodek] > co_szuk:
			koniec = srodek - 1
		else:
			start = srodek
	return dzielniki[start]

def nwd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a
(a, b) = map(int, input().split())
NWD = nwd(a, b)
dzielniki = []
for i in range(1, int(sqrt(NWD)) + 1):
	if NWD % i == 0:
		dzielniki.append(i)
		if i * i != NWD:
			dzielniki.append(NWD // i)
dzielniki.sort()
for t in range(int(input())):
	(low, high) = map(int, input().split())
	wynik = bin_ser(high)
	if wynik <= high and wynik >= low:
		print(wynik)
	else:
		print(-1)


(a, b) = map(int, input().split())
n = int(input())

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def bs(l, r, val, a):
	l1 = l
	r1 = r
	res = 0
	while l1 <= r1:
		mid = (l1 + r1) // 2
		if a[mid] <= val:
			l1 = mid + 1
			res = a[mid]
		else:
			r1 = mid - 1
	return res
t = gcd(a, b)
wish = []
for i in range(1, t + 1):
	if t // i < i:
		break
	if t % i == 0:
		wish.append(i)
		if t // i != i:
			wish.append(t // i)
wish.sort()
for i in range(0, n):
	(low, high) = map(int, input().split())
	temp = bs(0, len(wish) - 1, high, wish)
	if temp < low:
		temp = -1
	print(temp)


import math, bisect
(a, b) = map(int, input().split())
gcd = math.gcd(a, b)
arr = [i for i in range(1, int(math.sqrt(gcd)) + 1) if gcd % i == 0]
for i in arr[::-1]:
	arr.append(gcd // i)
for _ in range(int(input())):
	(l, h) = map(int, input().split())
	idx = bisect.bisect(arr, h) - 1
	if l > arr[idx]:
		print(-1)
	else:
		print(arr[idx])


import math
import bisect

def calc_divisors(a, b):
	(a, b) = (a, b) if a < b else (b, a)
	divisors = []
	for i in range(1, int(math.sqrt(a)) + 1):
		(an, af) = divmod(a, i)
		(bn, bf) = divmod(b, i)
		if af == bf == 0:
			divisors.append(i)
		if b % an == af == 0:
			divisors.append(an)
	divisors.sort()
	return divisors
(a, b) = map(int, input().split())
common_divisors = calc_divisors(a, b)
for _ in range(int(input())):
	(low, high) = map(int, input().split())
	ie = bisect.bisect_right(common_divisors, high)
	cd = common_divisors[ie - 1]
	if not low <= cd <= high:
		cd = -1
	print(cd)


from bisect import bisect_left as bl
import math

def factors(n):
	l = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			l.append(i)
			if i != n // i:
				l.append(n // i)
	return sorted(l)
(a, b) = map(int, input().split())
g = math.gcd(a, b)
l = factors(g)
for i in range(int(input())):
	(l1, h) = map(int, input().split())
	if l1 <= g <= h:
		ans = g
	elif h < g:
		x = bl(l, h)
		if x < len(l) and l[x] == h:
			ans = h
		elif l[x - 1] >= l1:
			ans = l[x - 1]
		else:
			ans = -1
	else:
		ans = -1
	print(ans)


from collections import Counter
from bisect import bisect_right as br
saved = {}

def gcd(a, b):
	return a if b == 0 else gcd(b, a % b)

def pf(num):
	i = 2
	ps = []
	while num > 1:
		if num % i == 0:
			num //= i
			ps.append(i)
		else:
			i += 1
	return ps

def f(num):
	i = 1
	res = []
	while i * i <= num:
		if num % i == 0:
			res.append(num // i)
			if i != num // i:
				res.append(i)
		i += 1
	return sorted(res)

def solve(c, x, y):
	for i in range(y, x - 1, -1):
		if i in saved and saved[i] != -1:
			return i
		if not Counter(pf(i)) - Counter(c):
			return i
		else:
			saved[i] = -1
	return -1

def solve1(c, x, y):
	res = c[br(c, y) - 1]
	return res if res >= x else -1
args = map(int, input().split())
(n1, n2) = args
g = f(gcd(n1, n2))
for _ in range(int(input())):
	(r1, r2) = map(int, input().split())
	print(solve1(g, r1, r2))


import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]

def divisors(n):
	div = set()
	for i in range(1, int(sqrt(n)) + 1):
		if n % i == 0:
			div.add(i)
			div.add(n // i)
	return div
(a, b) = value()
div = divisors(a).intersection(divisors(b))
div = sorted(div)
for _ in range(Int()):
	(low, high) = value()
	ans = bisect_right(div, high) - 1
	if div[ans] >= low:
		ans = div[ans]
	else:
		ans = -1
	print(ans)


import math
import sys

def gcd(a, b):
	while b > 0:
		rest = a % b
		a = b
		b = rest
	return a
dividers = []
(a, b) = tuple(map(int, input().split()))
cases = int(input())
g = gcd(a, b)
for x in range(1, int(math.sqrt(g) // 1) + 1):
	if g % x == 0:
		dividers.append(x)
		dividers.append(g // x)
dividers.sort()
dividers = list(dict.fromkeys(dividers))
for x in range(cases):
	(low, high) = tuple(map(int, input().split()))
	ans = 0
	if dividers[0] == high:
		print(dividers[0])
		continue
	if low > dividers[-1] or high < dividers[0]:
		print('-1')
		continue
	elif dividers[-1] <= high and dividers[-1] >= low:
		print(dividers[-1])
		continue
	start = 0
	end = len(dividers) - 1
	mid = (start + end + 1) // 2
	while start < end:
		mid = (start + end + 1) // 2
		if dividers[mid] > high:
			end = mid - 1
		else:
			start = mid
		if dividers[mid] <= high and dividers[mid + 1] > high:
			ans = dividers[start]
			break
	ans = dividers[start]
	if ans >= low and ans <= high:
		print(ans)
	else:
		print('-1')


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
from collections import defaultdict
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
(a, b) = list(map(int, input().split()))
cd = math.gcd(a, b)
ar = []
for i in range(1, int(cd ** 0.5) + 1):
	if cd % i == 0:
		ar.append(i)
		if i * i != cd:
			ar.append(cd // i)
for _ in range(int(input())):
	(l, h) = list(map(int, input().split()))
	res = -1
	for i in ar:
		if i >= l and i <= h:
			res = max(res, i)
	print(res)


import math
(a, b) = map(int, input().split())
g = math.gcd(a, b)
di = []
i = 1
while i * i <= g:
	if g % i == 0:
		di.append(i)
		if i * i != g:
			di.append(g // i)
	i += 1
di.sort()
m = len(di)
q = int(input())
for i in range(q):
	(x, y) = map(int, input().split())
	(l, r, p) = (0, m - 1, 0)
	while l <= r:
		mid = (l + r) // 2
		if di[mid] <= y:
			(p, l) = (mid, mid + 1)
		else:
			r = mid - 1
	if di[p] >= x:
		print(di[p])
	else:
		print(-1)


import sys
lines = sys.stdin.readlines()
(m, n) = map(int, lines[0].strip().split(' '))

def gcd(a, b):
	if a == 0:
		return b
	if a > b:
		return gcd(b, a)
	return gcd(b % a, a)
G = gcd(m, n)
tmp = G
factors = []
i = 2
while i ** 2 <= tmp:
	if tmp % i == 0:
		factors.append(i)
		tmp //= i
	else:
		i += 1
if tmp != 1:
	factors.append(tmp)
count = {}
for f in factors:
	if f not in count:
		count[f] = 0
	count[f] += 1
primes = list(count.keys())
primes.sort()
factors = [1]
for p in primes:
	tmp = []
	for f in factors:
		for i in range(1, count[p] + 1):
			tmp.append(f * p ** i)
	factors += tmp
factors.sort()
L = len(factors)
Q = int(lines[1].strip())
for q in range(2, Q + 2):
	(lo, hi) = map(int, lines[q].strip().split(' '))
	(l, r) = (0, L)
	while l < r - 1:
		mid = (r - l) // 2 + l
		if factors[mid] > hi:
			r = mid
		else:
			l = mid
	if factors[l] >= lo:
		print(factors[l])
	else:
		print(-1)


import sys
inf = float('inf')
mod = 1000000007

def get_array():
	return list(map(int, sys.stdin.readline().split()))

def get_ints():
	return map(int, sys.stdin.readline().split())

def input():
	return sys.stdin.readline()

def main():
	(a, b) = get_ints()
	from math import gcd, sqrt
	x = gcd(a, b)
	divisors = []
	i = 1
	while i <= sqrt(x):
		if x % i == 0:
			if x // i == i:
				divisors.append(i)
			else:
				divisors.append(i)
				divisors.append(x // i)
		i += 1
	divisors.sort()
	n = len(divisors)
	from bisect import bisect_left
	for tc in range(int(input())):
		(low, high) = get_ints()
		l = bisect_left(divisors, low)
		r = bisect_left(divisors, high)
		if l == n:
			print(-1)
			continue
		if r == n or (r < n and divisors[r] != high):
			r -= 1
		if r >= l:
			print(divisors[r])
		else:
			print(-1)
main()