#include <bits/stdc++.h>
using namespace std;
int main() {
  unsigned long a, b, h, l, c, d;
  int n;
  cin >> a >> b >> n;
  unsigned long t;
  while (b != 0) {
    t = b;
    b = a % b;
    a = t;
  }
  while (n--) {
    cin >> l >> h;
    if (t <= h) {
      if (t < l) {
        cout << "-1" << endl;
      } else {
        cout << t << endl;
      }
    } else {
      if (t % h == 0) {
        c = t / h;
      } else {
        c = t / h + 1;
      }
      d = t / l;
      while (t % c != 0 && c <= d) {
        c++;
      }
      if (c <= d) {
        cout << t / c << "\n";
      } else
        cout << "-1" << endl;
    }
  }
}
