#include <bits/stdc++.h>
using namespace std;
unordered_map<long long, int> mp;
vector<long long> divis;
void divi(long long num) {
  for (long long i = 1; i * i <= num; ++i) {
    if (num % i == 0LL) {
      mp[i]++;
      if (mp[i] == 2) divis.emplace_back(i);
      if (num / i != i) {
        mp[num / i]++;
        if (mp[num / i] == 2) divis.emplace_back(num / i);
      }
    }
  }
}
int main() {
  long long a, b, lo, hi;
  scanf("%lld%lld", &a, &b);
  divi(a);
  divi(b);
  sort(divis.begin(), divis.end());
  int n;
  scanf("%d", &n);
  while (n--) {
    scanf("%lld%lld", &lo, &hi);
    int id = upper_bound(divis.begin(), divis.end(), hi) - divis.begin();
    id--;
    if (id < 0 || divis[id] < lo || divis[id] > hi)
      puts("-1");
    else
      printf("%I64d\n", divis[id]);
  }
  return 0;
}
