#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (!b) return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
bool isprime(long long n) {
  if (n == 0 || n == 1) return 0;
  for (long long i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      return 0;
    }
  }
  return 1;
}
int main() {
  int a, b, n;
  cin >> a >> b;
  set<int> s1, s2, s;
  for (int i = 1; i * i <= a; i++)
    if (a % i == 0) s1.insert(i), s1.insert(a / i);
  for (int i = 1; i * i <= b; i++)
    if (b % i == 0) s2.insert(i), s2.insert(b / i);
  set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(),
                   inserter(s, s.begin()));
  cin >> n;
  while (n--) {
    int k = -1, l, r;
    cin >> l >> r;
    for (auto it : s) {
      if (it <= r) {
        if (it >= l) k = it;
      } else
        break;
    }
    cout << k << '\n';
  }
  return 0;
}
