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 JAVA):

Solution:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

import static java.util.Collections.sort;

public class Main {
     static  ArrayList<Integer > store;
     static class Divisor{
         public int bs, pow;

         public Divisor(int bs, int pow) {
             this.bs = bs;
             this.pow = pow;
         }

         public Divisor() {
         }
     }
     static ArrayList<Divisor > getFactors(int n ){
          ArrayList<Divisor > arr = new ArrayList<>();
          for(int i = 2 ; i*i <= n ; i++  )
              if(n%i == 0){
               Divisor divisor = new Divisor();
               divisor.bs = i;
               int cnt = 0;
               while(n%i == 0){
                   cnt++;
                   n = n/i;
               }
               divisor.pow = cnt;
               arr.add(divisor);
          }
          if(n != 1 ){
               arr.add(new Divisor(n , 1));
          }
          return  arr;
     }
    static  void  getDivisors(int id  , ArrayList<Divisor > list, int res){
         if(id  == list.size()){
             store.add(res);
             return;
         }
         for(int i = 0 ; i <= list.get(id).pow ; i++ ){
               getDivisors(id+1, list, res);
             res = res*list.get(id).bs;
         }
    }
     static ArrayList<Integer > getCommonDivisor(int n , int m ){
         ArrayList<Divisor > divisor1 = getFactors(n);
         store = new ArrayList<Integer >();
         getDivisors(0 , divisor1 , 1);
         ArrayList<Integer > d1  = store;
         ArrayList<Divisor > divisor2 = getFactors(m);
         store = new ArrayList<Integer >();
         getDivisors(0 , divisor2 , 1);
         ArrayList<Integer > d2  = store;
         ArrayList<Integer > cd = new ArrayList<>();
         sort(d1);
         sort(d2);
        // System.out.println(d1 + "\n" + d2);
         int i1 = 0 , i2 = 0;
         while((i1 < d1.size()) && (i2 < d2.size())) {
             //System.out.println(d1.get(i1) + "\t" + d2.get(i2));
              if(d1.get(i1).equals(d2.get(i2))){
                  cd.add(d1.get(i1));
                  i1++;i2++;
               }else if(d1.get(i1)> d2.get(i2)){
                  i2++;
              }else{
                  i1++;
              }
         }
         return cd;
     }
     public static void main (String[] args) throws IOException {
          int a, b, n;
          BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
          StringTokenizer st = new StringTokenizer(buff.readLine());
          a = Integer.parseInt(st.nextToken());
          b = Integer.parseInt(st.nextToken());
          st = new StringTokenizer(buff.readLine());
          n = Integer.parseInt(st.nextToken());
          ArrayList<Integer > arr = getCommonDivisor(a, b);
          sort(arr);
          for(int i = 0 ; i < n ; i++ ){
              int l , r, ans = -1;
              st = new StringTokenizer(buff.readLine());
              l = Integer.parseInt(st.nextToken());
              r = Integer.parseInt(st.nextToken());
              int sta = 0 , en = arr.size()-1, mid;
              while(sta <= en){
                 mid = (sta + en)/2;
                 if(arr.get(mid) < l ){
                    sta = mid+1;
                 }else if(arr.get(mid) > r) {
                    en = mid-1;
                 }else{
                     sta = mid+1;
                     ans = arr.get(mid);
                 }
             }
             System.out.println(ans);
          }
     }
}