#include <bits/stdc++.h>
using namespace std;
int pgcd(int a, int b) {
  if (b % a == 0) return a;
  pgcd(b % a, a);
}
int main() {
  int o = 0, gcd, az[1001], t[5000], j, k = 0, m, x, y, i, a, b, n;
  cin >> a >> b >> n;
  gcd = pgcd(a, b);
  for (i = 1; i * i <= gcd; i++) {
    if (gcd % i == 0) {
      t[k] = i;
      k++;
      t[k] = gcd / i;
      k++;
    }
  }
  for (i = 0; i < n; i++) {
    m = -1;
    cin >> x >> y;
    for (j = 0; j < k; j++) {
      if (t[j] <= y && t[j] >= x) m = max(m, t[j]);
    }
    cout << m << endl;
  }
}
