#include <bits/stdc++.h>
using namespace std;
int a, b, x, y, in, t, num[10006];
int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  cin >> a >> b;
  in = gcd(a, b);
  for (int i = 1; i <= sqrt(in); i++) {
    if (in % i) continue;
    num[++num[0]] = i;
    if (i * i != in) num[++num[0]] = in / i;
  }
  sort(num + 1, num + 1 + num[0]);
  cin >> t;
  while (t--) {
    cin >> x >> y;
    int l = 1, r = num[0], ans = -1, mid;
    while (l <= r) {
      mid = (l + r) / 2;
      if (num[mid] <= y) {
        l = mid + 1, ans = num[mid];
      } else
        r = mid - 1;
    }
    if (ans < x)
      cout << "-1" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}
