#include <bits/stdc++.h>
using namespace std;
long long a, b, n, x, y, cnt, p[100009];
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
int main() {
  cin >> a >> b >> n, a = gcd(max(a, b), min(a, b));
  for (long long i = 1; i <= sqrt(a); i += 1)
    if (!(a % i)) p[cnt++] = i, p[cnt++] = a / i;
  sort(p, p + cnt);
  for (long long i = 0; i < n; i += 1) {
    cin >> x >> y;
    long long qwq = p[upper_bound(p, p + cnt, y) - p - 1];
    cout << (qwq >= x ? qwq : -1) << endl;
  }
  return 0;
}
