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;
const int MOD = 1e9 + 7;
const long double PI = (3.1415926535897932384626433832795);
vector<long long int> v;
bool prime(long long int n) {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 == 0 || n % 3 == 0) return false;
  for (long long int i = 5; i * i <= n; i = i + 6) {
    if (n % i == 0 || n % (i + 2) == 0) {
      return false;
    }
  }
  return true;
}
long long int gcd(long long int a, long long int b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}
void factors(long long int n) {
  for (long long int i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      if (n / i == i)
        v.push_back(i);
      else {
        v.push_back(i);
        v.push_back(n / i);
      }
    }
  }
  sort(v.begin(), v.end());
}
long long int bs(vector<long long int> v, long long int l, long long int h,
                 long long int size) {
  long long int low = 0, high = size - 1;
  long long int ans = 0;
  while (low <= high) {
    long long int mid = (high + low) / 2;
    if (v[mid] >= l && v[mid] <= h) {
      ans = max(ans, v[mid]);
      low = mid + 1;
      if (v[mid] > h) break;
    } else if (v[mid] > h) {
      high = mid - 1;
    } else if (v[mid] < l) {
      low = mid + 1;
    }
  }
  if (ans != 0)
    return ans;
  else
    return -1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int x, y;
  cin >> x >> y;
  long long int r;
  r = gcd(x, y);
  factors(r);
  long long int size = v.size();
  int n;
  cin >> n;
  while (n--) {
    long long int l, h;
    cin >> l >> h;
    cout << bs(v, l, h, size) << '\n';
  }
  return 0;
}