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;
struct debugger {
  static void call(string::iterator it, string::iterator ed) {}
  template <typename T, typename... aT>
  static void call(string::iterator it, string::iterator ed, T a, aT... rest) {
    string b;
    for (; *it != ','; ++it)
      if (*it != ' ') b += *it;
    cerr << b << " = " << a << " ";
    call(++it, ed, rest...);
  }
};
const double GOLD = (1.0 + sqrt(5.0)) / 2.0;
const double EPS = 1e-9;
const int INF = 0x7f7f7f7f;
const double PI = 3.141592653589793;
const double E = 2.718281828459045;
long long int expo(long long int a, long long int n) {
  long long int result = 1;
  while (n) {
    if (n % 2) result *= a;
    a *= a;
    n /= 2;
  }
  return result;
}
long long int mod_expo(long long int x, long long int y, long long int p) {
  long long int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
void __divisors(int a, vector<int> &v) {
  for (int i = 1; i <= sqrt(a); i++) {
    if (a % i == 0) {
      v.push_back(i);
      if (i != a / i) v.push_back(a / i);
    }
  }
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  int a, b;
  cin >> a >> b;
  if (a > b) swap(a, b);
  vector<int> left_side, right_side, divisors;
  __divisors(a, left_side);
  __divisors(b, right_side);
  unordered_map<int, bool> exist;
  for (int x : right_side) exist[x] = true;
  for (int x : left_side) {
    if (exist[x]) divisors.push_back(x);
  }
  sort(divisors.begin(), divisors.end());
  int q;
  cin >> q;
  while (q--) {
    int L, R;
    cin >> L >> R;
    int left =
        lower_bound(divisors.begin(), divisors.end(), L) - divisors.begin();
    int right =
        lower_bound(divisors.begin(), divisors.end(), R) - divisors.begin();
    if (right == divisors.size() || divisors[right] > R) right -= 1;
    int result_index = max(left, right);
    if (result_index >= 0 && result_index < divisors.size() &&
        divisors[result_index] >= L && divisors[result_index] <= R)
      cout << divisors[result_index] << endl;
    else
      cout << "-1\n";
  }
  cerr << "time = " << clock() << " ms" << '\n';
  return 0;
}