#include <bits/stdc++.h>
using namespace std;
long long MOD = 1e9 + 7;
const int OO = (int)1e9 + 7;
const int MAX = 3009;
char c1, c2, c3;
string s, s1, s2, s3;
long long n, m, k, t, a, b, c, d, e, f, x, y, z, sol, ans = 0;
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
vector<long long> v;
vector<long long> v2;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> a >> b;
  cin >> n;
  z = gcd(a, b);
  for (int i = 1; i * i <= z; i++) {
    if (z % i == 0 && a % i == 0 && b % i == 0) {
      v.push_back(i);
      v2.push_back(z / i);
    }
  }
  for (int i = (int)v2.size() - 1; i >= 0; i--) v.push_back(v2[i]);
  while (n--) {
    cin >> x >> y;
    sol = -1;
    if (x > z) {
      cout << -1 << endl;
      continue;
    }
    int i = upper_bound((v).begin(), (v).end(), y) - v.begin();
    for (int i = (int)v.size() - 1; i >= 0; i--) {
      if (v[i] >= x && v[i] <= y) {
        sol = v[i];
        break;
      }
      if (v[i] < x) break;
    }
    cout << sol << endl;
  }
  return 0;
}
