#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  while (b) {
    auto tmp = a;
    a = b;
    b = tmp % b;
  }
  return a;
}
void oneCase() {
  int a, b;
  cin >> a >> b;
  int n;
  cin >> n;
  auto d = gcd(a, b);
  set<int> div;
  for (int i = 1; i * i <= d; ++i) {
    if (d % i == 0) {
      div.insert(i);
      div.insert(d / i);
    }
  }
  while (n--) {
    int l, h;
    cin >> l >> h;
    auto p = div.upper_bound(h);
    if (p == div.begin()) {
      cout << "-1\n";
      continue;
    }
    --p;
    if (*p >= l) {
      cout << *p << "\n";
    } else {
      cout << "-1\n";
    }
  }
}
int main() {
  int t = 1;
  while (t--) {
    oneCase();
  }
  return 0;
}
