#include <bits/stdc++.h>
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
  int a, b, g, i, n, max;
  scanf("%d%d%d", &a, &b, &n);
  g = gcd(a, b);
  while (n-- > 0) {
    int low, high;
    scanf("%d%d", &low, &high);
    max = -1;
    for (i = 1; i * i <= g; i++) {
      if (g % i == 0 && i >= low && i <= high && max < g / i) max = i;
      if (g % i == 0 && g / i >= low && g / i <= high && max < g / i)
        max = g / i;
    }
    printf("%d\n", max);
  }
  return 0;
}
