#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const long double PI = (3.1415926535897932384626433832795);
vector<long long int> v;
bool prime(long long int n) {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 == 0 || n % 3 == 0) return false;
  for (long long int i = 5; i * i <= n; i = i + 6) {
    if (n % i == 0 || n % (i + 2) == 0) {
      return false;
    }
  }
  return true;
}
long long int gcd(long long int a, long long int b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}
void factors(long long int n) {
  for (long long int i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      if (n / i == i)
        v.push_back(i);
      else {
        v.push_back(i);
        v.push_back(n / i);
      }
    }
  }
  sort(v.begin(), v.end());
}
long long int bs(vector<long long int> v, long long int l, long long int h,
                 long long int size) {
  long long int low = 0, high = size - 1;
  long long int ans = 0;
  while (low <= high) {
    long long int mid = (high + low) / 2;
    if (v[mid] >= l && v[mid] <= h) {
      ans = max(ans, v[mid]);
      low = mid + 1;
      if (v[mid] > h) break;
    } else if (v[mid] > h) {
      high = mid - 1;
    } else if (v[mid] < l) {
      low = mid + 1;
    }
  }
  if (ans != 0)
    return ans;
  else
    return -1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int x, y;
  cin >> x >> y;
  long long int r;
  r = gcd(x, y);
  factors(r);
  long long int size = v.size();
  int n;
  cin >> n;
  while (n--) {
    long long int l, h;
    cin >> l >> h;
    cout << bs(v, l, h, size) << '\n';
  }
  return 0;
}
