#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
int main(int argc, char const* argv[]) {
  int a, b;
  cin >> a >> b;
  int divisor = gcd(a, b);
  vector<int> v;
  for (int i = 1; i * i <= divisor; i++) {
    if (divisor % i == 0) {
      v.push_back(i);
      if (i * i != divisor) v.push_back(divisor / i);
    }
  }
  sort(v.begin(), v.end());
  int n;
  cin >> n;
  while (n--) {
    int low, high;
    cin >> low >> high;
    int i;
    for (i = v.size() - 1; i >= 0; i--) {
      if (v[i] >= low && v[i] <= high) {
        break;
      }
    }
    if (i >= 0)
      cout << v[i] << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}
