#include <bits/stdc++.h>
using namespace std;
long long int mod = 998244353;
long long gcd(long long int a, long long int b) {
  if (b == 0) return a;
  return (b, a % b);
}
int main() {
  int a, b;
  scanf("%d%d", &a, &b);
  int gcd_a_b = gcd(a, b);
  vector<int> divisors;
  for (int i = 1; i * i <= gcd_a_b; i++) {
    if (gcd_a_b % i == 0) {
      divisors.push_back(i);
      if (i * i != gcd_a_b) {
        divisors.push_back(gcd_a_b / i);
      }
    }
  }
  sort(divisors.begin(), divisors.end());
  int n = divisors.size();
  int q;
  scanf("%d", &q);
  while (q--) {
    int L, R;
    scanf("%d%d", &L, &R);
    int res = -1;
    int l = 0, r = n - 1;
    while (l <= r) {
      int mid = l + (r - l) / 2;
      if (divisors[mid] >= L && divisors[mid] <= R) {
        res = max(res, divisors[mid]);
        l = mid + 1;
      } else if (divisors[mid] < L) {
        l = mid + 1;
      } else {
        r = mid - 1;
      }
    }
    cout << res << "\n";
  }
}
