#include <bits/stdc++.h>
using namespace std;
vector<int> g;
int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
int main(void) {
  int a, b;
  cin >> a >> b;
  int gc = gcd(a, b);
  for (int i = 1; i * i <= gc; ++i)
    if (!(gc % i)) {
      g.push_back(i);
      if (i * i != gc) g.push_back(gc / i);
    }
  sort(g.begin(), g.end());
  int n;
  cin >> n;
  while (n--) {
    cin >> a >> b;
    int ans = -1;
    for (vector<int>::iterator it = g.begin(); it != g.end(); ++it)
      if (a <= *it && *it <= b) ans = max(ans, *it);
    cout << ans << '\n';
  }
  return 0;
}
