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.*;
import java.util.*;
import java.math.*;
import java.lang.*;
import static java.lang.Math.*;
 
public class fast implements Runnable {
    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;
        private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 
        public InputReader(InputStream stream) {
            this.stream = stream;
        }
        
        public int read() {
            if (numChars==-1) 
                throw new InputMismatchException();
            
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                }
                catch (IOException e) {
                    throw new InputMismatchException();
                }
                
                if(numChars <= 0)               
                    return -1;
            }
            return buf[curChar++];
        }
     
        public String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
        public int nextInt() {
            int c = read();
            
            while(isSpaceChar(c)) 
                c = read();
            
            int sgn = 1;
            
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            
            int res = 0;
            do {
                if(c<'0'||c>'9') 
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c)); 
            
            return res * sgn;
        }
        
        public long nextLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));
                return res * sgn;
        }
        
        public double nextDouble() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.') {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, nextInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.') {
                c = read();
                double m = 1;
                while (!isSpaceChar(c)) {
                    if (c == 'e' || c == 'E')
                        return res * Math.pow(10, nextInt());
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }
        
        public String readString() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } 
            while (!isSpaceChar(c));
            
            return res.toString();
        }
     
        public boolean isSpaceChar(int c) {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }
     
        public String next() {
            return readString();
        }
        
        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);
        }
    }
    
    class pair implements Comparable<pair>{
        int x,y;
        pair(int xi, int yi){
            x=xi;
            y=yi;
        }
        @Override
        public int compareTo(pair other){
            if(this.x>other.x){return 1;}
            if(this.x<other.x){return -1;}
            if(this.y>other.y){return 1;}
            if(this.y<other.y){return -1;}
            return 0;
        }    
    }
 
    class dist implements Comparable<dist>{
        int x,y,z;
        dist(int xi, int yi, int zi){
            x=xi;
            y=yi;
            z=zi;
        }
        @Override
        public int compareTo(dist other){
            if(this.z>other.z){return 1;}
            if(this.z<other.z){return -1;}
            return 0;
        } 
    } 
    
    public static void main(String args[]) throws Exception {
        new Thread(null, new fast(),"fast",1<<26).start();
    }
    
    public void sortbyColumn(int arr[][], final int col){ 
        // Using built-in sort function Arrays.sort 
        Arrays.sort(arr, new Comparator<int[]>() { 
            
          @Override              
          // Compare values according to columns 
          public int compare(final int[] entry1, final int[] entry2) { 
  
            // To sort in descending order revert  
            // the '>' Operator 
            if (entry1[col] > entry2[col]) 
                return 1; 
            if(entry1[col] < entry2[col])
                return -1;
            return 0;
          } 
        });  // End of function call sort(). 
    }
    
    public void sortbyColumn(long arr[][], final int col){ 
        // Using built-in sort function Arrays.sort 
        Arrays.sort(arr, new Comparator<long[]>() { 
            
          @Override              
          // Compare values according to columns 
          public int compare(final long[] entry1, final long[] entry2) { 
  
            // To sort in descending order revert  
            // the '>' Operator 
            if (entry1[col] > entry2[col]) 
                return 1; 
            if(entry1[col] < entry2[col])
                return -1;
            return 0;
          } 
        });  // End of function call sort(). 
    }
    
    public void sort(int ar[]){
        int n=ar.length;
        Integer arr[]=new Integer[n];
        for(int i=0;i<n;i++){
            arr[i]=ar[i];
        }
        Arrays.sort(arr); 
        for(int i=0;i<n;i++){
            ar[i]=arr[i].intValue();
        }
    }
    
    public void sort(long ar[]){
        int n=ar.length;
        Long arr[]=new Long[n];
        for(int i=0;i<n;i++){
            arr[i]=ar[i];
        }
        Arrays.sort(arr); 
        for(int i=0;i<n;i++){
            ar[i]=arr[i].longValue();
        }
    }
    
    long power(long x, long y, long p){  
        long res = 1;      
        x = x % p;  
        while (y > 0) 
        { 
            if((y & 1)==1) 
                res = (res * x) % p; 
            y = y >> 1;  
            x = (x * x) % p;  
        } 
        return res; 
    }
    
    long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);}
    int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);}

    int bins(ArrayList<Integer> a, int l, int r, int val){
        int mid;
        while(l<=r){
            mid=(l+r)>>1;
            if(a.get(mid)==val){
                return mid;
            }
            else if(a.get(mid)>val){
                r=mid-1;
            }
            else{
                l=mid+1;
            }
        }return r;
    }

    public void run(){
        InputReader s = new InputReader(System.in);
        PrintWriter w = new PrintWriter(System.out);
        
        int a=s.nextInt(),b=s.nextInt(),q=s.nextInt();
        TreeSet<Integer> fa=new TreeSet<>(),fb=new TreeSet<>();
        ArrayList<Integer> fac=new ArrayList<>();
        int sq=(int)sqrt(a);
        for(int i=1;i<=sq;i++){
            if(a%i==0){
                if(a/i!=i){
                    fa.add(a/i);
                }
                fa.add(i);
            }
        }sq=(int)sqrt(b);
        for(int i=1;i<=sq;i++){
            if(b%i==0){
                if(b/i!=i){
                    fb.add(b/i);
                }
                fb.add(i);
            }
        }
        for(int i:fa){
            if(fb.contains(i)){
                fac.add(i);
            }
        }
        while(q-->0){
            int x=s.nextInt(),y=s.nextInt(),z;
            z=bins(fac,0,fac.size()-1,y);
            w.println(z<0 || fac.get(z)<x?-1:fac.get(z));
        }

        w.close();
    }
}