#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<long long int> answer;
  long long int a, b, n, j, low, high, k;
  cin >> a >> b;
  if (a > b) {
    swap(a, b);
  }
  cin >> n;
  for (j = 1; j <= int(sqrt(a)) + 1; j++) {
    if (a % j == 0) {
      if (b % j == 0) {
        answer.push_back(j);
      }
      if (b % (a / j) == 0) {
        answer.push_back(a / j);
      }
    }
  }
  sort(answer.begin(), answer.end());
  for (j = 1; j <= n; j++) {
    cin >> low >> high;
    k = (upper_bound(answer.begin(), answer.end(), high) - answer.begin());
    if (answer[k - 1] >= low) {
      cout << answer[k - 1] << endl;
    } else {
      cout << "-1" << endl;
    }
  }
}
