#include <bits/stdc++.h>
using namespace std;
const int maxn = 40000 + 10;
int a, b, n, gcd, low, hig;
int arr[maxn * 2], cnt;
int Gcd(int a, int b) {
  if (b == 0) return a;
  return Gcd(b, a % b);
}
void GetYue(int tar) {
  for (int i = 1; i * i <= tar; i++) {
    if (tar % i == 0) {
      arr[cnt++] = i;
      if (tar / i != i) arr[cnt++] = tar / i;
    }
  }
  sort(arr, arr + cnt);
}
int Query(int lo, int hi) {
  int p = upper_bound(arr, arr + cnt, hi) - arr - 1;
  return arr[p] >= lo ? arr[p] : -1;
}
int main() {
  while (scanf("%d%d", &a, &b) != EOF) {
    scanf("%d", &n);
    gcd = Gcd(a, b);
    GetYue(gcd);
    for (int i = 0; i < n; i++) {
      scanf("%d%d", &low, &hig);
      printf("%d\n", Query(low, hig));
    }
  }
  return 0;
}
