#include <bits/stdc++.h>
using namespace std;
long long n, a, b;
vector<long long> v;
long long gcd(long long n, long long m) {
  if (!m) return n;
  return gcd(m, n % m);
}
void div_(long long x, long long y) {
  long long maxi = gcd(x, y);
  for (int i = 1; i * i <= maxi; i++) {
    if (x % i == 0 && y % i == 0) v.push_back(i), v.push_back(maxi / i);
  }
  sort(v.begin(), v.end());
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> a >> b;
  div_(a, b);
  cin >> n;
  while (n--) {
    cin >> a >> b;
    long long x = lower_bound(v.begin(), v.end(), b) - v.begin();
    if (x < v.size() && v[x] == b)
      cout << b << "\n";
    else {
      x--;
      if (x < v.size() && v[x] >= a && v[x] <= b)
        cout << v[x] << "\n";
      else
        cout << "-1\n";
    }
  }
  cin >> n;
  return 0;
}
