#include <bits/stdc++.h>
using namespace std;
void swap(int &a, int &b) {
  int t = a;
  a = b;
  b = t;
}
long long gcd(long long a, long long b) {
  if (a == 0 || b == 0) return max(a, b);
  if (b % a == 0) return a;
  return gcd(b % a, a);
}
vector<int> f1;
int bsearch(int a) {
  int low = 0, high = f1.size();
  while (low < high) {
    int mid = (low + high) / 2;
    if (f1[mid] <= a)
      low = mid + 1;
    else
      high = mid;
  }
  return low;
}
int main() {
  int a, b;
  cin >> a >> b;
  a = gcd(a, b);
  for (int i = 1; i * i <= a; i++)
    if (a % i == 0) {
      f1.push_back(i);
      f1.push_back(a / i);
    }
  std::sort((f1).begin(), (f1).end());
  f1.resize((int)(std::unique((f1).begin(), (f1).end()) - f1.begin()));
  int n;
  cin >> n;
  while (n--) {
    cin >> a >> b;
    int pos = bsearch(b);
    if (f1[pos - 1] >= a)
      printf("%d\n", f1[pos - 1]);
    else
      printf("-1\n");
  }
  return 0;
}
