#include <bits/stdc++.h>
using namespace std;
const int maxn = 20005;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
  long long a, b;
  vector<int> gc;
  while (cin >> a >> b) {
    int c = gcd(a, b);
    for (int i = 1; i * i <= c; i++) {
      if (c % i == 0) {
        gc.push_back(i);
        gc.push_back(c / i);
      }
    }
    sort(gc.begin(), gc.end());
    int n;
    cin >> n;
    while (n--) {
      int l, h;
      cin >> l >> h;
      int ans = upper_bound(gc.begin(), gc.end(), h) - gc.begin() - 1;
      if (gc[ans] < l)
        cout << -1 << endl;
      else
        cout << gc[ans] << endl;
    }
  }
  return 0;
}
