#include <bits/stdc++.h>
using namespace std;
map<int, bool> mymap;
set<int> myset;
int main() {
  ios_base::sync_with_stdio(0);
  int t, i, j;
  int a, b;
  cin >> a >> b;
  for (i = 1; i <= sqrt(a); i++) {
    if (a % i == 0) {
      mymap[i] = true;
      mymap[a / i] = true;
    }
  }
  for (i = 1; i <= sqrt(b); i++) {
    if (b % i == 0) {
      if (mymap.find(i) != mymap.end()) myset.insert(i);
      if (mymap.find(b / i) != mymap.end()) myset.insert(b / i);
    }
  }
  int n;
  cin >> n;
  while (n--) {
    int low, high;
    cin >> low >> high;
    auto it = myset.upper_bound(high);
    if (it == myset.begin()) {
      cout << "-1\n";
      continue;
    }
    --it;
    if (*it >= low)
      cout << *it << "\n";
    else
      cout << "-1\n";
  }
}
