#include <bits/stdc++.h>
using namespace std;
set<long long> v;
vector<long long> arr;
int main() {
  ios::sync_with_stdio(false);
  long long a, b, i, j, k, n, x, y, p, q;
  cin >> a >> b;
  n = min(a, b);
  for (i = 1; i <= sqrt(n); i++) {
    x = i;
    y = n / x;
    if (a % x == 0 && b % x == 0) v.insert(x);
    if (b % y == 0 && a % y == 0) v.insert(y);
  }
  for (auto i : v) {
    arr.push_back(i);
  }
  cin >> q;
  while (q--) {
    cin >> x >> y;
    i = lower_bound(arr.begin(), arr.end(), y) - arr.begin();
    if (i == arr.size()) {
      i--;
      if (arr[i] >= x)
        cout << arr[i] << "\n";
      else
        cout << -1 << "\n";
    } else if (arr[i] == y) {
      cout << y << "\n";
    } else if (arr[i] != y) {
      i--;
      p = arr[i];
      if (p >= x) {
        cout << p << "\n";
      } else {
        cout << -1 << "\n";
      }
    }
  }
}
