def main():
A, B = map( int, input().split())
if A > B:
A, B = B, A
divisors = set()
div = 1
while div * div <= A:
if A % div == 0:
x = div
y = A // div
if B % x == 0:
divisors.add(x)
if B % y == 0:
divisors.add(y)
div = div + 1
Q = int(input())
divs = list(divisors)
for i in range(0, Q):
low, high = map( int, input().split())
s, e, mid, sol = 0, len(divs) - 1, 0, -1
while(s <= e):
mid = s + (e - s) // 2
if divs[mid] <= high:
sol = mid
s = mid + 1
else:
e = mid - 1
if(divs[sol] < low):
print(-1)
else:
print(divs[sol])
if __name__ == "__main__":
main()
ZGVmIG1haW4oKToKICAgIEEsIEIgPSBtYXAoIGludCwgaW5wdXQoKS5zcGxpdCgpKQogICAgaWYgQSA+IEI6CiAgICAgICAgQSwgQiA9IEIsIEEKICAgIGRpdmlzb3JzID0gc2V0KCkKICAgIGRpdiA9IDEKICAgIHdoaWxlIGRpdiAqIGRpdiA8PSBBOgogICAgICAgIGlmIEEgJSBkaXYgPT0gMDoKICAgICAgICAgICAgeCA9IGRpdgogICAgICAgICAgICB5ID0gQSAvLyBkaXYKICAgICAgICAgICAgaWYgQiAlIHggPT0gMDoKICAgICAgICAgICAgICAgIGRpdmlzb3JzLmFkZCh4KQogICAgICAgICAgICBpZiBCICUgeSA9PSAwOgogICAgICAgICAgICAgICAgZGl2aXNvcnMuYWRkKHkpCiAgICAgICAgZGl2ID0gZGl2ICsgMQogICAgUSA9IGludChpbnB1dCgpKQogICAgZGl2cyA9IGxpc3QoZGl2aXNvcnMpCiAgICBmb3IgaSBpbiByYW5nZSgwLCBRKToKICAgICAgICBsb3csIGhpZ2ggPSBtYXAoIGludCwgaW5wdXQoKS5zcGxpdCgpKQogICAgICAgIHMsIGUsIG1pZCwgc29sID0gMCwgbGVuKGRpdnMpIC0gMSwgMCwgLTEKICAgICAgICB3aGlsZShzIDw9IGUpOgogICAgICAgICAgICBtaWQgPSBzICsgKGUgLSBzKSAvLyAyCiAgICAgICAgICAgIGlmIGRpdnNbbWlkXSA8PSBoaWdoOgogICAgICAgICAgICAgICAgc29sID0gbWlkCiAgICAgICAgICAgICAgICBzID0gbWlkICsgMQogICAgICAgICAgICBlbHNlOgogICAgICAgICAgICAgICAgZSA9IG1pZCAtIDEKICAgICAgICBpZihkaXZzW3NvbF0gPCBsb3cpOgogICAgICAgICAgICBwcmludCgtMSkKICAgICAgICBlbHNlOgogICAgICAgICAgICBwcmludChkaXZzW3NvbF0pCgppZiBfX25hbWVfXyA9PSAiX19tYWluX18iOgogICAgbWFpbigp