#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b;
  cin >> a >> b;
  vector<int> cd;
  if (a > b) {
    a = a + b;
    b = a - b;
    a = a - b;
  }
  for (int i = 1; i * i <= a; i++) {
    if (a % i != 0) continue;
    if (b % i == 0) cd.push_back(i);
    if (b % (a / i) == 0) cd.push_back(a / i);
  }
  sort(cd.begin(), cd.end());
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int low, high, ans = -1;
    cin >> low >> high;
    for (int i = 0; i < cd.size(); i++)
      if (cd[i] > high)
        break;
      else if (cd[i] >= low)
        ans = cd[i];
    cout << ans << endl;
  }
  return 0;
}
