Problem: Well, here is another math class task. In mathematics, GCD is the greatest common divisor, and it's an easy task to calculate the GCD between two positive integers.

A common divisor for two positive numbers is a number which both numbers are divisible by.

But your teacher wants to give you a harder task, in this task you have to find the greatest common divisor d between two integers a and b that is in a given range from low to high (inclusive), i.e. low ≤ d ≤ high. It is possible that there is no common divisor in the given range.

You will be given the two integers a and b, then n queries. Each query is a range from low to high and you have to answer each query.

Input

The first line contains two integers a and b, the two integers as described above (1 ≤ a, b ≤ 109). The second line contains one integer n, the number of queries (1 ≤ n ≤ 104). Then n lines follow, each line contains one query consisting of two integers, low and high (1 ≤ low ≤ high ≤ 109).

Output

Print n lines. The i-th of them should contain the result of the i-th query in the input. If there is no common divisor in the given range for any query, you should print -1 as a result for this query.

Examples

Input

9 27
3
1 5
10 11
9 11


Output

3
-1
9

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T modpow(T base, T exp, T modulus) {
  base %= modulus;
  T result = 1;
  while (exp > 0) {
    if (exp & 1) result = (result * base) % modulus;
    base = (base * base) % modulus;
    exp >>= 1;
  }
  return result;
}
bool sortbysec(const pair<pair<long long, long long>, long long> &a,
               const pair<pair<long long, long long>, long long> &b) {
  if (a.first.first == b.first.first && a.first.second == b.first.second)
    return a.second > b.second;
  else if (a.first.second == b.first.second)
    return a.first.first > b.first.first;
  return a.first.second < b.first.second;
}
struct cmp {
  bool operator()(const pair<int, int> &a, const pair<int, int> &b) const {
    int lena = a.second - a.first + 1;
    int lenb = b.second - b.first + 1;
    if (lena == lenb) return a.first < b.first;
    return lena > lenb;
  }
};
struct c_h {
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    x ^= FIXED_RANDOM;
    return x ^ (x >> 16);
  }
};
long long calcpow(long long x, long long y) {
  long long temp;
  if (y == 0) return 1;
  temp = calcpow(x, y / 2);
  if (y % 2 == 0)
    return temp * temp;
  else {
    if (y > 0)
      return x * temp * temp;
    else
      return (temp * temp) / x;
  }
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long egcd(long long a, long long b, long long &x, long long &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  long long x1;
  long long y1;
  long long g = egcd(b, a % b, x1, y1);
  x = y1;
  y = x1 - y1 * (a / b);
  return g;
}
bool isprime(long long a) {
  bool flag = 1;
  for (long long i = 2; i * i <= a; i++) {
    if (a % i == 0) {
      flag = 0;
      break;
    }
  }
  return flag;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long a, b;
  cin >> a >> b;
  long long g = gcd(a, b);
  vector<long long> vec;
  vec.push_back(1);
  vec.push_back(g);
  for (long long i = 2; i * i <= g; i++) {
    if (g % i == 0) {
      vec.push_back(i);
      if (i != g / i) vec.push_back(g / i);
    }
  }
  long long n;
  cin >> n;
  sort(vec.begin(), vec.end());
  for (long long i = 0; i < n; i++) {
    long long l, r;
    cin >> l >> r;
    auto x = lower_bound(vec.begin(), vec.end(), r + 1);
    x--;
    if (x == (--vec.begin()) || (*x) < l)
      cout << "-1 \n";
    else
      cout << (*x) << endl;
  }
}