#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int lcm(int a, int b) { return b * a / gcd(a, b); }
int x, y, z, ans[100099], anss, t, a, b, ll, rr, r, mid;
int main() {
  cin >> x >> y;
  z = gcd(x, y);
  for (int i = 1; i * i <= z; i++)
    if (z % i == 0) {
      ans[++ans[0]] = i;
      if (i * i != z) ans[++ans[0]] = z / i;
    }
  sort(ans + 1, ans + ans[0] + 1);
  cin >> t;
  while (t--) {
    scanf("%d%d", &a, &b);
    r = -1;
    ll = 1, rr = ans[0];
    while (ll <= rr) {
      mid = (ll + rr) / 2;
      if (ans[mid] <= b) {
        r = mid;
        ll = mid + 1;
      } else
        rr = mid - 1;
    }
    if (a <= ans[r])
      printf("%d\n", ans[r]);
    else
      printf("-1\n");
  }
  return 0;
}
