import java.io.*;
import java.util.*;

import static java.lang.Math.*;
import static java.util.Arrays.*;

public class cf75c_2 {

    public static void main(String[] args) throws IOException {
        int a = rni(), b = ni();
        Map<Integer, Integer> fca = new HashMap<>(), fcb = new HashMap<>();
        for(int i = 2; i * i <= a; ++i) {
            while(a % i == 0) {
                fca.put(i, fca.getOrDefault(i, 0) + 1);
                a /= i;
            }
        }
        if(a > 1) {
            fca.put(a, 1);
        }
        for(int i = 2; i * i <= b; ++i) {
            while(b % i == 0) {
                fcb.put(i, fcb.getOrDefault(i, 0) + 1);
                b /= i;
            }
        }
        if(b > 1) {
            fcb.put(b, 1);
        }
        int cnt = 0;
        for(int k : fca.keySet()) {
            if(fcb.containsKey(k)) {
                ++cnt;
            }
        }
        int map[] = new int[cnt], fc[] = new int[cnt], ind = 0;
        for(int k : fca.keySet()) {
            if(fcb.containsKey(k)) {
                map[ind] = k;
                fc[ind++] = min(fca.get(k), fcb.get(k));
            }
        }
        List<Integer> div = new ArrayList<>();
        getDiv(map, fc, cnt, div, 0, 1);
        Collections.sort(div);
        cnt = div.size();
        // prln(map);
        // prln(fc);
        // prln(div);
        int n = ri();
        while(n --> 0) {
            int lo = rni(), hi = ni(), l = 0, r = cnt - 1, res = -1;
            while(l <= r) {
                int m = l + (r - l) / 2;
                if(div.get(m) <= hi) {
                    l = m + 1;
                    res = m;
                } else {
                    r = m - 1;
                }
            }
            prln(res == -1 || div.get(res) < lo ? -1 : div.get(res));
        }
        close();
    }

    static void getDiv(int[] map, int[] fc, int cnt, List<Integer> div, int cur, int x) {
        div.add(x);
        if(cur == cnt) {
            return;
        } else {
            getDiv(map, fc, cnt, div, cur + 1, x);
        }
        for(int i = 0; i < fc[cur]; ++i) {
            x *= map[cur];
            getDiv(map, fc, cnt, div, cur + 1, x);
        }
    }

