#include <bits/stdc++.h>
using namespace std;
set<int> S;
int a, b;
int q[7000000];
int cnt;
int l, r;
int t;
int main() {
  int i, j;
  cnt = 0;
  S.clear();
  cin >> a >> b;
  for (i = 1; i < 100000; i++) {
    if (a % i == 0) {
      if (b % i == 0) {
        S.insert(i);
      }
      j = a / i;
      if (b % j == 0) {
        S.insert(j);
      }
    }
  }
  set<int>::iterator it;
  for (it = S.begin(); it != S.end(); it++) q[cnt++] = *it;
  cin >> t;
  while (t--) {
    cin >> l >> r;
    int low = 0, high = cnt - 1;
    while (low + 1 < high) {
      int mid = (low + high) / 2;
      if (q[mid] <= r)
        low = mid;
      else
        high = mid - 1;
    }
    if (q[high] >= l && q[high] <= r)
      cout << q[high] << endl;
    else if (q[low] >= l && q[low] <= r)
      cout << q[low] << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}