    static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out));
    static StringTokenizer input;
    static Random rand = new Random();

    // references
    // IBIG = 1e9 + 7
    // IRAND ~= 3e8
    // IMAX ~= 2e10
    // LMAX ~= 9e18
    // constants
    static final int IBIG = 1000000007;
    static final int IRAND = 327859546;
    static final int IMAX = 2147483647;
    static final int IMIN = -2147483648;
    static final long LMAX = 9223372036854775807L;
    static final long LMIN = -9223372036854775808L;
    // util
    static int minof(int a, int b, int c) {return min(a, min(b, c));}
    static int minof(int... x) {return x.length == 1 ? x[0] : x.length == 2 ? min(x[0], x[1]) : min(x[0], minstarting(1, x));}
    static int minstarting(int offset, int... x) {assert x.length > 2; return offset == x.length - 2 ? min(x[offset], x[offset + 1]) : min(x[offset], minstarting(offset + 1, x));}
    static long minof(long a, long b, long c) {return min(a, min(b, c));}
    static long minof(long... x) {return x.length == 1 ? x[0] : x.length == 2 ? min(x[0], x[1]) : min(x[0], minstarting(1, x));}
    static long minstarting(int offset, long... x) {assert x.length > 2; return offset == x.length - 2 ? min(x[offset], x[offset + 1]) : min(x[offset], minstarting(offset + 1, x));}
    static int maxof(int a, int b, int c) {return max(a, max(b, c));}
    static int maxof(int... x) {return x.length == 1 ? x[0] : x.length == 2 ? max(x[0], x[1]) : max(x[0], maxstarting(1, x));}
    static int maxstarting(int offset, int... x) {assert x.length > 2; return offset == x.length - 2 ? max(x[offset], x[offset + 1]) : max(x[offset], maxstarting(offset + 1, x));}
    static long maxof(long a, long b, long c) {return max(a, max(b, c));}
    static long maxof(long... x) {return x.length == 1 ? x[0] : x.length == 2 ? max(x[0], x[1]) : max(x[0], maxstarting(1, x));}
    static long maxstarting(int offset, long... x) {assert x.length > 2; return offset == x.length - 2 ? max(x[offset], x[offset + 1]) : max(x[offset], maxstarting(offset + 1, x));}
    static int powi(int a, int b) {if(a == 0) return 0; int ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
    static long powl(long a, int b) {if(a == 0) return 0; long ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
    static int floori(double d) {return (int)d;}
    static int ceili(double d) {return (int)ceil(d);}
    static long floorl(double d) {return (long)d;}
    static long ceill(double d) {return (long)ceil(d);}
    static void shuffle(int[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
    static void shuffle(long[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
    static void shuffle(double[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
    static <T> void shuffle(T[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); T swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
    static void sort(int[] a) {shuffle(a); Arrays.sort(a);}
    static void sort(long[] a) {shuffle(a); Arrays.sort(a);}
    static void sort(double[] a) {shuffle(a); Arrays.sort(a);}
    static void qsort(int[] a) {Arrays.sort(a);}
    static void qsort(long[] a) {Arrays.sort(a);}
    static void qsort(double[] a) {Arrays.sort(a);}
    static int randInt(int min, int max) {return rand.nextInt(max - min + 1) + min;}
    // input
    static void r() throws IOException {input = new StringTokenizer(__in.readLine());}
    static int ri() throws IOException {return Integer.parseInt(__in.readLine());}
    static long rl() throws IOException {return Long.parseLong(__in.readLine());}
    static int[] ria(int n) throws IOException {int[] a = new int[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Integer.parseInt(input.nextToken()); return a;}
    static long[] rla(int n) throws IOException {long[] a = new long[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Long.parseLong(input.nextToken()); return a;}
    static char[] rcha() throws IOException {return __in.readLine().toCharArray();}
    static String rline() throws IOException {return __in.readLine();}
    static int rni() throws IOException {input = new StringTokenizer(__in.readLine()); return Integer.parseInt(input.nextToken());}
    static int ni() {return Integer.parseInt(input.nextToken());}
    static long rnl() throws IOException {input = new StringTokenizer(__in.readLine()); return Long.parseLong(input.nextToken());}
    static long nl() {return Long.parseLong(input.nextToken());}
    // output
    static void pr(int i) {__out.print(i);}
    static void prln(int i) {__out.println(i);}
    static void pr(long l) {__out.print(l);}
    static void prln(long l) {__out.println(l);}
    static void pr(double d) {__out.print(d);}
    static void prln(double d) {__out.println(d);}
    static void pr(char c) {__out.print(c);}
    static void prln(char c) {__out.println(c);}
    static void pr(char[] s) {__out.print(new String(s));}
    static void prln(char[] s) {__out.println(new String(s));}
    static void pr(String s) {__out.print(s);}
    static void prln(String s) {__out.println(s);}
    static void pr(Object o) {__out.print(o);}
    static void prln(Object o) {__out.println(o);}
    static void prln() {__out.println();}
    static void pryes() {__out.println("yes");}
    static void pry() {__out.println("Yes");}
    static void prY() {__out.println("YES");}
    static void prno() {__out.println("no");}
    static void prn() {__out.println("No");}
    static void prN() {__out.println("NO");}
    static void pryesno(boolean b) {__out.println(b ? "yes" : "no");};
    static void pryn(boolean b) {__out.println(b ? "Yes" : "No");}
    static void prYN(boolean b) {__out.println(b ? "YES" : "NO");}
    static void prln(int... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);}
    static void prln(long... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);}
    static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for(int i = 0; i < n; __out.print(iter.next()), __out.print(' '), ++i); if(n >= 0) __out.println(iter.next());}
    static void h() {__out.println("hlfd");}
    static void flush() {__out.flush();}
    static void close() {__out.close();}
}


import java.io.*;
import java.util.*;

import static java.lang.Math.*;
import static java.util.Arrays.*;

public class cf75c_2 {

    public static void main(String[] args) throws IOException {
        int a = rni(), b = ni();
        Map<Integer, Integer> fca = new HashMap<>(), fcb = new HashMap<>();
        for(int i = 2; i * i <= a; ++i) {
            while(a % i == 0) {
                fca.put(i, fca.getOrDefault(i, 0) + 1);
                a /= i;
            }
        }
        if(a > 1) {
            fca.put(a, 1);
        }
        for(int i = 2; i * i <= b; ++i) {
            while(b % i == 0) {
                fcb.put(i, fcb.getOrDefault(i, 0) + 1);
                b /= i;
            }
        }
        if(b > 1) {
            fcb.put(b, 1);
        }
        int cnt = 0;
        for(int k : fca.keySet()) {
            if(fcb.containsKey(k)) {
                ++cnt;
            }
        }
        int map[] = new int[cnt], fc[] = new int[cnt], ind = 0;
        for(int k : fca.keySet()) {
            if(fcb.containsKey(k)) {
                map[ind] = k;
                fc[ind++] = min(fca.get(k), fcb.get(k));
            }
        }
        List<Integer> div = new ArrayList<>();
        getDiv(map, fc, cnt, div, 0, 1);
        Collections.sort(div);
        cnt = div.size();
        int n = ri();
        while(n --> 0) {
            int lo = rni(), hi = ni(), l = 0, r = cnt - 1, res = -1;
            while(l <= r) {
                int m = l + (r - l) / 2;
                if(div.get(m) <= hi) {
                    l = m + 1;
                    res = m;
                } else {
                    r = m - 1;
                }
            }
            prln(res == -1 || div.get(res) < lo ? -1 : div.get(res));
        }
        close();
    }

    static void getDiv(int[] map, int[] fc, int cnt, List<Integer> div, int cur, int x) {
        div.add(x);
        if(cur == cnt) {
            return;
        }
        for(int i = 0; i < fc[cur]; ++i) {
            x *= map[cur];
            getDiv(map, fc, cnt, div, cur + 1, x);
        }
    }

    static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out));
    static StringTokenizer input;
    static Random rand = new Random();

    // references
    // IBIG = 1e9 + 7
    // IRAND ~= 3e8
    // IMAX ~= 2e10
    // LMAX ~= 9e18
    // constants
    static final int IBIG = 1000000007;
    static final int IRAND = 327859546;
    static final int IMAX = 2147483647;
    static final int IMIN = -2147483648;
    static final long LMAX = 9223372036854775807L;
    static final long LMIN = -9223372036854775808L;
    // util
    static int minof(int a, int b, int c) {return min(a, min(b, c));}
    static int minof(int... x) {return x.length == 1 ? x[0] : x.length == 2 ? min(x[0], x[1]) : min(x[0], minstarting(1, x));}
    static int minstarting(int offset, int... x) {assert x.length > 2; return offset == x.length - 2 ? min(x[offset], x[offset + 1]) : min(x[offset], minstarting(offset + 1, x));}
    static long minof(long a, long b, long c) {return min(a, min(b, c));}
    static long minof(long... x) {return x.length == 1 ? x[0] : x.length == 2 ? min(x[0], x[1]) : min(x[0], minstarting(1, x));}
    static long minstarting(int offset, long... x) {assert x.length > 2; return offset == x.length - 2 ? min(x[offset], x[offset + 1]) : min(x[offset], minstarting(offset + 1, x));}
    static int maxof(int a, int b, int c) {return max(a, max(b, c));}
    static int maxof(int... x) {return x.length == 1 ? x[0] : x.length == 2 ? max(x[0], x[1]) : max(x[0], maxstarting(1, x));}
    static int maxstarting(int offset, int... x) {assert x.length > 2; return offset == x.length - 2 ? max(x[offset], x[offset + 1]) : max(x[offset], maxstarting(offset + 1, x));}
    static long maxof(long a, long b, long c) {return max(a, max(b, c));}
    static long maxof(long... x) {return x.length == 1 ? x[0] : x.length == 2 ? max(x[0], x[1]) : max(x[0], maxstarting(1, x));}
    static long maxstarting(int offset, long... x) {assert x.length > 2; return offset == x.length - 2 ? max(x[offset], x[offset + 1]) : max(x[offset], maxstarting(offset + 1, x));}
    static int powi(int a, int b) {if(a == 0) return 0; int ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
    static long powl(long a, int b) {if(a == 0) return 0; long ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
    static int floori(double d) {return (int)d;}
    static int ceili(double d) {return (int)ceil(d);}
    static long floorl(double d) {return (long)d;}
    static long ceill(double d) {return (long)ceil(d);}
    static void shuffle(int[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
    static void shuffle(long[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
    static void shuffle(double[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
    static <T> void shuffle(T[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); T swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
    static void sort(int[] a) {shuffle(a); Arrays.sort(a);}
    static void sort(long[] a) {shuffle(a); Arrays.sort(a);}
    static void sort(double[] a) {shuffle(a); Arrays.sort(a);}
    static void qsort(int[] a) {Arrays.sort(a);}
    static void qsort(long[] a) {Arrays.sort(a);}
    static void qsort(double[] a) {Arrays.sort(a);}
    static int randInt(int min, int max) {return rand.nextInt(max - min + 1) + min;}
    // input
    static void r() throws IOException {input = new StringTokenizer(__in.readLine());}
    static int ri() throws IOException {return Integer.parseInt(__in.readLine());}
    static long rl() throws IOException {return Long.parseLong(__in.readLine());}
    static int[] ria(int n) throws IOException {int[] a = new int[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Integer.parseInt(input.nextToken()); return a;}
    static long[] rla(int n) throws IOException {long[] a = new long[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Long.parseLong(input.nextToken()); return a;}
    static char[] rcha() throws IOException {return __in.readLine().toCharArray();}
    static String rline() throws IOException {return __in.readLine();}
    static int rni() throws IOException {input = new StringTokenizer(__in.readLine()); return Integer.parseInt(input.nextToken());}
    static int ni() {return Integer.parseInt(input.nextToken());}
    static long rnl() throws IOException {input = new StringTokenizer(__in.readLine()); return Long.parseLong(input.nextToken());}
    static long nl() {return Long.parseLong(input.nextToken());}
    // output
    static void pr(int i) {__out.print(i);}
    static void prln(int i) {__out.println(i);}
    static void pr(long l) {__out.print(l);}
    static void prln(long l) {__out.println(l);}
    static void pr(double d) {__out.print(d);}
    static void prln(double d) {__out.println(d);}
    static void pr(char c) {__out.print(c);}
    static void prln(char c) {__out.println(c);}
    static void pr(char[] s) {__out.print(new String(s));}
    static void prln(char[] s) {__out.println(new String(s));}
    static void pr(String s) {__out.print(s);}
    static void prln(String s) {__out.println(s);}
    static void pr(Object o) {__out.print(o);}
    static void prln(Object o) {__out.println(o);}
    static void prln() {__out.println();}
    static void pryes() {__out.println("yes");}
    static void pry() {__out.println("Yes");}
    static void prY() {__out.println("YES");}
    static void prno() {__out.println("no");}
    static void prn() {__out.println("No");}
    static void prN() {__out.println("NO");}
    static void pryesno(boolean b) {__out.println(b ? "yes" : "no");};
    static void pryn(boolean b) {__out.println(b ? "Yes" : "No");}
    static void prYN(boolean b) {__out.println(b ? "YES" : "NO");}
    static void prln(int... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);}
    static void prln(long... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);}
    static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for(int i = 0; i < n; __out.print(iter.next()), __out.print(' '), ++i); if(n >= 0) __out.println(iter.next());}
    static void h() {__out.println("hlfd");}
    static void flush() {__out.flush();}
    static void close() {__out.close();}
}


import java.io.*;
import java.util.*;

public class Q3 {
    public static int gcd(int a,int b)
    {
        if(b==0)
         return a;
        else
         return gcd(b,a%b);
    }
    public static void main(String[] args)throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String[] ss=br.readLine().split(" ");
        int a=Integer.parseInt(ss[0]);
        int b=Integer.parseInt(ss[1]);
        int n=Integer.parseInt(br.readLine());
        int g=gcd(Math.max(a,b),Math.min(a,b));
        ArrayList<Integer> arr=new ArrayList<>();
        HashMap<Integer,Integer> map=new HashMap<>();
        for(int i=1;i*i<=g;i++)
        {
            if(g%i==0)
            {
                if(i*i==g)
                {   map.put(i,1);
                    arr.add(i);
                }
                else
                {
                    if(map.containsKey(i)==false)
                    { arr.add(i);
                      map.put(i,1);
                    }
                    if(map.containsKey(g/i)==false)
                    {
                        arr.add(g/i);
                        map.put(g/i,1);
                    }
                }
            }
        }
        Collections.sort(arr);
        StringBuffer sb=new StringBuffer();
        while(n>0)
        {
            n--;
            ss=br.readLine().split(" ");
            int l=Integer.parseInt(ss[0]);
            int h=Integer.parseInt(ss[1]);
            if(l<=g&&g<=h)
             sb.append(g);
            else
            {
               int low=0;int high=arr.size()-1;
               int mid=0;int ans=-1;
               while(low<=high)
               {
                   mid=(low+high)/2;
                   if(arr.get(mid)<l)
                    low=mid+1;
                   else if(arr.get(mid)>h)
                    high=mid-1;
                   else
                   {
                      ans=arr.get(mid);
                      low=mid+1;
                   }
               }
               sb.append(ans);
            }
            sb.append(System.lineSeparator());
        }
        System.out.println(sb);
    }
}


import java.math.*;
import java.util.*;
public class Main {

    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
     int t,c,l,u,a=sc.nextInt(),b=sc.nextInt(),T=sc.nextInt();

        int gs=gcd(a,b);

        TreeSet<Integer> tree_set = new TreeSet<Integer>();
        for (int i = 1; i * i <= gs; i+=1)
        {
            if (gs % i == 0)
            {
                tree_set.add(i);
                tree_set.add(gs / i);
            }
        }
        ArrayList<Integer> ar=new ArrayList();

        Iterator<Integer> it = tree_set.iterator();
        Integer current = 0;
        while(it.hasNext() ) {
            current = it.next();
          ar.add(current);
        }

        while (T-->0){
        l=sc.nextInt();u=sc.nextInt();c=1;

         if(l>gs){System.out.println(-1);c=11;}
           else for(t=ar.size()-1;t>=0;t--)
                 if(l<=ar.get(t)&&ar.get(t)<=u){System.out.println(ar.get(t));c=11;break;}

           if(c==1)System.out.println(-1);
     }

    }

    private static int gcd(int a, int b) {
        BigInteger b1 = BigInteger.valueOf(a);
        BigInteger b2 = BigInteger.valueOf(b);
        BigInteger gcd = b1.gcd(b2);
        return gcd.intValue();
    }

}


import java.math.*;
import java.util.*;
public class Main {
    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
     int t,c=1,l,u,a=sc.nextInt(),b=sc.nextInt(),T=sc.nextInt(),gs=gcd(a,b),gss=gs;
     while (T-->0){
        l=sc.nextInt();u=sc.nextInt();gs=gss;
        while(true){
           if(l<=gs&&gs<=u){System.out.println(gs);break;}
           else if(l>gs){System.out.println(-1);break;}
             while (true){
                   if(gs%++c==0){gs/=c;break;}
               }c=0;

        }
     }
    }
    private static int gcd(int a, int b) {
        BigInteger b1 = BigInteger.valueOf(a);
        BigInteger b2 = BigInteger.valueOf(b);
        BigInteger gcd = b1.gcd(b2);
        return gcd.intValue();
    }

}


import java.math.*;
import java.util.*;
public class Main {
    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
     int t,c=1,l,u,a=sc.nextInt(),b=sc.nextInt(),T=sc.nextInt(),gs=gcd(a,b);
     while (T-->0){
        l=sc.nextInt();u=sc.nextInt();c=1;

           if(l<=gs&&gs<=u){System.out.println(gs);c=11;}
           else if(l>gs){System.out.println(-1);c=11;}
           else for(t=u;t>=l;t--)
                 if(a%t==0&&b%t==0){System.out.println(t);c=11;break;}

           if(c==1)System.out.println(-1);
     }
    }
    private static int gcd(int a, int b) {
        BigInteger b1 = BigInteger.valueOf(a);
        BigInteger b2 = BigInteger.valueOf(b);
        BigInteger gcd = b1.gcd(b2);
        return gcd.intValue();
    }

}


import java.math.*;
import java.util.*;
public class Main {
    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
     int t,c=1,l,u,a=sc.nextInt(),b=sc.nextInt(),T=sc.nextInt(),gs=gcd(a,b),gss=gs;
     while (T-->0){
        l=sc.nextInt();u=sc.nextInt();gs=gss;
        while(true){
           if(l<=gs&&gs<=u){System.out.println(gs);break;}
           else if(l>gs){System.out.println(-1);break;}
             while (true){
                   if(gs%++c==0){gs/=c;break;}
               }c=1;

        }
     }
    }
    private static int gcd(int a, int b) {
        BigInteger b1 = BigInteger.valueOf(a);
        BigInteger b2 = BigInteger.valueOf(b);
        BigInteger gcd = b1.gcd(b2);
        return gcd.intValue();
    }

}


import java.util.*;
public class Main {
    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
     int t,c=1,l,u,a=sc.nextInt(),b=sc.nextInt(),T=sc.nextInt();
     while (T-->0){
        l=sc.nextInt();u=sc.nextInt();c=1;

               for(t=u;t>=l;t--)
                 if(a%t==0&&b%t==0){System.out.println(t);c=11;break;}

           if(c==1)System.out.println(-1);
     }
    }

}


import java.awt.event.MouseAdapter;
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Main {

    private static final Scanner sc = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        PrintWriter pw = new PrintWriter(System.out);

        int a = sc.nextInt();
        int b = sc.nextInt();

        if(b > a)
        {
            a = a ^ b;
            b = a ^ b;
            a = a ^ b;
        }
        TreeSet<Integer> set = new TreeSet<>();

        int i = 1;
        while (1l * i * i <= b)
        {
            if(b % i == 0 && a % i == 0)
                set.add(i);

            if( b%i == 0 && a % (b/i) == 0)
                set.add(b/i);
            i++;
        }

        int q = sc.nextInt();
        while (q-- > 0)
        {
            int lo = sc.nextInt();
            int hi = sc.nextInt();

            Integer query = set.floor(hi);
            if(query != null && query >= lo)
                pw.println(query);
            else
                pw.println(-1);
        }
        pw.close();
    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(FileReader r) {
            br = new BufferedReader(r);
        }

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public String next() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return st.nextToken();
        }

        public boolean check() {
            if (!st.hasMoreTokens())
                return false;
            return true;
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public String nextLine() {
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        public double nextDouble() {
            String x = next();
            StringBuilder sb = new StringBuilder("0");
            double res = 0, f = 1;
            boolean dec = false, neg = false;
            int start = 0;
            if (x.charAt(0) == '-') {
                neg = true;
                start++;
            }
            for (int i = start; i < x.length(); i++)
                if (x.charAt(i) == '.') {
                    res = Long.parseLong(sb.toString());
                    sb = new StringBuilder("0");
                    dec = true;
                } else {
                    sb.append(x.charAt(i));
                    if (dec)
                        f *= 10;
                }
            res += Long.parseLong(sb.toString()) / f;
            return res * (neg ? -1 : 1);
        }

        public boolean ready() {
            try {
                return br.ready();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}


import java.awt.event.MouseAdapter;
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Main {

    private static final Scanner sc = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        PrintWriter pw = new PrintWriter(System.out);

        int a = sc.nextInt();
        int b = sc.nextInt();

        TreeSet<Integer> set = new TreeSet<>();

        int i = 1;
        while (1l * i * i <= Math.min(a,b))
        {
            if(Math.min(a , b) % i == 0 && Math.max(a , b)%i == 0)
                set.add(i);

            if(Math.max(a , b)%(Math.min(a,b) / i) == 0)
                set.add((Math.min(a,b) / i));
            i++;
        }

        int q = sc.nextInt();
        while (q-- > 0)
        {
            int lo = sc.nextInt();
            int hi = sc.nextInt();

            Integer query = set.floor(hi);
            if(query != null && query >= lo)
                pw.println(query);
            else
                pw.println(-1);
        }
        pw.close();
    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(FileReader r) {
            br = new BufferedReader(r);
        }

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public String next() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return st.nextToken();
        }

        public boolean check() {
            if (!st.hasMoreTokens())
                return false;
            return true;
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public String nextLine() {
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        public double nextDouble() {
            String x = next();
            StringBuilder sb = new StringBuilder("0");
            double res = 0, f = 1;
            boolean dec = false, neg = false;
            int start = 0;
            if (x.charAt(0) == '-') {
                neg = true;
                start++;
            }
            for (int i = start; i < x.length(); i++)
                if (x.charAt(i) == '.') {
                    res = Long.parseLong(sb.toString());
                    sb = new StringBuilder("0");
                    dec = true;
                } else {
                    sb.append(x.charAt(i));
                    if (dec)
                        f *= 10;
                }
            res += Long.parseLong(sb.toString()) / f;
            return res * (neg ? -1 : 1);
        }

        public boolean ready() {
            try {
                return br.ready();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}


import java.util.*;
import java.io.*;
import java.math.*;

public class Main
{
    public static void process(int test_number)throws IOException
    {
        int a = ni(), b = ni(), g = gcd(a, b);
        int q = ni();

        TreeSet<Integer> set = new TreeSet<Integer>();
        for(int i = 1; i <= Math.sqrt(g); i++){
            if(g % i != 0)
                continue;
            set.add(i);
            set.add(g / i);
        }

        Integer divisor = null;

        while(q-- > 0){
            int l = ni(), r = ni();
            divisor = set.floor(r);

            if(divisor == null || divisor < l)
                pn(-1);
            else
                pn(divisor);
        }
    }

    static final long mod = (long)1e9+7l;

    static FastReader sc;
    static PrintWriter out;
    public static void main(String[]args)throws IOException
    {
        out = new PrintWriter(System.out);
        sc = new FastReader();

        long s = System.currentTimeMillis();
        int t = 1;
        //t = ni();
        for(int i = 1; i <= t; i++)
            process(i);

        out.flush();
        System.err.println(System.currentTimeMillis()-s+"ms");
    }

    static void trace(Object... o){ System.err.println(Arrays.deepToString(o)); };
    static void pn(Object o){ out.println(o); }
    static void p(Object o){ out.print(o); }
    static int ni()throws IOException{ return Integer.parseInt(sc.next()); }
    static long nl()throws IOException{ return Long.parseLong(sc.next()); }
    static double nd()throws IOException{ return Double.parseDouble(sc.next()); }
    static String nln()throws IOException{ return sc.nextLine(); }
    static long gcd(long a, long b)throws IOException{ return (b==0)?a:gcd(b,a%b);}
    static int gcd(int a, int b)throws IOException{ return (b==0)?a:gcd(b,a%b); }
    static int bit(long n)throws IOException{ return (n==0)?0:(1+bit(n&(n-1))); }

    static class FastReader{
        BufferedReader br;
        StringTokenizer st;

        public FastReader(){
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next(){
            while (st == null || !st.hasMoreElements()){
                try{ st = new StringTokenizer(br.readLine()); } catch (IOException  e){ e.printStackTrace(); }
            }
            return st.nextToken();
        }

        String nextLine(){
            String str = "";
            try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); }
            return str;
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args) throws IOException {
		FastReader in = new FastReader();

		int a =in.nextInt(),b=in.nextInt();
		int gcd=gcd(Math.max(a,b),Math.min(a, b));
		ArrayList<Integer>f=getFactor(gcd);
		Collections.sort(f);
		int n =in.nextInt();

		for(int i=0;i<n;i++)
		{
			int lower=in.nextInt(),upper=in.nextInt(),best=-1;
			int l=0,r=f.size()-1;
			while(l<=r)
			{
				int mid=(l+r)/2;

				if(f.get(mid)<upper)
				{
					if(f.get(mid)<lower)
						l++;
						else {
							best=f.get(mid);
							l++;
						}

				}else if(f.get(mid)>upper)
					r=mid-1;
				else
				{
					best=f.get(mid);
					break;
				}

			}

			System.out.println(best);

		}

	}

	public static ArrayList<Integer> getFactor(int gcd) {
		ArrayList<Integer>f=new ArrayList<>();

		for(int i=1;i*i<=gcd;i++)
		{
			if(gcd%i==0) {
			if(i*i==gcd)
				f.add(i);
			else {
				f.add(i);
				f.add(gcd/i);
			}

		}
		}

		return f;

	}

	public static int gcd(int a, int b) {

		if(b==0)
			return a;

		return gcd(b,a%b);

	}

}

class FastReader {
	BufferedReader br;
	StringTokenizer st;

	public FastReader() {
		br = new BufferedReader(new InputStreamReader(System.in));

	}

	String next() {
		while (st == null || !st.hasMoreElements()) {
			try {
				st = new StringTokenizer(br.readLine());

			} catch (IOException e) {
				e.printStackTrace();
			}

		}

		return st.nextToken();

	}

	int nextInt() {

		return Integer.parseInt(next());
	}

	double nextDouble() {

		return Double.parseDouble(next());
	}

	long nextLong() {

		return Long.parseLong(next());
	}

	String nextLine() {
		String str = "";
		try {
			str = br.readLine();

		} catch (IOException e) {

			e.printStackTrace();
		}
		return str;

	}

}


import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class ModifiedGcd
{
	public static void main(String[] args)
	 {
		Scanner scan=new Scanner(System.in);

		long a=scan.nextLong();
		long b=scan.nextLong();
		int q=scan.nextInt();

		long gcd=getGcd(a,b);

		ArrayList<Long> list=getDivisor(gcd);
		Collections.sort(list);
		Long[] divisors=new Long[list.size()];
		divisors=list.toArray(divisors);

		while(q-->0)
		{
			long l=scan.nextLong();
			long r=scan.nextLong();

			 if(r==gcd)
			  {
				System.out.println(r);
			  }
			else
			{
				System.out.println(getModifiedGcd(divisors,l,r));

			}

		}

		scan.close();
	 }

	static long getModifiedGcd(Long[] divisors,long l,long r)
	 {
		int start=-1;
		int end=divisors.length;

		while(start+1!=end)
		{
			int mid=start+(end-1)/2;

			if(divisors[mid]<=r)
			  {
				start=mid;
			  }
			else
			{
				end=mid;
			}
		}

		if(divisors[start]>=l)
		{
			return divisors[start];
		}
		else
		{
			return -1;
		}
	 }

	static long getGcd(long a,long b)
	  {
	   	if(a==0)
	   		{
	   		 return b;
	   		}

	   	return getGcd(b%a,a);

	  }

	static ArrayList<Long>  getDivisor(long num)
	{

        ArrayList<Long> list=new ArrayList<Long>();
		for(long i=1;i<=Math.sqrt(num);i++)
		{
			if(num%i==0)
			{
				if(num/i==i)
				{
					list.add(i);
				}
				else
				{
					list.add(i);
					list.add(num/i);
				}
			}
		}
		return list;
	}
}


import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class ModifiedGcd
{
	public static void main(String[] args)
	 {
		Scanner scan=new Scanner(System.in);
		long a=scan.nextLong();
		long b=scan.nextLong();
		int q=scan.nextInt();

	    long gcd=getGcd(a,b);

		ArrayList<Long> list=getDivisor(gcd);
		Collections.sort(list);
		Long[] divisors=new Long[list.size()];

		divisors=list.toArray(divisors);
   		while(q-->0)
		{

			long l=scan.nextLong();
			long r=scan.nextLong();

			 if(r==gcd)
			  {
				System.out.println(r);
			  }
			else
			{
				System.out.println(getModifiedGcd(divisors,l,r));

			}

		}

		scan.close();
	 }

	static long getModifiedGcd(Long[] divisors,long l,long r)
	 {
		int start=0;
		int end=divisors.length;

		while(start+1<end)
		{
			int mid=(start+end)/2;
			if(divisors[mid]==r)
			  {
				start=mid;
				break;
			  }
			else if(divisors[mid]<r)
			{
				start=mid;
				mid++;

			}
			else
			{
				end=mid;
				mid--;
			}
		}
		if(divisors[start]>=l)
		  {
			return divisors[start];
		  }
		else
		  {
			return -1;
		}
	 }
	static boolean isPrime(long l)
	{

		    if (l%2==0) return false;
		    for(int i=3;i<=Math.sqrt(l);i+=2)
		    {
		        if(l%i==0)
		        {
		        	return false;
		        }
		    }
		    return true;
	}

	static long getGcd(long a,long b)
	  {
	   	if(a==0)
	   		{
	   		 return b;
	   		}

	   	return getGcd(b%a,a);

	  }

	static ArrayList<Long>  getDivisor(long num)
	{

        ArrayList<Long> list=new ArrayList<Long>();
		for(long i=1;i<=Math.sqrt(num);i++)
		{
			if(num%i==0)
			{
				if(num/i==i)
				{
					list.add(i);
				}
				else
				{
					list.add(i);
					list.add(num/i);
				}
			}
		}
		return list;
	}
}


import javax.print.DocFlavor;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import java.lang.management.MemoryType;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.security.cert.CollectionCertStoreParameters;
import java.util.*;
import java.util.function.Function;

public class Main {

    static long gcd(long a, long b)
    {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }

        public static void main (String[]args) throws IOException {
        Scanner in = new Scanner(System.in);
        try(PrintWriter or = new PrintWriter(System.out)) {
                int n=in.nextInt();
                int m=in.nextInt();
                ArrayList<Integer>ans=new ArrayList<>();
                int g=(int)gcd(n,m);
            for (int i = 1; i*i<=g ; i++) {
                if (g%i==0){
                    ans.add(i);
                    if (g/i!=i)ans.add(g/i);
                }
            }
            n=in.nextInt();
            Collections.sort(ans);

            outer:
            while (n-->0){
                int l=in.nextInt(),r=in.nextInt(),an=-1;
                   int low=0,high=ans.size()-1;
                   while (high>=low){
                       int mid=(low+high)>>1;
                       if (ans.get(mid)>r)high=mid-1;
                       else if (ans.get(mid)<=r){
                           an=ans.get(mid);
                           low=mid+1;
                       }
                   }
                  // or.println("temp "+an);
                   if (an>=l){
                       or.println(an);
                       continue outer;
                   }
                   an=-1;
                     low=0;
                   high=ans.size()-1;
                    while (high>=low){
                        int mid=(low+high)>>1;
                        if (ans.get(mid)>=l){
                                an=ans.get(mid);
                                high=mid-1;
                        }else if (ans.get(mid)<l)low=mid+1;

                    }
                    if (an<=r){
                        or.println(an);
                    }else {
                        or.println(-1);

                    }

            }

        }
    }

    static int getlen(int r,int l,int a){
        return (r-l+1+1)/(a+1);
    }

    static class Scanner {

        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public String nextLine() throws IOException {
            return br.readLine();
        }

        public double nextDouble() throws IOException {
            String x = next();
            StringBuilder sb = new StringBuilder("0");
            double res = 0, f = 1;
            boolean dec = false, neg = false;
            int start = 0;
            if (x.charAt(0) == '-') {
                neg = true;
                start++;
            }
            for (int i = start; i < x.length(); i++) {
                if (x.charAt(i) == '.') {
                    res = Long.parseLong(sb.toString());
                    sb = new StringBuilder("0");
                    dec = true;
                } else {
                    sb.append(x.charAt(i));
                    if (dec) {
                        f *= 10;
                    }
                }
            }
            res += Long.parseLong(sb.toString()) / f;
            return res * (neg ? -1 : 1);
        }

        public boolean ready() throws IOException {
            return br.ready();
        }

    }
}

class Pair  {
    int first, second;

    public Pair(int first, int second) {
        this.first = first;
        this.second = second;
    }

}

class Tempo {

    int num;
    int index;

    public Tempo(int num, int index) {
        this.num = num;
        this.index = index;
    }
}


import javax.print.DocFlavor;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import java.lang.management.MemoryType;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.security.cert.CollectionCertStoreParameters;
import java.util.*;
import java.util.function.Function;

public class Main {

    static long gcd(long a, long b)
    {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }

        public static void main (String[]args) throws IOException {
        Scanner in = new Scanner(System.in);
        try(PrintWriter or = new PrintWriter(System.out)) {
                int n=in.nextInt();
                int m=in.nextInt();
                ArrayList<Integer>ans=new ArrayList<>();
                int g=(int)gcd(n,m);
            for (int i = 1; i<=g ; i++) {
                if (g%i==0){
                    ans.add(i);
                    if (g/i!=i)ans.add(g/i);
                }
            }
            n=in.nextInt();
            Collections.sort(ans);
            outer:
            while (n-->0){
                int l=in.nextInt(),r=in.nextInt(),an=-1;
                   int low=0,high=ans.size()-1;
                   while (high>=low){
                       int mid=(low+high)>>1;
                       if (ans.get(mid)>r)high=mid-1;
                       else if (ans.get(mid)<=r){
                           an=ans.get(mid);
                           low=mid+1;
                       }
                   }
                  // or.println("temp "+an);
                   if (an>=l){
                       or.println(an);
                       continue outer;
                   }
                   an=-1;
                     low=0;
                   high=ans.size()-1;
                    while (high>=low){
                        int mid=(low+high)>>1;
                        if (ans.get(mid)>=l){
                                an=mid;
                                high=mid-1;
                        }else if (ans.get(mid)<=l)low=mid+1;

                    }
                    if (an<=r){
                        or.println(an);
                        continue outer;
                    }else {
                        or.println(-1);
                        continue outer;
                    }

            }

        }
    }

    static int getlen(int r,int l,int a){
        return (r-l+1+1)/(a+1);
    }

    static class Scanner {

        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public String nextLine() throws IOException {
            return br.readLine();
        }

        public double nextDouble() throws IOException {
            String x = next();
            StringBuilder sb = new StringBuilder("0");
            double res = 0, f = 1;
            boolean dec = false, neg = false;
            int start = 0;
            if (x.charAt(0) == '-') {
                neg = true;
                start++;
            }
            for (int i = start; i < x.length(); i++) {
                if (x.charAt(i) == '.') {
                    res = Long.parseLong(sb.toString());
                    sb = new StringBuilder("0");
                    dec = true;
                } else {
                    sb.append(x.charAt(i));
                    if (dec) {
                        f *= 10;
                    }
                }
            }
            res += Long.parseLong(sb.toString()) / f;
            return res * (neg ? -1 : 1);
        }

        public boolean ready() throws IOException {
            return br.ready();
        }

    }
}

class Pair  {
    int first, second;

    public Pair(int first, int second) {
        this.first = first;
        this.second = second;
    }

}

class Tempo {

    int num;
    int index;

    public Tempo(int num, int index) {
        this.num = num;
        this.index = index;
    }
}


import java.util.*;
import java.io.*;

public class File {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int a = sc.nextInt();
        int b = sc.nextInt();

        int min = Math.min(a, b);
        int max = Math.max(a, b);

        int queries = sc.nextInt();

        // Precompute all factors of min
        List<Integer> factors = getFactors(min);

        for (int q = 0; q < queries; q++) {
            int low = sc.nextInt();
            int high = sc.nextInt();

            // Find largest divisor between low and high.
            int res = search(factors, low, high);

            System.out.println(res);
        }
    }

    public static int search(List<Integer> factors, int low, int high) {
        int lo = 0;
        int hi = factors.size() - 1;
        int largestIndex = -1;

        while (lo <= hi) {
            int mid = lo + (hi - lo) / 2;

            if (factors.get(mid) > high) {
                hi = mid - 1;
            }
            else if (factors.get(mid) < low) {
                lo = mid + 1;
            }
            else {
                largestIndex = Math.max(largestIndex, mid);
                lo = mid + 1;
            }
        }

        return largestIndex == -1 ? -1 : factors.get(largestIndex);
    }

    public static List<Integer> getFactors(int num) {
        List<Integer> list = new ArrayList<>();

        list.add(1);
        if (num == 1) {
            return list;
        }

        list.add(num);
        int root = (int) Math.sqrt(num);

        for (int i = 2; i <= root; i++) {
            if (num % i == 0) {
                list.add(i);

                if ((num / i) != i) {
                    list.add(num / i);
                }
            }
        }

        Collections.sort(list);
        return list;
    }
}


import java.util.*;
import java.io.*;

public class File {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int a = sc.nextInt();
        int b = sc.nextInt();

        int min = Math.min(a, b);
        int max = Math.max(a, b);

        int queries = sc.nextInt();

        // Precompute all factors of min
        List<Integer> factors = getFactors(min, max);

        for (int q = 0; q < queries; q++) {
            int low = sc.nextInt();
            int high = sc.nextInt();

            // Find largest divisor between low and high.
            int res = search(factors, low, high);

            System.out.println(res);
        }
    }

    public static int search(List<Integer> factors, int low, int high) {
        int lo = 0;
        int hi = factors.size() - 1;
        int largestIndex = -1;

        while (lo <= hi) {
            int mid = lo + (hi - lo) / 2;

            if (factors.get(mid) > high) {
                hi = mid - 1;
            }
            else if (factors.get(mid) < low) {
                lo = mid + 1;
            }
            else {
                largestIndex = Math.max(largestIndex, mid);
                lo = mid + 1;
            }
        }

        return largestIndex == -1 ? -1 : factors.get(largestIndex);
    }

    public static List<Integer> getFactors(int num, int max) {
        List<Integer> list = new ArrayList<>();

        list.add(1);
        if (num == 1) return list;

        if (max % num == 0) {
            list.add(num);
        }

        int root = (int) Math.sqrt(num);

        for (int i = 2; i <= root; i++) {
            if (num % i == 0) {
                if (max % i == 0) list.add(i);

                if (((num / i) != i) && ((max % (num / i)) == 0)) {
                    list.add(num / i);
                }
            }
        }

        Collections.sort(list);
        return list;
    }
}


import java.util.*;
import java.math.BigInteger;

public class hell
{
    public static void main(String args[])
    {
        Scanner cin = new Scanner(System.in);
        int a = cin.nextInt(), b = cin.nextInt();
        TreeSet<Integer> div1 = new TreeSet<Integer>(), div2 = new TreeSet<Integer>();
        for(int i=1; i*i<=a; i++)
        {
            if( a%i == 0 )
            {
                int x = i, y = a/i;
                div1.add(x);
                if( x!=y ) div1.add(y);
            }
        }
        for(int i=1; i*i<=b; i++)
        {
            if( b%i == 0 )
            {
                int x = i, y = b/i;
                div2.add(x);
                if( x!=y ) div2.add(y);
            }
        }
        TreeSet<Integer> common = new TreeSet<Integer>();
        for(int i:div1) if( div2.contains(i) ) common.add(i);
        // for(int i:common) System.out.print(i+" ");
        // System.out.println();
        int[] s = new int[common.size()];
        int it = 0;
        for(int i:common) s[it++] = i;

        int q = cin.nextInt();
        for(int query=0; query<q; query++)
        {
            int l = cin.nextInt(), r = cin.nextInt();
            int LO = 0, HI = common.size() - 1, ans = -1;
            while( LO<=HI )
            {
                int M = (LO+HI) >> 1;
                if( s[M] > r ) HI = M-1;
                else
                {
                    ans = Math.max( ans,s[M] );
                    LO = M+1;
                }
            }
            if( ans < l ) System.out.println(-1);
            else System.out.println(ans);
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class d {
    public static void main(String[] args) throws IOException {
        //      Scanner s = new Scanner(System.in);
        BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
        //       String[] st=s.readLine().trim().split("\\s+");
        //      a[i]=Integer.parseInt(st[i]);
        //      Integer.parseInt(s.readLine().trim().split("\\s+");
        StringBuilder sb = new StringBuilder();StringBuilder sbf = new StringBuilder();
        //  int n=Integer.parseInt(s.readLine().trim().split("\\s+")[0]);
     /*   String[] st=s.readLine().trim().split("\\s+");
        int n=Integer.parseInt(st[0]);*/
         String[] st=s.readLine().trim().split("\\s+");
         long a=Long.parseLong(st[0]);
         long b=Long.parseLong(st[1]);
         HashMap<Long,Long> vis=new HashMap<>();
         for(long i=1;i<=Math.sqrt(a);i++){
             if(a%i==0){
                 vis.put(i,0L);vis.put(a/i,0L);
             }
         }
         ArrayList<Long> ab=new ArrayList<Long>();
         for(long i=1;i<=Math.sqrt(b);i++){
             if(b%i==0){
                 if(vis.containsKey(i)) ab.add(i);
                 if(i!=b/i&&vis.containsKey(b/i)) ab.add(b/i);
             }
         }Long[] ans=new Long[ab.size()];
        ans=ab.toArray(ans);
        int n=Integer.parseInt(s.readLine().trim().split("\\s+")[0]);
        Arrays.sort(ans);
        for(int i=0;i<n;i++){String[] st1=s.readLine().trim().split("\\s+");
            int low=Integer.parseInt(st1[0]);
            int high=Integer.parseInt(st1[1]);
            int l=0;int h=ans.length-1;int val=ans.length-1;
            while(l<=h){
                int mid=(l+h)/2;
                if(high>=ans[mid]){
                    val=mid;
                    l=mid+1;
                }else h=mid-1;
            }
            if(ans[val]>=low){
                sb.append(ans[val]+"\n");
            }else sb.append(-1+"\n");
        }
        System.out.println(sb.toString());
    }
    static String lexographicallysmallest(String s) {
        if (s.length() % 2 == 1) return s;
        String s1 =lexographicallysmallest(s.substring(0, s.length()/2));
        String s2 = lexographicallysmallest(s.substring(s.length()/2, s.length()));
        if (s1.compareTo(s2)<0) return s1 + s2;
        else return s2 + s1;
    }
    public static int countSetBits(int n)
    {
        return (BitsSetTable256[n & 0xff]
                + BitsSetTable256[(n >> 8) & 0xff]
                + BitsSetTable256[(n >> 16) & 0xff]
                + BitsSetTable256[n >> 24]);
    }
    static int[] BitsSetTable256 ;
    public static void initialize(int n)
    {
        BitsSetTable256[0] = 0;
        for (int i = 0; i <=Math.pow(2,n); i++) {
            BitsSetTable256[i] = (i & 1) + BitsSetTable256[i / 2];
        }
    }
    static void dfs(int i,int val,ArrayList<Integer>[] adj){
    }

    static  void computeLPSArray(String pat, int M, int lps[]) {
        int len = 0;
        int i = 1;
        lps[0] = 0;
        while (i < M) {
            if (pat.charAt(i) == pat.charAt(len)) {
                len++;
                lps[i] = len;
                i++;
            }
            else
            {
                if (len != 0) {
                    len = lps[len - 1];
                }
                else
                {
                    lps[i] = len;
                    i++;
                }
            }
        }
    }
    static boolean isPrime(int n) {
        if (n <= 1) return false;
        if (n <= 3) return true;

        if (n % 2 == 0 || n % 3 == 0) return false;

        for (int i = 5; i * i <= n; i = i + 6)
            if (n % i == 0 || n % (i + 2) == 0)
                return false;

        return true;
    }
    static long powerwithmod(long  x, long y, int 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;
    }
    static long  powerwithoutmod(long x, int y) {
        long  temp;
        if( y == 0)
            return 1;
        temp = powerwithoutmod(x, y/2);
        if (y%2 == 0)
            return temp*temp;
        else
        {
            if(y > 0)
                return x * temp * temp;
            else
                return (temp * temp) / x;
        }
    }
    static void fracion(double x) {
        String a = "" + x;
        String spilts[] = a.split("\\."); // split using decimal
        int b = spilts[1].length(); // find the decimal length
        int denominator = (int) Math.pow(10, b); // calculate the denominator
        int numerator = (int) (x * denominator); // calculate the nerumrator Ex
        // 1.2*10 = 12
        int gcd = (int) gcd((long) numerator, denominator); // Find the greatest common
        // divisor bw them
        String fraction = "" + numerator / gcd + "/" + denominator / gcd;
        // System.out.println((denominator/gcd));
        long x1 = modInverse(denominator / gcd, 998244353);
        //  System.out.println(x1);
        System.out.println((((numerator / gcd) % 998244353 * (x1 % 998244353)) % 998244353));
    }
    static int bfs(int i1, ArrayList<Integer>[] h, int[] vis, int n,int val1) {
        Queue<Integer> q = new LinkedList<Integer>();
        q.add(i1);Queue<Integer> aq=new LinkedList<Integer>();
        aq.add(0);
        while(!q.isEmpty()){
            int i=q.poll();
            int val=aq.poll();
            if(i==n){
                return val;
            }
            if(h[i]!=null){
                for(Integer j:h[i]){
                    if(vis[j]==0){
                        q.add(j);vis[j]=1;
                        aq.add(val+1);}
                }
            }
        }return -1;
    }
    static long gcd(long a, long b) {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }
    static long modInverse(long a, int m)
    {
        return (powerwithmod(a, m - 2, m));
    }
    static int MAXN;
    static int[] spf;
    static void sieve() {
        spf[1] = 1;
        for (int i=2; i<MAXN; i++)
            spf[i] = i;
        for (int i=4; i<MAXN; i+=2)
            spf[i] = 2;
        for (int i=3; i*i<MAXN; i++)
        {
            if (spf[i] == i)
            {
                for (int j=i*i; j<MAXN; j+=i)

                    if (spf[j]==j)
                        spf[j] = i;
            }
        }
    }
    static ArrayList<Integer> getFactorizationUsingSeive(int x) {
        ArrayList<Integer> ret = new ArrayList<Integer>();
        while (x != 1)
        {
            ret.add(spf[x]);
            x = x / spf[x];
        }
        return ret;
    }
   static long[] fac ;
     static void calculatefac(int mod){
         fac[0]=1;
        for (int i = 1 ;i <= MAXN; i++)
            fac[i] = fac[i-1] * i % mod;
     }
      static long nCrModPFermat(int n, int r, int mod) {
         if (r == 0)
            return 1;
        fac[0] = 1;
        return (fac[n]*
                modInverse(fac[r], mod)
                % mod * modInverse(fac[n-r], mod)
                                    % mod) % mod;
    } }
class Student {
    long  l;long r;long x;
    public Student(long l, long r,long x) {
        this.l = l;this.x=x;
        this.r = r;
    }

    public String toString()
    {
        return this.l+" ";
    }
}
class Sortbyroll implements Comparator<Student>
{

    public int compare(Student a, Student b){
      if(a.l<b.l) return 1;
      else if(a.l==b.l){
          if(a.r==b.r){
              return 0;
          }
          if(a.r<b.r) return -1;
          return 1;}
      return -1;  }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Random;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Contest {
	private static PrintWriter out = new PrintWriter(System.out);
	private static int n, m, k, tc, x, N, q;
	private static int[] a;
	private static int[][] grid;

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt(), b = sc.nextInt();
		if(a > b) {
			int tmp = a;
			a = b;
			b = tmp;
		}
		q = sc.nextInt();
		ArrayList<Integer> div_a = divisors(a);
		Collections.sort(div_a);
		ArrayList<Integer> div_b = divisors(b);
		Collections.sort(div_b);
//		System.out.println(div_a);
//		System.out.println(div_b);
		while(q-->0) {
			int s = sc.nextInt();
			int e = sc.nextInt();
//			System.out.println(div_a.size());
//			System.out.println(div_b.size());
			int up_a = up(s, div_a, e), up_b = up(s, div_b, e);
			int lb_a = lb(e, div_a, s), lb_b = lb(e, div_b, s);
			//System.out.println(up_a + " " + up_b + " " + lb_a + " " + lb_b);
			if((up_a == -1 && lb_a == -1) || (up_b == -1 && lb_b == -1)) {
				out.println(-1);
				continue;
			}
			n = div_a.size();
			int res = -1;
			for(int i = lb_a;i >= up_a;i--) {
				int el = div_a.get(i);
				int search = lb(el, div_b,s);
				//System.out.println(el + " " + search);
				if( search == -1 || div_b.get(search) != el)
					continue;
				res = el;
				break;
			}
			out.println(res);

		}
		out.flush();
	}

	private static int lb(int x, ArrayList<Integer> d, int st) {
		int s = 0, e = d.size() - 1, ans = -1;
		while(s <= e) {
			int mid = s + (e - s) / 2;
			if(d.get(mid) <= x) {
				ans = mid;
				s = mid + 1;
			} else {
				e = mid - 1;
			}
		}
		if(ans == -1 || d.get(ans) < st)
			return -1;
		return ans;
	}

	private static int up(int x, ArrayList<Integer> d, int end) {
		int s = 0, e = d.size() - 1, ans = -1;
		while(s <= e) {
			int mid = s + (e - s) / 2;
			if(d.get(mid) >= x) {
				ans = mid;
				e = mid - 1;
			} else {
				s = mid + 1;
			}
		}
		if(ans == -1 || d.get(ans) > end)
			return -1;
		return ans;
	}

	private static ArrayList<Integer> divisors(int n) {
		ArrayList<Integer> div = new ArrayList<Integer>();
		int i;
		for(i = 1; i * i < n;i++) {
			if(n % i == 0) {
				div.add(n / i);
				div.add(i);
			}
		}
		if(i*i == n)
			div.add(i);

		return div;
	}

	private static void sort(int[] a, int n) {
		ArrayList<Integer> b = new ArrayList<Integer>();
		for (int i = 0; i < n; i++)
			b.add(a[i]);
		Collections.sort(b);
		for (int i = 0; i < n; i++)
			a[i] = b.get(i);
	}

	static final Random random = new Random();

	static void ruffleSort(int[] a) {
		int n = a.length;// shuffle, then sort
		for (int i = 0; i < n; i++) {
			int oi = random.nextInt(n), temp = a[oi];
			a[oi] = a[i];
			a[i] = temp;
		}
		java.util.Arrays.sort(a);
	}

//	private static class Scanner {
//		public BufferedReader reader;
//		public StringTokenizer st;
//
//		public Scanner(InputStream stream) {
//			reader = new BufferedReader(new InputStreamReader(stream));
//			st = null;
//		}
//
//		public String next() {
//			while (st == null || !st.hasMoreTokens()) {
//				try {
//					String line = reader.readLine();
//					if (line == null)
//						return null;
//					st = new StringTokenizer(line);
//				} catch (Exception e) {
//					throw (new RuntimeException());
//				}
//			}
//			return st.nextToken();
//		}
//
//		public int nextInt() {
//			return Integer.parseInt(next());
//		}
//
//		public long nextLong() {
//			return Long.parseLong(next());
//		}
//
//		public double nextDouble() {
//			return Double.parseDouble(next());
//		}
//	}

}


#include <bits/stdc++.h>
#include <string>
#include <cmath>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<ll> vll;

#define all(v) v.begin(),v.end()
#define test() int t;cin>>t;while(t--)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int mod = 1e9+7;

vpii prime_fact;
vi div;

int power(int x, int a)
{
    if(a==0)return(1);
    int z = power(x, a/2);
    z*=z;
    if(a%2)z*=x;
    return(z);
}

void factorise(int n)
{
    int no = 2;
    while(no*no<=n)
    {
        int count = 0;
        while(n%no==0)
        {
            count++;
            n/=no;
        }
        prime_fact.push_back({no, count});
        no++;
    }
    if(n>1)prime_fact.push_back({n, 1});
}
void create(auto ptr, int prod)
{
    if(ptr==prime_fact.end())
    {
        div.push_back(prod);
        return;
    }
    int num = prime_fact[ptr].first, freq = prime_fact[ptr].second;
    for (int i = 0; i <= freq; i++)
    {
        create(ptr+1, prod*power(num, freq));
    }
}

int main()
{
    fastio
    int a, b;
    cin>>a>>b;
    factorise(__gcd(a, b));
    create(prime_fact.begin(), 1);
    sort(all(div));
    int count = div.size();
    test()
    {
        int low, high;
        cin>>low>>high;
        int l =0, h = count-1, val1, val2;
        while(l<=h)
        {
            int mid = l + (h-l)/2;
            if(div[mid]<=low)
            {
                val1=mid;
                l=mid+1;
            }
            else
                h=mid-1;
        }
        l = 0, h = count-1;
        while(l<=h)
        {
            int mid = l + (h-l)/2;
            if(div[mid]>=high)
            {
                val2=mid;
                h=mid-1;
            }
            else
                l=mid+1;
        }
        if(div[val1]<low)val1++;
        if(div[val2]>high)val2--;
        if(val1>val2)
            cout<<-1<<endl;
        else
            cout<<div[val1]<<endl;
    }
}


#include <bits/stdc++.h>
#include <string>
#include <cmath>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<ll> vll;

#define all(v) v.begin(),v.end()
#define test() int t;cin>>t;while(t--)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int mod = 1e9+7;

vpii prime_fact;
vi store;

int power(int x, int a)
{
    if(a==0)return(1);
    int z = power(x, a/2);
    z*=z;
    if(a%2)z*=x;
    return(z);
}

void factorise(int n)
{
    int no = 2;
    while(no*no<=n)
    {
        if(n%no==0)
        {
            int count = 0;
            while(n%no==0)
            {
                count++;
                n/=no;
            }
            prime_fact.push_back({no, count});
        }
        no++;
    }
    if(n>1)prime_fact.push_back({n, 1});
}
void create(int ptr, int prod)
{
    if(ptr==prime_fact.size())
    {
        store.push_back(prod);
        return;
    }
    int num = prime_fact[ptr].first, freq = prime_fact[ptr].second;
    for (int i = 0; i <= freq; i++)
    {
        create(ptr+1, prod*power(num, i));
    }
}

int main()
{
    fastio
    int a, b;
    cin>>a>>b;
    factorise(__gcd(a, b));
    create(0, 1);
    sort(all(store));
    int count = store.size();
    test()
    {
        int low, high;
        cin>>low>>high;
        int l =0, h = count-1, val1=0, val2=count-1;
        while(l<=h)
        {
            int mid = l + (h-l)/2;
            if(store[mid]<=low)
            {
                val1=mid;
                l=mid+1;
            }
            else
                h=mid-1;
        }
        l = 0, h = count-1;
        while(l<=h)
        {
            int mid = l + (h-l)/2;
            if(store[mid]>=high)
            {
                val2=mid;
                h=mid-1;
            }
            else
                l=mid+1;
        }
        if(store[val1]<low)val1++;
        if(store[val2]>high)val2--;
        if(val1>val2)
            cout<<-1<<endl;
        else
            cout<<store[val1]<<endl;
    }
}


#include <bits/stdc++.h>
#include <string>
#include <cmath>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<ll> vll;

#define all(v) v.begin(),v.end()
#define test() int t;cin>>t;while(t--)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int mod = 1e9+7;

vpii prime_fact;
vi store;

int power(int x, int a)
{
    if(a==0)return(1);
    int z = power(x, a/2);
    z*=z;
    if(a%2)z*=x;
    return(z);
}

void factorise(int n)
{
    int no = 2;
    while(no*no<=n)
    {
        if(n%no==0)
        {
            int count = 0;
            while(n%no==0)
            {
                count++;
                n/=no;
            }
            prime_fact.push_back({no, count});
        }
        no++;
    }
    if(n>1)prime_fact.push_back({n, 1});
}
void create(int ptr, int prod)
{
    if(ptr==prime_fact.size())
    {
        store.push_back(prod);
        return;
    }
    int num = prime_fact[ptr].first, freq = prime_fact[ptr].second;
    for (int i = 0; i <= freq; i++)
    {
        create(ptr+1, prod*power(num, i));
    }
}

int main()
{
    fastio
    int a, b;
    cin>>a>>b;
    factorise(__gcd(a, b));
    create(0, 1);
    sort(all(store));
    int count = store.size();
    test()
    {
        int low, high;
        cin>>low>>high;
        int l =0, h = count-1, val1=0, val2=count-1;
        while(l<=h)
        {
            int mid = l + (h-l)/2;
            if(store[mid]<=low)
            {
                val1=mid;
                l=mid+1;
            }
            else
                h=mid-1;
        }
        l = 0, h = count-1;
        while(l<=h)
        {
            int mid = l + (h-l)/2;
            if(store[mid]>=high)
            {
                val2=mid;
                h=mid-1;
            }
            else
                l=mid+1;
        }
        if(store[val1]<low)val1++;
        if(store[val2]>high)val2--;
        if(val1>val2)
            cout<<-1<<endl;
        else
            cout<<store[val2]<<endl;
    }
}


#include <bits/stdc++.h>
#include <string>
#include <cmath>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<ll> vll;

#define all(v) v.begin(),v.end()
#define test() int t;cin>>t;while(t--)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int mod = 1e9+7;

int main()
{
    fastio
    int a, b;
    cin>>a>>b;
    set<int> factor;
    int num = __gcd(a, b);
    for (int i = 1; i*i <= num; i++)
    {
        if(num%i==0)
        {
            factor.insert(i);
            factor.insert(num/i);
        }
    }
    test()
    {
        int low, high;
        cin>>low>>high;
        auto lwr = factor.lower_bound(low);
        auto uppr = factor.upper_bound(high);
        if(lwr==uppr)
            cout<<-1<<endl;
        else
            cout<<*(--uppr)<<endl;
    }
    return(0);
}


#include <bits/stdc++.h>
#include <string>
#include <cmath>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<ll> vll;

#define all(v) v.begin(),v.end()
#define test() int t;cin>>t;while(t--)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int mod = 1e9+7;

vpii prime_fact;
vi store;

int power(int x, int a)
{
    if(a==0)return(1);
    int z = power(x, a/2);
    z*=z;
    if(a%2)z*=x;
    return(z);
}

void factorise(int n)
{
    int no = 2;
    while(no*no<=n)
    {
        if(n%no==0)
        {
            int count = 0;
            while(n%no==0)
            {
                count++;
                n/=no;
            }
            prime_fact.push_back({no, count});
        }
        no++;
    }
    if(n>1)prime_fact.push_back({n, 1});
}
void create(int ptr, int prod)
{
    if(ptr==prime_fact.size())
    {
        store.push_back(prod);
        cout<<prod<<endl;
        return;
    }
    int num = prime_fact[ptr].first, freq = prime_fact[ptr].second;
    for (int i = 0; i <= freq; i++)
    {
        create(ptr+1, prod*power(num, freq));
    }
}

int main()
{
    fastio
    int a, b;
    cin>>a>>b;
    factorise(__gcd(a, b));
    create(0, 1);
    sort(all(store));
    int count = store.size();
    test()
    {
        int low, high;
        cin>>low>>high;
        int l =0, h = count-1, val1, val2;
        while(l<=h)
        {
            int mid = l + (h-l)/2;
            if(store[mid]<=low)
            {
                val1=mid;
                l=mid+1;
            }
            else
                h=mid-1;
        }
        l = 0, h = count-1;
        while(l<=h)
        {
            int mid = l + (h-l)/2;
            if(store[mid]>=high)
            {
                val2=mid;
                h=mid-1;
            }
            else
                l=mid+1;
        }
        if(store[val1]<low)val1++;
        if(store[val2]>high)val2--;
        if(val1>val2)
            cout<<-1<<endl;
        else
            cout<<store[val1]<<endl;
    }
}


#include <bits/stdc++.h>
#include <string>
#include <cmath>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<ll> vll;

#define all(v) v.begin(),v.end()
#define test() int t;cin>>t;while(t--)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int mod = 1e9+7;

vpii prime_fact;
vi div;

int power(int x, int a)
{
    if(a==0)return(1);
    int z = power(x, a/2);
    z*=z;
    if(a%2)z*=x;
    return(z);
}

void factorise(int n)
{
    int no = 2;
    while(no*no<=n)
    {
        int count = 0;
        while(n%no==0)
        {
            count++;
            n/=no;
        }
        prime_fact.push_back({no, count});
        no++;
    }
    if(n>1)prime_fact.push_back({n, 1});
}
void create(auto ptr, int prod)
{
    if(ptr==prime_fact.end())
    {
        div.push_back(prod);
        return;
    }
    int num = prime_fact[ptr].first, freq = prime_fact[ptr].second;
    for (int i = 0; i <= freq; i++)
    {
        create(ptr+1, prod*power(num, freq));
    }
}

int main()
{
    fastio
    int a, b;
    cin>>a>>b;
    factorise(__gcd(a, b));
    create(prime_fact.begin(), 1);
    sort(all(div));
    int count = div.size();
    test()
    {
        int low, high;
        cin>>low>>high;
        int l =0, h = count-1, val1, val2;
        while(l<=h)
        {
            int mid = l + (h-l)/2;
            if(div[mid]<=low)
            {
                val1=mid;
                l=mid+1;
            }
            else
                h=mid-1;
        }
        int l = 0, h = count-1;
        while(l<=h)
        {
            int mid = l + (h-l)/2;
            if(div[mid]>=high)
            {
                val2=mid;
                h=mid-1;
            }
            else
                l=mid+1;
        }
        if(div[val1]<low)val1++;
        if(div[val2]>high)val2--;
        if(val1>val2)
            cout<<-1<<endl;
        else
            cout<<div[val1]<<endl;
    }
}


#include <bits/stdc++.h>
#include <string>
#include <cmath>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<ll> vll;

#define all(v) v.begin(),v.end()
#define test() int t;cin>>t;while(t--)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int mod = 1e9+7;
vi factor;
int main()
{
    fastio
    int a, b;
    cin>>a>>b;
    int num = __gcd(a, b);
    for (int i = 1; i*i <= num; i++)
    {
        if(num%i==0)
        {
            if(i*i==num)
                factor.push_back(i);
            else
            {
                factor.push_back(i);
                factor.push_back(num/i);
            }
        }
    }
    sort(all(factor));
    int count = factor.size();
    test()
    {
        int low, high;
        cin>>low>>high;
        int l =0, h = count-1, val1=0, val2=count-1;
        while(l<=h)
        {
            int mid = l + (h-l)/2;
            if(factor[mid]<=low)
            {
                val1=mid;
                l=mid+1;
            }
            else
                h=mid-1;
        }
        l = 0, h = count-1;
        while(l<=h)
        {
            int mid = l + (h-l)/2;
            if(factor[mid]>=high)
            {
                val2=mid;
                h=mid-1;
            }
            else
                l=mid+1;
        }
        if(factor[val1]<low)val1++;
        if(factor[val2]>high)val2--;
        if(val1>val2)
            cout<<-1<<endl;
        else
            cout<<factor[val2]<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;

const int MAX =  2 * 507;
int table[MAX];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    long long n, jaka = 0, ile = 0, i, j, akt;
    cin >> n;
    n *= n;
    for(i = 1; i <= n; i++){
        cin >> table[i];
    }
    sort(table + 1, table + 1 + n);
    if(table[1] == table[n]){
    	for(int i = 1; i <= sqrt(n); i++){
    		cout << table[1] << " ";
    	}
    	cout << '\n';
    	return 0;
    }
    for(i = 1; i <= n; i++){
        akt = table[i];
        if(akt != jaka){
            if(ile % 2 == 1){
                cout << jaka << " ";
            }
            ile = 1;
            jaka = akt;
        }
        else{
            ile++;
        }
    }
    if(ile % 2 == 1){
       cout << jaka << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int MAX =  507 * 507;
int table[MAX];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, jaka = 0, ile = 0, i, j, akt;
    cin >> n;
    for(i = 1; i <= n * n; i++){
        cin >> table[i];
    }
    sort(table, table + 2 + (n * n));
    if(table[1] == table[n * n]){
    	for(int i = 1; i <= n; i++){
    		cout << table[1] << " ";
    	}
    	cout << '\n';
    	return 0;
    }
    for(i = 1; i <= n * n; i++){
        akt = table[i];
        if(akt > jaka){
            if(ile % 2 == 1){
                cout << jaka << " ";
            }
            ile = 1;
            jaka = akt;
        }
        else{
            ile++;
        }
    }
    if(ile % 2 == 1){
       cout << jaka << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int dziel[10007];

int NWD(int a, int b){
    int x;
    while(b != 0){
        x = b;
        b = a % b;
        a = x;
    }
    return a;
}

int BinSearch(int pocz, int kon, int naj){
    int sro;
    while(pocz < kon){
        sro = (pocz + kon + 1) / 2;
        if(dziel[sro] < naj){
            pocz = sro + 1;
        }
        else{
            kon = sro;
        }
    }
    return dziel[pocz];
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, a, b, pocz, kon, wyn, pier;
    cin >> a >> b >> n;
    int nwd = NWD(a, b);
    pier = sqrt(nwd);
    int j = 0;
    for(int i = 1; i <= pier; i++){
        if(nwd % i == 0){
            if(i * i == nwd){
                dziel[j] = i;
                j++;
            }
            else{
                dziel[j] = i;
                j++;
                dziel[j] = nwd/i;
                j++;
            }
        }
    }
    sort(dziel, dziel + j);
    for(int i = 1; i <= n; i++){
        cin >> pocz >> kon;
        wyn = BinSearch(0, j - 1, kon);
        if(wyn >= pocz && wyn <= kon){
            cout << wyn << '\n';
            continue;
        }
        cout << -1 << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int dziel[10007];

int NWD(int a, int b){
    int x;
    while(b != 0){
        x = b;
        b = a % b;
        a = x;
    }
    return a;
}

int BinSearch(int pocz, int kon, int naj){
    int sro;
    while(pocz < kon){
        sro = (pocz + kon + 1) / 2;
        if(dziel[sro] < naj){
            pocz = sro;
        }
        else{
            kon = sro - 1;
        }
    }
    return dziel[sro];
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, a, b, pocz, kon, wyn, pier;
    cin >> a >> b >> n;
    int nwd = NWD(a, b);
    pier = sqrt(nwd);
    int j = 0;
    for(int i = 1; i <= pier; i++){
        if(nwd % i == 0){
            if(i * i == nwd){
                dziel[j] = i;
                j++;
            }
            else{
                dziel[j] = i;
                j++;
                dziel[j] = nwd/i;
                j++;
            }
        }
    }
    sort(dziel, dziel + j);
    for(int i = 1; i <= n; i++){
        cin >> pocz >> kon;
        wyn = BinSearch(0, j, kon);
        if(wyn >= pocz && wyn <= kon){
            cout << wyn << '\n';
            continue;
        }
        cout << -1 << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int dziel[10007];

int NWD(int a, int b){
    int x;
    while(b != 0){
        x = b;
        b = a % b;
        a = x;
    }
    return a;
}

int BinSearch(int pocz, int kon, int naj){
    int sro;
    while(pocz < kon){
        sro = (pocz + kon + 1) / 2;
        if(dziel[sro] < naj){
            pocz = sro + 1;
        }
        else{
            kon = sro;
        }
    }
    return dziel[sro];
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, a, b, pocz, kon, wyn, pier;
    cin >> a >> b >> n;
    int nwd = NWD(a, b);
    pier = sqrt(nwd);
    int j = 0;
    for(int i = 1; i <= pier; i++){
        if(nwd % i == 0){
            if(i * i == nwd){
                dziel[j] = i;
                j++;
            }
            else{
                dziel[j] = i;
                j++;
                dziel[j] = nwd/i;
                j++;
            }
        }
    }
    sort(dziel, dziel + j);
    for(int i = 1; i <= n; i++){
        cin >> pocz >> kon;
        wyn = BinSearch(0, j, kon);
        if(wyn >= pocz && wyn <= kon){
            cout << wyn << '\n';
            continue;
        }
        cout << -1 << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int dziel[10007];

int NWD(int a, int b){
    int x;
    while(b != 0){
        x = b;
        b = a % b;
        a = x;
    }
    return a;
}

int BinSearch(int pocz, int kon, int naj){
    int sro;
    while(pocz < kon){
        sro = (pocz + kon + 1) / 2;
        if(dziel[sro] < naj){
            pocz = sro + 1;
        }
        else{
            kon = sro;
        }
    }
    return dziel[pocz];
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, a, b, pocz, kon, wyn, pier;
    cin >> a >> b >> n;
    int nwd = NWD(a, b);
    pier = sqrt(nwd);
    int j = 0;
    for(int i = 1; i <= pier; i++){
        if(nwd % i == 0){
            if(i * i == nwd){
                dziel[j] = i;
                j++;
            }
            else{
                dziel[j] = i;
                j++;
                dziel[j] = nwd/i;
                j++;
            }
        }
    }
    sort(dziel, dziel + j);
    for(int i = 1; i <= n; i++){
        cin >> pocz >> kon;
        wyn = BinSearch(0, j, kon);
        if(wyn >= pocz && wyn <= kon){
            cout << wyn << '\n';
            continue;
        }
        cout << -1 << '\n';
    }
    return 0;
}


    #include <bits/stdc++.h>
    using namespace std;

    int dziel[10007];

    int NWD(int a, int b){
        int x;
        while(b != 0){
            x = b;
            b = a % b;
            a = x;
        }
        return a;
    }

    int BinSearch(int pocz, int kon, int naj){
        int sro;
        while(pocz < kon){
            sro = (pocz + kon + 1) / 2;
            if(dziel[sro] <= naj){
                pocz = sro;
            }
            else{
                kon = sro - 1;
            }
        }
        return dziel[pocz];
    }

    int main(){
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
        int n, a, b, pocz, kon, wyn, pier;
        cin >> a >> b >> n;
        int nwd = NWD(a, b);
        pier = sqrt(nwd);
        int j = 0;
        for(int i = 1; i <= pier; i++){
            if(nwd % i == 0){
                if(i * i == nwd){
                    dziel[j] = i;
                    j++;
                }
                else{
                    dziel[j] = i;
                    j++;
                    dziel[j] = nwd/i;
                    j++;
                }
            }
        }
        sort(dziel, dziel + j);
        for(int i = 1; i <= n; i++){
            cin >> pocz >> kon;
            wyn = BinSearch(0, j - 1, kon);
            if(wyn >= pocz && wyn <= kon){
                cout << wyn << '\n';
                continue;
            }
            cout << -1 << '\n';
        }
        return 0;
    }


#include <bits/stdc++.h>
using namespace std;

int dziel[10007];

int NWD(int a, int b){
    int x;
    while(b != 0){
        x = b;
        b = a % b;
        a = x;
    }
    return a;
}

int BinSearch(int pocz, int kon, int naj){
    int sro;
    while(pocz <= kon){
        sro = (pocz + kon + 1) / 2;
        if(dziel[sro] < naj){
            pocz = sro + 1;
        }
        else{
            kon = sro - 1;
        }
    }
    return dziel[pocz];
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, a, b, pocz, kon, wyn, pier;
    cin >> a >> b >> n;
    int nwd = NWD(a, b);
    pier = sqrt(nwd);
    int j = 0;
    for(int i = 1; i <= pier; i++){
        if(nwd % i == 0){
            if(i * i == nwd){
                dziel[j] = i;
                j++;
            }
            else{
                dziel[j] = i;
                j++;
                dziel[j] = nwd/i;
                j++;
            }
        }
    }
    sort(dziel, dziel + j);
    for(int i = 1; i <= n; i++){
        cin >> pocz >> kon;
        wyn = BinSearch(0, j - 1, kon);
        if(wyn >= pocz && wyn <= kon){
            cout << wyn << '\n';
            continue;
        }
        cout << -1 << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int dziel[10007];

int NWD(int a, int b){
    int x;
    while(b != 0){
        x = b;
        b = a % b;
        a = x;
    }
    return a;
}

int BinSearch(int pocz, int kon, int naj){
    int sro;
    while(pocz <= kon){
        sro = (pocz + kon + 1) / 2;
        if(dziel[sro] < naj){
            pocz = sro + 1;
        }
        else{
            kon = sro;
        }
    }
    return dziel[pocz];
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, a, b, pocz, kon, wyn, pier;
    cin >> a >> b >> n;
    int nwd = NWD(a, b);
    pier = sqrt(nwd);
    int j = 0;
    for(int i = 1; i <= pier; i++){
        if(nwd % i == 0){
            if(i * i == nwd){
                dziel[j] = i;
                j++;
            }
            else{
                dziel[j] = i;
                j++;
                dziel[j] = nwd/i;
                j++;
            }
        }
    }
    sort(dziel, dziel + j);
    for(int i = 1; i <= n; i++){
        cin >> pocz >> kon;
        wyn = BinSearch(0, j - 1, kon);
        if(wyn >= pocz && wyn <= kon){
            cout << wyn << '\n';
            continue;
        }
        cout << -1 << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;

#define f(i,a,b) for(int i = a;i<b;i++)
#define rep(i,a,b) for(int i = a;i<=b;i++)
#define fd(i,a,b) for(int i = a;i>b;i--)
#define repd(i,a,b) for(int i = a;i>=b;i--)

#define pii pair<int,int>
#define pll pair<ll,ll>

#define fi first
#define se second
#define pb push_back
#define sz(x) (int) (x).size()
#define all(x) x.begin(),x.end()
#define endl '\n'
using namespace std;
const int mod = 1000000007;
const int maxn = 2e5;
set<int> factors(int n){
     set<int> s;
     int rootn = sqrt(n);
     rep(i,1,rootn){
          if(n%i==0){
               s.insert(i);
               s.insert(n/i);
          }
     }
     return s;
}
int main(){
     int a,b;
     cin>>a>>b;
     set<int> a1 = factors(a);
     set<int> b1 = factors(b);
     vector<int> ans;
     set_intersection(all(a1),all(b1),back_inserter(ans));
     sort(all(ans));
     int n;cin>>n;
     int low,high;
     f(i,0,n){
          cin>>low>>high;
          auto it1 = lower_bound(all(ans),low);
          auto it2 = upper_bound(all(ans),high);
          if(it1 == ans.end()){
               cout<<-1<<endl;
               continue;
          }
          if(it2 == ans.begin()){
               cout<<-1<<endl;
               continue;
          }
          --it2;
          int idx2 = it2 - ans.begin();
          cout<<ans[idx2]<endl;
     }

}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;

#define f(i,a,b) for(int i = a;i<b;i++)
#define rep(i,a,b) for(int i = a;i<=b;i++)
#define fd(i,a,b) for(int i = a;i>b;i--)
#define repd(i,a,b) for(int i = a;i>=b;i--)

#define pii pair<int,int>
#define pll pair<ll,ll>

#define fi first
#define se second
#define pb push_back
#define sz(x) (int) (x).size()
#define all(x) x.begin(),x.end()
#define endl '\n'
using namespace std;
const int mod = 1000000007;
const int maxn = 2e5;
set<int> factors(int n){
     set<int> s;
     int rootn = sqrt(n);
     rep(i,1,rootn){
          if(n%i==0){
               s.insert(i);
               s.insert(n/i);
          }
     }
     return s;
}
int main(){
     int a,b;
     cin>>a>>b;
     set<int> a1 = factors(a);
     set<int> b1 = factors(b);
     vector<int> ans;
     set_intersection(all(a1),all(b1),back_inserter(ans.begin()));
     sort(all(ans));
     int n;cin>>n;
     int low,high;
     f(i,0,n){
          cin>>low>>high;
          auto it1 = lower_bound(all(ans),low);
          auto it2 = upper_bound(all(ans),high);
          if(it1 == ans.end()){
               cout<<-1<<endl;
               continue;
          }
          if(it2 == ans.begin()){
               cout<<-1<<endl;
               continue;
          }
          int idx1 = it1 - ans.begin();
          --it2;
          int idx2 = it2 - ans.begin();
          cout<<idx2 - idx1 + 1<<endl;
     }

}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;

#define f(i,a,b) for(int i = a;i<b;i++)
#define rep(i,a,b) for(int i = a;i<=b;i++)
#define fd(i,a,b) for(int i = a;i>b;i--)
#define repd(i,a,b) for(int i = a;i>=b;i--)

#define pii pair<int,int>
#define pll pair<ll,ll>

#define fi first
#define se second
#define pb push_back
#define sz(x) (int) (x).size()
#define all(x) x.begin(),x.end()
#define endl '\n'
using namespace std;
const int mod = 1000000007;
const int maxn = 2e5;
set<int> factors(int n){
     set<int> s;
     int rootn = sqrt(n);
     rep(i,1,rootn){
          if(n%i==0){
               s.insert(i);
               s.insert(n/i);
          }
     }
     return s;
}
int main(){
     int a,b;
     cin>>a>>b;
     set<int> a1 = factors(a);
     set<int> b1 = factors(b);
     vector<int> ans;
     set_intersection(all(a1),all(b1),back_inserter(ans));
     sort(all(ans));
     int n;cin>>n;
     int low,high;
     f(i,0,n){
          cin>>low>>high;
          auto it1 = lower_bound(all(ans),low);
          auto it2 = upper_bound(all(ans),high);
          if(it1 == ans.end()){
               cout<<-1<<endl;
               continue;
          }
          if(it2 == ans.begin()){
               cout<<-1<<endl;
               continue;
          }
          --it2;
          int idx2 = it2 - ans.begin();
          if(ans[idx2] >= low)
               cout<<ans[idx2]<<endl;
          else
               cout<<-1<<endl;
     }

}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;

#define f(i,a,b) for(int i = a;i<b;i++)
#define rep(i,a,b) for(int i = a;i<=b;i++)
#define fd(i,a,b) for(int i = a;i>b;i--)
#define repd(i,a,b) for(int i = a;i>=b;i--)

#define pii pair<int,int>
#define pll pair<ll,ll>

#define fi first
#define se second
#define pb push_back
#define sz(x) (int) (x).size()
#define all(x) x.begin(),x.end()
#define endl '\n'
using namespace std;
const int mod = 1000000007;
const int maxn = 2e5;
set<int> factors(int n){
     set<int> s;
     int rootn = sqrt(n);
     rep(i,1,rootn){
          if(n%i==0){
               s.insert(i);
               s.insert(n/i);
          }
     }
     return s;
}
int main(){
     int a,b;
     cin>>a>>b;
     set<int> a1 = factors(a);
     set<int> b1 = factors(b);
     vector<int> ans;
     set_intersection(all(a1),all(b1),back_inserter(ans));
     sort(all(ans));
     int n;cin>>n;
     int low,high;
     f(i,0,n){
          cin>>low>>high;
          auto it1 = lower_bound(all(ans),low);
          auto it2 = upper_bound(all(ans),high);
          if(it1 == ans.end()){
               cout<<-1<<endl;
               continue;
          }
          if(it2 == ans.begin()){
               cout<<-1<<endl;
               continue;
          }
          int idx1 = it1 - ans.begin();
          --it2;
          int idx2 = it2 - ans.begin();
          cout<<idx2 - idx1 + 1<<endl;
     }

}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;

#define f(i,a,b) for(int i = a;i<b;i++)
#define rep(i,a,b) for(int i = a;i<=b;i++)
#define fd(i,a,b) for(int i = a;i>b;i--)
#define repd(i,a,b) for(int i = a;i>=b;i--)

#define pii pair<int,int>
#define pll pair<ll,ll>

#define fi first
#define se second
#define pb push_back
#define sz(x) (int) (x).size()
#define all(x) x.begin(),x.end()
#define endl '\n'
using namespace std;
const int mod = 1000000007;
const int maxn = 2e5;
set<int> factors(int n){
     set<int> s;
     int rootn = sqrt(n);
     rep(i,1,rootn){
          if(n%i==0){
               s.insert(i);
               s.insert(n/i);
          }
     }
     return s;
}
int main(){
     int a,b;
     cin>>a>>b;
     set<int> a1 = factors(a);
     set<int> b1 = factors(b);
     vector<int> ans;
     set_intersection(all(a1),all(b1),back_inserter(ans.begin()));
     sort(all(ans));
     int n;cin>>n;
     f(i,0,n){
          cin>>low>>high;
          auto it1 = lower_bound(all(ans),low);
          auto it2 = upper_bound(all(ans),high);
          if(it1 == ans.end()){
               cout<<-1<<endl;
               continue;
          }
          if(it2 == ans.begin()){
               cout<<-1<<endl;
               continue;
          }
          int idx1 = it1 - ans.begin();
          --it2;
          int idx2 = it2 - ans.begin();
          cout<<idx2 - idx1 + 1<<endl;
     }

}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;

#define f(i,a,b) for(int i = a;i<b;i++)
#define rep(i,a,b) for(int i = a;i<=b;i++)
#define fd(i,a,b) for(int i = a;i>b;i--)
#define repd(i,a,b) for(int i = a;i>=b;i--)

#define pii pair<int,int>
#define pll pair<ll,ll>

#define fi first
#define se second
#define pb push_back
#define sz(x) (int) (x).size()
#define all(x) x.begin(),x.end()
#define endl '\n'
using namespace std;
const int mod = 1000000007;
const int maxn = 2e5;
set<int> factors(int n){
     set<int> s;
     int rootn = sqrt(n);
     rep(i,1,rootn){
          if(n%i==0){
               s.insert(i);
               s.insert(n/i);
          }
     }
     return s;
}
int main(){
     int a,b;
     cin>>a>>b;
     set<int> a1 = factors(a);
     set<int> b1 = factors(b);
     vector<int> ans;
     set_intersection(all(a1),all(b1),back_inserter(ans));
     sort(all(ans));
     int n;cin>>n;
     int low,high;
     f(i,0,n){
          cin>>low>>high;
          auto it1 = lower_bound(all(ans),low);
          auto it2 = upper_bound(all(ans),high);
          if(it1 == ans.end()){
               cout<<-1<<endl;
               continue;
          }
          if(it2 == ans.begin()){
               cout<<-1<<endl;
               continue;
          }
          --it2;
          int idx2 = it2 - ans.begin();
          cout<<ans[idx2]<<endl;
     }

}


/* So, lets start coding

                       _oo0oo_
                      o8888888o
                      88" . "88
                      (| -_- |)
                      0\  =  /0
                    ___/`---'\___
                  .' \\|     |// '.
                 / \\|||  :  |||// \
                / _||||| -:- |||||- \
               |   | \\\  -  /// |   |
               | \_|  ''\---/''  |_/ |
               \  .-\__  '-'  ___/-. /
             ___'. .'  /--.--\  `. .'___
          ."" '<  `.___\_<|>_/___.' >' "".
         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
         \  \ `_.   \_ __\ /__ _/   .-` /  /
     =====`-.____`.___ \_____/___.-`___.-'=====
                       `=---='

 Author: Anuj Jhawar
   University: DAI-ICT, Gandhinagar, India.
   ID: fucking_noob */

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll loops")

#include<bits/stdc++.h>
using namespace std;

const long long int  MOD = 1e9 + 7;
const long long int INF = 1e18;
#define pb push_back
#define ff first
#define ss second
#define f(i,a,b) for(long long int i=a;i<b;i++)
#define ll long long int
#define srt(a) sort(a.begin(),a.end())
#define srtc(a,c) sort(a.begin(),a.end(),c);
#define fi(i,a,b) for(long long int i = a;i>=b;i--)
#define vectori vector<long long int>
#define vectorc vector<char>
#define ma(v) max_element(v.begin(),v.end())
#define mi(v) min_element(v.begin(),v.end())
#define rev(s) reverse(s.begin(),s.end())
#define up(v,x) upper_bound(v.begin(),v.end(),x)
#define lo(v,x) lower_bound(v.begin(),v.end(),x)
#define endl "\n"

int main() {

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll a,b;
	cin>>a>>b;
	ll t;
	cin>>t;
	while(t--)
	{
	   ll low,high;
	   cin>>low>>high;
	   ll g = __gcd(a,b);
	   if(g>=low&&g<=high)
	   {
	       cout<<g<<endl;
	       continue;
	   }
	   ll ans = -1;
	   for(ll i = min(high,g);i>=low&&i>=(ll)sqrt(g);i--)
	   {
	       if(g%i==0)
	       {
	           ans = i;
	           break;
	       }
	   }
	   cout<<ans<<endl;
	}
 	return 0;
}