#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#define CL(x) memset(x,0,sizeof(x))
#define rep(i,s,t) for(int i=(s);i<=(t);i++)
#define dow(i,s,t) for(int i=(s);i>=(t);i--)
using namespace std;
typedef long long LL;
LL read(){
    LL ret=0,f=1;
    char x=getchar();
    while(!(x>='0' && x<='9')){if(x=='-')f=-1;x=getchar();}
    while(x>='0' && x<='9') ret=ret*10+x-'0', x=getchar();
    return ret*f;
}
char get_char(){
    char x=getchar();
    while(!((x>='a' && x<='z')||(x>='A' && x<='Z'))) x=getchar();
    return x;
}

const int MN=1e6+10;
int N,A,B;
int is_d[MN];
int p[MN],pn;
int find(int n){
	int l=1,r=pn;
	while(l<r){
		int mid=(l+r)/2;
		if(p[mid]==n) return n;
		else if(p[mid]<n) l=mid+1;
		else r=mid;
	}
	if(p[l]>n) l--;
	if(l<1) return -1;
	else return p[l];
}
int main(){
	//freopen("in.txt","r",stdin);
	A=read(), B=read(), N=read();
	rep(i,1,N){
		int a=read(),b=read();
		bool ok=false;
		dow(j,b,a) if(A%j==0 && B%j==0){
			printf("%d\n",j);
			ok=true;
			break;
		}
		if(!ok) printf("%d\n",-1);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#define CL(x) memset(x,0,sizeof(x))
#define rep(i,s,t) for(int i=(s);i<=(t);i++)
#define dow(i,s,t) for(int i=(s);i>=(t);i--)
using namespace std;
typedef long long LL;
LL read(){
    LL ret=0,f=1;
    char x=getchar();
    while(!(x>='0' && x<='9')){if(x=='-')f=-1;x=getchar();}
    while(x>='0' && x<='9') ret=ret*10+x-'0', x=getchar();
    return ret*f;
}
char get_char(){
    char x=getchar();
    while(!((x>='a' && x<='z')||(x>='A' && x<='Z'))) x=getchar();
    return x;
}

const int MN=1e5+10;
int N,A,B;
int is_d[MN];
int p[MN],pn;
int find(int n){
    int l=1,r=pn;
    while(l<r){
        int mid=(l+r)/2;
        if(p[mid]==n) return n;
        else if(p[mid]<n) l=mid+1;
        else r=mid;
    }
    if(p[l]>n) l--;
    if(l<1) return -1;
    else return p[l];
}
int main(){
    //freopen("in.txt","r",stdin);
    A=read(), B=read(), N=read();
    int m=sqrt(min(A,B))+2;
    rep(i,2,m) if(A%i==0 && B%i==0)
        p[++pn]=i;
    if(B%A==0) p[++pn]=A;
    sort(p,p+pn);
    rep(i,1,N){
        int a=read(), b=read();
        int n=find(b);
        if(n<a) n=-1;
        printf("%d\n",n);
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#define CL(x) memset(x,0,sizeof(x))
#define rep(i,s,t) for(int i=(s);i<=(t);i++)
#define dow(i,s,t) for(int i=(s);i>=(t);i--)
using namespace std;
typedef long long LL;
LL read(){
    LL ret=0,f=1;
    char x=getchar();
    while(!(x>='0' && x<='9')){if(x=='-')f=-1;x=getchar();}
    while(x>='0' && x<='9') ret=ret*10+x-'0', x=getchar();
    return ret*f;
}
char get_char(){
    char x=getchar();
    while(!((x>='a' && x<='z')||(x>='A' && x<='Z'))) x=getchar();
    return x;
}

const int MN=1e6+10;
int N,A,B;
int is_d[MN];
int p[MN],pn;
int find(int n){
    int l=1,r=pn;
    while(l<r){
        int mid=(l+r)/2;
        if(p[mid]==n) return n;
        else if(p[mid]<n) l=mid+1;
        else r=mid;
    }
    if(p[l]>n) l--;
    if(l<1) return -1;
    else return p[l];
}
void check(int num){
    if(A%num==0 && B%num==0) p[++pn]=num;
}
int main(){
   // freopen("in.txt","r",stdin);
    A=read(), B=read(), N=read();
    int m=sqrt(min(A,B))+2;
    rep(i,2,m) if(A%i==0 && B%i==0)
        check(i),check(A/i),check(B/i);
    check(A);

    sort(p,p+pn);
    rep(i,1,N){
        int a=read(), b=read();
        int n=find(b);
        if(n<a) n=-1;
        printf("%d\n",n);
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#define CL(x) memset(x,0,sizeof(x))
#define rep(i,s,t) for(int i=(s);i<=(t);i++)
#define dow(i,s,t) for(int i=(s);i>=(t);i--)
using namespace std;
typedef long long LL;
LL read(){
    LL ret=0,f=1;
    char x=getchar();
    while(!(x>='0' && x<='9')){if(x=='-')f=-1;x=getchar();}
    while(x>='0' && x<='9') ret=ret*10+x-'0', x=getchar();
    return ret*f;
}
char get_char(){
    char x=getchar();
    while(!((x>='a' && x<='z')||(x>='A' && x<='Z'))) x=getchar();
    return x;
}

const int MN=1e6+10;
int N,A,B;
int is_d[MN];
int p[MN],pn;
int find(int n){
	int l=1,r=pn;
	while(l<r){
		int mid=(l+r)/2;
		if(p[mid]==n) return n;
		else if(p[mid]<n) l=mid+1;
		else r=mid;
	}
	if(p[l]>n) l--;
	if(l<1) return -1;
	else return p[l];
}
void check(int num){
	if(A%num==0 && B%num==0) p[++pn]=num;
}
int main(){
	//freopen("in.txt","r",stdin);
	A=read(), B=read(), N=read();
	int m=sqrt(min(A,B))+2;
	rep(i,1,m) check(i),check(A/i),check(B/i);

	sort(p+1,p+1+pn);
	rep(i,1,N){
		int a=read(), b=read();
		int n=find(b);
		if(n<a) n=-1;
		printf("%d\n",n);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#define CL(x) memset(x,0,sizeof(x))
#define rep(i,s,t) for(int i=(s);i<=(t);i++)
#define dow(i,s,t) for(int i=(s);i>=(t);i--)
using namespace std;
typedef long long LL;
LL read(){
    LL ret=0,f=1;
    char x=getchar();
    while(!(x>='0' && x<='9')){if(x=='-')f=-1;x=getchar();}
    while(x>='0' && x<='9') ret=ret*10+x-'0', x=getchar();
    return ret*f;
}
char get_char(){
    char x=getchar();
    while(!((x>='a' && x<='z')||(x>='A' && x<='Z'))) x=getchar();
    return x;
}

const int MN=1e6+10;
int N,A,B;
int is_d[MN];
int p[MN],pn;
int find(int n){
	int l=1,r=pn;
	while(l<r){
		int mid=(l+r)/2;
		if(p[mid]==n) return n;
		else if(p[mid]<n) l=mid+1;
		else r=mid;
	}
	if(p[l]>n) l--;
	if(l<1) return -1;
	else return p[l];
}
void check(int num){
	if(A%num==0 && B%num==0) p[++pn]=num;
}
int main(){
	//freopen("in.txt","r",stdin);
	A=read(), B=read(), N=read();
	int m=sqrt(min(A,B))+2;
	rep(i,1,m) if(A%i==0 && B%i==0)
		check(i),check(A/i),check(B/i);

	sort(p+1,p+1+pn);
	rep(i,1,N){
		int a=read(), b=read();
		int n=find(b);
		if(n<a) n=-1;
		printf("%d\n",n);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#define CL(x) memset(x,0,sizeof(x))
#define rep(i,s,t) for(int i=(s);i<=(t);i++)
#define dow(i,s,t) for(int i=(s);i>=(t);i--)
using namespace std;
typedef long long LL;
LL read(){
    LL ret=0,f=1;
    char x=getchar();
    while(!(x>='0' && x<='9')){if(x=='-')f=-1;x=getchar();}
    while(x>='0' && x<='9') ret=ret*10+x-'0', x=getchar();
    return ret*f;
}
char get_char(){
    char x=getchar();
    while(!((x>='a' && x<='z')||(x>='A' && x<='Z'))) x=getchar();
    return x;
}

const int MN=1e6+10;
int N,A,B;
int is_d[MN];
int p[MN],pn;
int find(int n){
    int l=1,r=pn;
    while(l<r){
        int mid=(l+r)/2;
        if(p[mid]==n) return n;
        else if(p[mid]<n) l=mid+1;
        else r=mid;
    }
    if(p[l]>n) l--;
    if(l<1) return -1;
    else return p[l];
}
void check(int num){
    if(A%num==0 && B%num==0) p[++pn]=num;
}
int main(){
    //freopen("in.txt","r",stdin);
    A=read(), B=read(), N=read();
    int m=sqrt(min(A,B))+2;
    rep(i,1,m) if(A%i==0 && B%i==0)
        check(i),check(A/i),check(B/i);
    check(A);

    sort(p,p+pn);
    rep(i,1,N){
        int a=read(), b=read();
        int n=find(b);
        if(n<a) n=-1;
        printf("%d\n",n);
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#define CL(x) memset(x,0,sizeof(x))
#define rep(i,s,t) for(int i=(s);i<=(t);i++)
#define dow(i,s,t) for(int i=(s);i>=(t);i--)
using namespace std;
typedef long long LL;
LL read(){
    LL ret=0,f=1;
    char x=getchar();
    while(!(x>='0' && x<='9')){if(x=='-')f=-1;x=getchar();}
    while(x>='0' && x<='9') ret=ret*10+x-'0', x=getchar();
    return ret*f;
}
char get_char(){
    char x=getchar();
    while(!((x>='a' && x<='z')||(x>='A' && x<='Z'))) x=getchar();
    return x;
}

const int MN=1e6+10;
int N,A,B;
int is_d[MN];
int p[MN],pn;
int find(int n){
    int l=1,r=pn;
    while(l<r){
        int mid=(l+r)/2;
        if(p[mid]==n) return n;
        else if(p[mid]<n) l=mid+1;
        else r=mid;
    }
    if(p[l]>n) l--;
    if(l<1) return -1;
    else return p[l];
}
int main(){
    //freopen("in.txt","r",stdin);
    A=read(), B=read(), N=read();
    rep(i,1,N){
        int a=read(),b=read();
        bool ok=false;
        dow(j,b,a) if(A%j==0 && B%j==0){
            printf("%d\n",j);
            ok=true;
            break;
        }
        if(!ok) printf("%d\n",-1);
    }
    return 0;
}


#include<cstdio>
#include<vector>
#include<iostream>

using namespace std;

int n;
long long l,h,a,b;

vector<long long>l1,h1;

void pr() {

    int d=2;
    while(l>1) {
        if(l%d==0) {
            l/=d;
            l1.push_back(d);
        }
        else d++;
    }

    d=2;
    while(h>1) {
        if(h%d==0) {
            h/=d;
            h1.push_back(d);
        }
        else d++;
    }

}

bool check() {

    int i,j;

    for(i=0;i<h1.size();i++) {
        int l=0;
        for(j=0;j<l1.size();j++)
        if(h1[i]==l1[j]) { l=1; break; }
        if(l==0) return 1;
    }

    return 0;

}

long long solve() {

    int i,j;
    long long m=1<<(l1.size()-1);

    for(i=0;i<m;i++) {
        long long pr=1;
        for(j=0;j<l1.size();j++)
        if(((1<<j)&(i))==0) pr*=l1[j];
        if(pr>=a&&pr<=b) return pr;
    }

    return -1;

}

void read() {

    int l1=0;

    cin>>l>>h;
    cin>>n;
    pr();
    if(check()) l1=1;

    for(int i=0;i<n;i++) {
        scanf("%lld%lld",&a,&b);
        if(l1==1) printf("-1\n");
        else printf("%lld\n",solve());
    }

}

int main() {

    read();

    return 0;

}


#include<cstdio>
#include<vector>
#include<iostream>

using namespace std;

int n;
long long l,h,a,b;

vector<long long>l1,h1;

void pr() {

    int d=2;
    while(l>1) {
        if(l%d==0) {
            l/=d;
            l1.push_back(d);
        }
        else d++;
    }

    d=2;
    while(h>1) {
        if(h%d==0) {
            h/=d;
            h1.push_back(d);
        }
        else d++;
    }

}

bool check() {

    int i,j;

    for(i=0;i<l1.size();i++) {
        int l=0;
        for(j=0;j<h1.size();j++)
        if(l1[i]==h1[j]) { l=1; break; }
        if(l==0) return 1;
    }

    return 0;

}

long long solve() {

    int i,j;
    long long m=2*(1<<(l1.size()-1));

    for(i=0;i<m;i++) {
        long long pr=1;
        for(j=0;j<l1.size();j++)
        if(((1<<j)&(i))==0) pr*=l1[j];
        if(pr>=a&&pr<=b) return pr;
    }

    return -1;

}

void read() {

    int l1=0;

    cin>>l>>h;
    cin>>n;
    pr();
    //if(check()) l1=1;

    for(int i=0;i<n;i++) {
        scanf("%lld%lld",&a,&b);
        //if(l1==1) printf("-1\n");
         printf("%lld\n",solve());
    }

}

int main() {

    read();

    return 0;

}


#include<cstdio>
#include<vector>

using namespace std;

vector<int>v;

int gcd(int a,int b) {

    while(1) {
        a=a%b;
        if(a==0) return b;
        b=b%a;
        if(b==0) return a;
    }

}

void solve(int g) {

    int i;

    for(i=2;i*i<=g;i++) {
        if(g%i==0) v.push_back(i);
    }

    int n=v.size();
    for(i=0;i<n;i++) {
        v.push_back(g/v[i]);
    }

    v.push_back(g);

}

void read() {

    int i,j,l,h,n,a,b,g;

    scanf("%d%d%d",&l,&h,&n);

    g=gcd(l,h);

    solve(g);

    for(i=0;i<n;i++) {

        scanf("%d%d",&a,&b);

        int l=0;
        for(j=0;j<v.size();j++)
        if(v[i]>=a&&v[i]<=b) {
            printf("%d\n",v[i]);
            l=1;
            break;
        }

        if(l==0) printf("-1\n");

    }

}

int main() {

    read();

    return 0;

}


#include<cstdio>
#include<vector>
#include<iostream>

using namespace std;

int n;
long long l,h,a,b;

vector<long long>l1,h1;

void pr() {

    int d=2;
    while(l>1) {
        if(l%d==0) {
            l/=d;
            l1.push_back(d);
        }
        else d++;
    }

    d=2;
    while(h>1) {
        if(h%d==0) {
            h/=d;
            h1.push_back(d);
        }
        else d++;
    }

}

long long solve() {

    int i,j;
    long long m=2*(1<<(l1.size()-1));
    long long min=0;

    for(i=0;i<m;i++) {
        long long pr=1;
        for(j=0;j<l1.size();j++)
        if(((1<<j)&(i))==0) pr*=l1[j];
        if(pr>=a&&pr<=b&&pr>1) {
            if(pr>min) min=pr;
        }
    }

    if(min==0) return -1;
    return min;

}

void read() {

    cin>>l>>h>>n;

    pr();

    int l1=0;
    if(h%l!=0) l1=1;

    for(int i=0;i<n;i++) {
        scanf("%lld%lld",&a,&b);
        if(l1==1) printf("-1\n");
        else printf("%lld\n",solve());
    }

}

int main() {

    read();

    return 0;

}


#include<cstdio>
#include<vector>
#include<iostream>

using namespace std;

int n;
long long l,h,a,b;

vector<long long>l1,h1;

void pr() {

    int d=2;
    while(l>1) {
        if(l%d==0) {
            l/=d;
            l1.push_back(d);
        }
        else d++;
    }

    d=2;
    while(h>1) {
        if(h%d==0) {
            h/=d;
            h1.push_back(d);
        }
        else d++;
    }

}

bool check() {

    int i,j;

    for(i=0;i<l1.size();i++) {
        int l=0;
        for(j=0;j<h1.size();j++)
        if(l1[i]==h1[j]) { l=1; break; }
        if(l==0) return 1;
    }

    return 0;

}

long long solve() {

    int i,j;
    long long m=2*(1<<(l1.size()-1));

    for(i=0;i<m;i++) {
        long long pr=1;
        for(j=0;j<l1.size();j++)
        if(((1<<j)&(i))==0) pr*=l1[j];
        if(pr>=a&&pr<=b) return pr;
    }

    return -1;

}

void read() {

    int l1=0;

    cin>>l>>h;
    cin>>n;
    pr();
    if(check()) l1=1;

    for(int i=0;i<n;i++) {
        scanf("%lld%lld",&a,&b);
        if(l1==1) printf("-1\n");
        else printf("%lld\n",solve());
    }

}

int main() {

    read();

    return 0;

}


#include<cstdio>
#include<vector>

using namespace std;

vector<int>v;

int gcd(int a,int b) {

    while(1) {
        a=a%b;
        if(a==0) return b;
        b=b%a;
        if(b==0) return a;
    }

}

void solve(int g) {

    int i;

    for(i=2;i*i<=g;i++) {
        if(g%i==0) v.push_back(i);
    }

    int n=v.size();
    for(i=0;i<n;i++) {
        v.push_back(g/v[i]);
    }

    v.push_back(g);

}

void read() {

    int i,j,l,h,n,a,b,g;

    scanf("%d%d%d",&l,&h,&n);

    g=gcd(l,h);

    solve(g);

    for(i=0;i<n;i++) {

        scanf("%d%d",&a,&b);

        int l=0;
        for(j=0;j<v.size();j++)
        if(v[j]>=a&&v[j]<=b) {
            printf("%d\n",v[j]);
            l=1;
            break;
        }

        if(l==0) printf("-1\n");

    }

}

int main() {

    read();

    return 0;

}


#include<cstdio>
#include<vector>
#include<iostream>

using namespace std;

int n;
long long l,h,a,b;

vector<long long>l1,h1;

void pr() {

    int d=2;
    while(l>1) {
        if(l%d==0) {
            l/=d;
            l1.push_back(d);
        }
        else d++;
    }

    d=2;
    while(h>1) {
        if(h%d==0) {
            h/=d;
            h1.push_back(d);
        }
        else d++;
    }

}

bool check() {

    int i,j;

    for(i=0;i<l1.size();i++) {
        int l=0;
        for(j=0;j<h1.size();j++)
        if(l1[i]==h1[j]) { l=1; break; }
        if(l==0) return 1;
    }

    return 0;

}

long long solve() {

    int i,j;
    long long m=2*(1<<(l1.size()-1));
    for(i=0;i<l1.size();i++) {
        cout<<l1[i]<<" ";
    }
    cout<<endl;

    for(i=0;i<m;i++) {
        long long pr=1;
        for(j=0;j<l1.size();j++)
        if(((1<<j)&(i))==0) pr*=l1[j];
        if(pr>=a&&pr<=b) return pr;
    }

    return -1;

}

void read() {

    int l1=0;

    cin>>l>>h;
    cin>>n;
    pr();
    if(check()) l1=1;

    for(int i=0;i<n;i++) {
        scanf("%lld%lld",&a,&b);
        if(l1==1) printf("-1\n");
        else printf("%lld\n",solve());
    }

}

int main() {

    read();

    return 0;

}


#include<cstdio>
#include<vector>
#include<iostream>

using namespace std;

int n;
long long l,h,a,b;

vector<long long>l1,h1,g;

void pr() {

    long long d=2;
    while(l>1) {
        if(l%d==0) {
            l/=d;
            l1.push_back(d);
        }
        else d++;
    }

    d=2;
    while(h>1) {
        if(h%d==0) {
            h/=d;
            h1.push_back(d);
        }
        else d++;
    }

    int i,j;
    for(i=0;i<h1.size();i++) {
        for(j=0;j<l1.size();j++)
        if(h1[i]==l1[j]) { g.push_back(h1[i]); break; }
    }

}

long long solve() {

    int i,j;
    long long m=2*(1<<(g.size()-1));
    long long min=0;

    for(i=0;i<m;i++) {
        long long pr=1;
        for(j=0;j<g.size();j++)
        if(((1<<j)&(i))==0) pr*=g[j];
        if(pr>=a&&pr<=b&&pr>1) {
            if(pr>min) min=pr;
        }
    }

    if(min==0) return -1;
    return min;

}

void read() {

    cin>>l>>h>>n;

    pr();

    for(int i=0;i<n;i++) {
        scanf("%lld%lld",&a,&b);
        printf("%lld\n",solve());
    }

}

int main() {

    read();

    return 0;

}


#include<cstdio>
#include<vector>

using namespace std;

vector<int>v;

int gcd(int a,int b) {

    while(1) {
        a=a%b;
        if(a==0) return b;
        b=b%a;
        if(b==0) return a;
    }

}

void solve(int g) {

    int i;

    for(i=1;i*i<=g;i++) {
        if(g%i==0) v.push_back(i);
    }

    int n=v.size();
    for(i=0;i<n;i++) {
        v.push_back(g/v[i]);
    }

}

void read() {

    int i,j,l,h,n,a,b,g;

    scanf("%d%d%d",&l,&h,&n);

    g=gcd(l,h);

    solve(g);

    for(i=0;i<n;i++) {

        scanf("%d%d",&a,&b);

        int ans=0;
        for(j=0;j<v.size();j++)
        if(v[j]>=a&&v[j]<=b&&v[j]>ans) ans=v[j];

        if(ans==0) printf("-1\n");
        else printf("%d\n",ans);

    }

}

int main() {

    read();

    return 0;

}


#include<cstdio>
#include<vector>

using namespace std;

vector<int>v;

int gcd(int a,int b) {

    while(1) {
        a=a%b;
        if(a==0) return b;
        b=b%a;
        if(b==0) return a;
    }

}

void solve(int g) {

    int i;

    for(i=2;i*i<=g;i++) {
        if(g%i==0) v.push_back(i);
    }

    int n=v.size();
    for(i=0;i<n;i++) {
        v.push_back(g/v[i]);
    }

    v.push_back(g);

}

void read() {

    int i,j,l,h,n,a,b,g;

    scanf("%d%d%d",&l,&h,&n);

    g=gcd(l,h);

    solve(g);

    for(i=0;i<n;i++) {

        scanf("%d%d",&a,&b);

        int ans=0;
        for(j=0;j<v.size();j++)
        if(v[j]>=a&&v[j]<=b&&v[j]>ans) ans=v[j];

        if(ans==0) printf("-1\n");
        else printf("%d\n",ans);

    }

}

int main() {

    read();

    return 0;

}


#include<cstdio>
#include<vector>
#include<iostream>

using namespace std;

int n;
long long ll,h,a,b;

vector<long long>l1,h1,g;

void pr() {

    long long d=2,l=ll;
    while(l>1) {
        if(l%d==0) {
            l/=d;
            l1.push_back(d);
        }
        else d++;
    }

    d=2;
    while(h>1) {
        if(h%d==0) {
            h/=d;
            h1.push_back(d);
        }
        else d++;
    }

    int i=0,j=0;
    while(i<l1.size()&&j<h1.size()) {
        if(l1[i]>h1[j]) j++;
        if(l1[i]==h1[j]) {g.push_back(l1[i]); i++; j++; }
        else i++;
    }

}

long long solve() {

    int i,j;
    long long m=2*1<<(g.size()-1);
    long long min=0;
    for(i=0;i<m;i++) {
        long long pr=1;
        for(j=0;j<g.size();j++)
        if(((1<<j)&(i))==0) pr*=g[j];
        if(pr>=a&&pr<=b&&pr>1&&pr<=ll) {
            if(pr>min) min=pr;
            if(min==ll) return min;
        }
    }

    if(min==0) return -1;
    return min;

}

void read() {

    cin>>ll>>h>>n;

    pr();

    for(int i=0;i<n;i++) {
        scanf("%lld%lld",&a,&b);
        printf("%lld\n",solve());
    }

}

int main() {

    read();

    return 0;

}


#include<cstdio>
#include<vector>
#include<iostream>

using namespace std;

int n;
long long ll,h,a,b;

vector<long long>l1,h1,g;

void pr() {

    long long d=2,l=ll;
    while(l>1) {
        if(l%d==0) {
            l/=d;
            l1.push_back(d);
        }
        else d++;
    }

    d=2;
    while(h>1) {
        if(h%d==0) {
            h/=d;
            h1.push_back(d);
        }
        else d++;
    }

    int i=0,j=0;
    while(i<l1.size()&&j<h1.size()) {
        if(l1[i]>h1[j]) j++;
        if(l1[i]==h1[j]) {g.push_back(l1[i]); i++; j++; }
        else i++;
    }

}

long long solve() {

    int i,j;
    long long m=2*1<<(g.size()-1);
    long long min=0;
    for(i=0;i<m;i++) {
        long long pr=1;
        for(j=0;j<g.size();j++)
        if(((1<<j)&(i))==0) pr*=g[j];
        if(pr>=a&&pr<=b&&pr>1&&pr<=ll) {
            if(pr>min) min=pr;
        }
    }

    if(min==0) return -1;
    return min;

}

void read() {

    cin>>ll>>h>>n;

    pr();

    for(int i=0;i<n;i++) {
        scanf("%lld%lld",&a,&b);
        printf("%lld\n",solve());
    }

}

int main() {

    read();

    return 0;

}


#include<cstdio>
#include<vector>
#include<iostream>

using namespace std;

int n;
long long l,h,a,b;

vector<long long>l1,h1;

void pr() {

    l1.push_back(1);
    int d=2;
    while(l>1) {
        if(l%d==0) {
            l/=d;
            l1.push_back(d);
        }
        else d++;
    }

    h1.push_back(1);
    d=2;
    while(h>1) {
        if(h%d==0) {
            h/=d;
            h1.push_back(d);
        }
        else d++;
    }

}

bool check() {

    int i,j;

    for(i=0;i<l1.size();i++) {
        int l=0;
        for(j=0;j<h1.size();j++)
        if(l1[i]==h1[j]) { l=1; break; }
        if(l==0) return 1;
    }

    return 0;

}

long long solve() {

    int i,j;
    long long m=2*(1<<(l1.size()-1));

    for(i=0;i<m;i++) {
        long long pr=1;
        for(j=0;j<l1.size();j++)
        if(((1<<j)&(i))==0) pr*=l1[j];
        if(pr>=a&&pr<=b) return pr;
    }

    return -1;

}

void read() {

    int l1=0;

    cin>>l>>h;
    cin>>n;
    pr();
    if(check()) l1=1;

    for(int i=0;i<n;i++) {
        scanf("%lld%lld",&a,&b);
        if(l1==1) printf("-1\n");
        else printf("%lld\n",solve());
    }

}

int main() {

    read();

    return 0;

}


#include<cstdio>
#include<vector>
#include<iostream>

using namespace std;

int n;
long long ll,h,a,b;

vector<long long>l1,h1,g;

void pr() {

    long long d=2,l=ll;
    while(l>1) {
        if(l%d==0) {
            l/=d;
            l1.push_back(d);
        }
        else d++;
    }

    d=2;
    while(h>1) {
        if(h%d==0) {
            h/=d;
            h1.push_back(d);
        }
        else d++;
    }

    int i=0,j=0;
    while(i<l1.size()&&j<h1.size()) {
        if(l1[i]>h1[j]) j++;
        if(l1[i]==h1[j]) {g.push_back(l1[i]); i++; j++; }
        else i++;
    }

}

long long solve() {

    int i,j;
    long long m=2*(1<<(g.size()-1));
    long long min=0;
    for(i=0;i<m;i++) {
        long long pr=1;
        for(j=0;j<g.size();j++)
        if(((1<<j)&(i))==0) pr*=g[j];
        if(pr>=a&&pr<=b&&pr>1&&pr<=ll) {
            if(pr>min) min=pr;
        }
    }

    if(min==0) return -1;
    return min;

}

void read() {

    cin>>ll>>h>>n;

    pr();

    for(int i=0;i<n;i++) {
        scanf("%lld%lld",&a,&b);
        printf("%lld\n",solve());
    }

}

int main() {

    read();

    return 0;

}


#include<cstdio>
#include<vector>
#include<iostream>

using namespace std;

int n;
long long ll,h,a,b;

vector<long long>l1,h1,g;

void pr() {

    long long d=2,l=ll;
    while(l>1) {
        if(l%d==0) {
            l/=d;
            l1.push_back(d);
        }
        else d++;
    }

    d=2;
    while(h>1) {
        if(h%d==0) {
            h/=d;
            h1.push_back(d);
        }
        else d++;
    }

    int i=0,j=0;
    while(i<l1.size()&&j<h1.size()) {
        if(l1[i]>h1[j]) j++;
        if(l1[i]==h1[j]) {g.push_back(l1[i]); i++; j++; }
        else i++;
    }

}

long long solve() {

    int i,j;
    long long m=1<<(g.size()-1);
    long long min=0;
    for(i=0;i<m;i++) {
        long long pr=1;
        for(j=0;j<g.size();j++)
        if(((1<<j)&(i))==0) pr*=g[j];
        if(pr>=a&&pr<=b&&pr>1&&pr<=ll) {
            if(pr>min) min=pr;
        }
    }

    if(min==0) return -1;
    return min;

}

void read() {

    cin>>ll>>h>>n;

    pr();

    for(int i=0;i<n;i++) {
        scanf("%lld%lld",&a,&b);
        printf("%lld\n",solve());
    }

}

int main() {

    read();

    return 0;

}


#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
using namespace std;
int yz[50000],ans[20000];
bool cmp(int a,int b)
{
	return a>b;
}
int main()
{
	int i,j,k,a,b,n,l,h,m,mi,ma;
	while(~scanf("%d%d",&a,&b))
	{
		memset(yz,0,sizeof(yz));
		mi=min(a,b);
		ma=max(a,b);
		k=0;
		for(i=1;i<=sqrt(mi)+1;i++)
		{
			if(mi%i==0&&ma%i==0)
			{
				yz[k++]=i;
				m=mi/i;
				if(ma%m==0)
				yz[k++]=m;
			}
		}
		sort(yz,yz+k,cmp);
	//	for(i=0;i<k;i++)
	//	printf("aaaa  %d\n",yz[i]);
		scanf("%d",&n);

		for(i=0;i<n;i++)
		{
			int flag=1;
			scanf("%d%d",&a,&b);
			l=min(a,b);
			h=max(a,b);
	//		if(yz[0]<l)
	//		{
			//	ans[i]=-1;
	//		}
	//		else
			{
				for(j=0;j<k;j++)
				{
					if(yz[j]<l)
					break;
					if((yz[j]<=h)&&(yz[j]>=l))
					{
						ans[i]=yz[j];
						flag=0;
						break;
					}
				}
				if(flag)
				ans[i]=-1;

			}

		}
		for(i=0;i<n;i++)
		printf("%d\n",ans[i]);
	}
	return 0;
}


#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
using namespace std;
int yz[5000000],ans[20000];
bool cmp(int a,int b)
{
	return a>=b;
}
int main()
{
	int i,j,k,a,b,n,l,h,m,mi,ma;
	while(~scanf("%d%d",&a,&b))
	{
		memset(yz,0,sizeof(yz));
		mi=min(a,b);
		ma=max(a,b);
		k=0;
		for(i=1;i<=400000;i++)
		{
			if(mi%i==0&&ma%i==0)
			{
				yz[k++]=i;
				m=mi/i;
				if(ma%m==0)
				yz[k++]=m;
			}
		}
		sort(yz,yz+k,cmp);
	//	for(i=0;i<k;i++)
	//	printf("aaaa  %d\n",yz[i]);
		scanf("%d",&n);

		for(i=0;i<n;i++)
		{
			int flag=1;
			scanf("%d%d",&a,&b);
			l=min(a,b);
			h=max(a,b);
	//		if(yz[0]<l)
	//		{
			//	ans[i]=-1;
	//		}
	//		else
			{
				for(j=0;j<k;j++)
				{
					if(yz[j]<l)
					break;
					if((yz[j]<=h)&&(yz[j]>=l))
					{
						ans[i]=yz[j];
						flag=0;
						break;
					}
				}
				if(flag)
				ans[i]=-1;

			}

		}
		for(i=0;i<n;i++)
		printf("%d\n",ans[i]);
	}
	return 0;
}


/*
 * File:   main.cpp
 * Author: Administrator
 *
 * Created on 2012年3月21日, 下午2:56
 */

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
//#define out(x) (std::cerr << #x << ": " << (x) << std::endl)
#define out(...)
#define MP make_pair
using namespace std;

/*
 *
 */

template<typename _Tp>
inline bool Max(_Tp & a, const _Tp &b) {
	return (less<_Tp > ()(a, b)) ? a = b, true : false;
}

template<typename _Tp>
inline bool Min(_Tp & a, const _Tp &b) {
	return (!less<_Tp > ()(a, b)) ? a = b, true : false;
}

template<typename _Tp>_Tp mygcd(_Tp a, _Tp b) {
	if (!a)return b;
	while (b) {
		_Tp t = a % b;
		a = b;
		b = t;
	}
	return a;
}

int find(const vector<int>&v, int x) {
	int l = 0, r = v.size();
	while (l <= r) {
		int mid = (l + r) / 2;
		if (v[mid] > x)r = mid - 1;
		else l = mid + 1;
	}
	return l;
}

int main(int argc, char** argv) {
	int x, y;
	cin >> x >> y;
	int gd = mygcd(x, y);
	set<int>st;
	for (int i = 1; i <= gd / i; ++i) {
		if (gd % i)continue;
		st.insert(i);
		st.insert(gd / i);
	}
	vector<int>v;
	for (set<int>::iterator it = st.begin(); it != st.end(); ++it) {
		v.push_back(*it);
	}
	st.~set();
	int n = -1, a, b;
	cin >> n;
	while (n-- && cin >> a >> b) {
		if (a > b)swap(a, b);
		int fa = find(v, a - 1), fb = find(v, b);
		out(fa);
		out(fb);
		if (fa >= fb)printf("-1\n");
		else printf("%d\n", v[fb - 1]);
		//for (int i = fa; i < fb; ++i) {
		//printf("%d%c", v[i], i + 1 == fb ? '\n' : ' ');
		//}
		//cout << find(v, a - 1) << ' ' << find(v, b) << endl;
	}
	return EXIT_SUCCESS;
}


#include <stdio.h>

long Euclid(long a,long b)
{
	long c,d;
	long re;
	c=(a>=b)?a:b;
	b=(a>=b)?b:a;
	a=c;
	//printf("%d\n%d",a,b);
	if(a%b==0)
	//printf("a与b的最大公约数为%d\n",b);}
	{
		re=b;
		return re;
	}
	else
	{
		d=Euclid(b,(a%b));
	}
}

int main()
{
	long a,b;
	while(scanf("%ld%ld",&a,&b)!=EOF)
	{
		long GCD=Euclid(a,b);  //printf("GCD=%ld\n",GCD);
		long n;
		scanf("%ld",&n);
		while(n--)
		{
			long low,high;
			scanf("%ld%ld",&low,&high);
			long i,temp=-1;
			for(i=low;i<=high;i++)
			{
				if(i==GCD)
				{temp=i;break;}
			}
			printf("%ld\n",temp);
		}
	}
	return 0;
}


#include <stdio.h>

long Euclid(long a,long b)
{
	long c,d;
	long re;
	c=(a>=b)?a:b;
	b=(a>=b)?b:a;
	a=c;
	//printf("%d\n%d",a,b);
	if(a%b==0)
	//printf("a与b的最大公约数为%d\n",b);}
	{
		re=b;
		return re;
	}
	else
	{
		d=Euclid(b,(a%b));
	}
}

int main()
{
	long a,b;
	while(scanf("%ld%ld",&a,&b)!=EOF)
	{
		long GCD=Euclid(a,b);  //printf("GCD=%ld\n",GCD);
		long n;
		scanf("%ld",&n);
		while(n--)
		{
			long low,high;
			scanf("%ld%ld",&low,&high);
			long i,temp=-1;
			for(i=low;i<=GCD;i++)
			{
				if(i%a==0&&i%b==0)
				temp=i;

			}
			printf("%ld\n",temp);
		}
	}
	return 0;
}


#include<stdio.h>
#include<math.h>
int abs(int a)
{
	if(a<0)return -a;
	else return a;
}
int min(int a,int b)
{
	if(a<b)return a;
	else return b;
}
int kgcd(int a, int b)
{
    if (a == 0) return b;
    if (b == 0) return a;
    if (!(a & 1) && !(b & 1))
        return kgcd(a>>1, b>>1)<<1;
    else if (!(b & 1))
    return kgcd(a, b>>1);
    else if (!(a & 1)) return kgcd(a>>1, b);
    else return kgcd(abs(a - b), min(a, b));
}

int main()
{
	int a,b,x,y,t,i,n,j,c[10003],k;
	scanf("%d%d",&a,&b);
	if(a>b)
	{
		t=a;a=b;b=t;
	}
	j=2;c[0]=0;c[1]=1;
	t=kgcd(a,b);
	if(t%2==0){c[j]=2;j++;}
	for(i=3;i<=t/3;i++)
	{
		if(t%i==0)
		{c[j]=i;j++;}
	}
	if(t%2==0&&t!=4){c[j]=t/2;j++;}
	if(t!=1&&t!=2){c[j]=t;j++;}
	scanf("%d",&n);
	while(n--)
	{
		scanf("%d%d",&x,&y);
		for(i=j-1;c[i]>y&&c[i]!=0;i--);
		if(c[i]>=x)printf("%d\n",c[i]);
		else printf("-1\n");
	}
	return 0;
}


#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
using namespace std;
int yz[5000000],ans[20000];
bool cmp(int a,int b)
{
	return a>=b;
}
int main()
{
	int i,j,k,a,b,n,l,h,m,mi,ma;
	while(~scanf("%d%d",&a,&b))
	{
		memset(yz,0,sizeof(yz));
		mi=min(a,b);
		ma=max(a,b);
		k=0;
		for(i=1;i<=sqrt(a);i++)
		{
			if(a%i==0&&b%i==0)
			{
				yz[k++]=i;
				m=a/i;
				if(b%m==0)
				yz[k++]=m;
			}
		}
		sort(yz,yz+k,cmp);
	//	for(i=0;i<k;i++)
	//	printf("aaaa  %d\n",yz[i]);
		scanf("%d",&n);

		for(i=0;i<n;i++)
		{
			int flag=1;
			scanf("%d%d",&l,&h);
	//		if(yz[0]<l)
	//		{
			//	ans[i]=-1;
	//		}
	//		else
			{
				for(j=0;j<k;j++)
				{
					if(yz[j]<l)
					break;
					if((yz[j]<=h)&&(yz[j]>=l))
					{
						ans[i]=yz[j];
						flag=0;
						break;
					}
				}
				if(flag)
				ans[i]=-1;

			}

		}
		for(i=0;i<n;i++)
		printf("%d\n",ans[i]);
	}
	return 0;
}


#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<iostream>
using namespace std;
int main()
{
	int a,b;
	while(scanf("%d%d",&a,&b)!=EOF)
	{
		int n,s[100025];
		scanf("%d",&n);
		int l,h,i,k;
		   if(a>b)
		   {
			  int t=a;
			  a=b;
			  b=t;
		    }
		    int d;
			 d=__gcd(a,b);
		     //printf("%d\n",d);
		     int j=0;

		     for(i=1;i<=d;i++)
		     {
		     	if(d%i==0) s[j++]=i;
		     }
	     sort(s,j+s);
     	while(n--)
	    {
		    int sum=0;
		    int max=-1;
	    	scanf("%d%d",&l,&h);
		     for(i=0;i<j;i++)
		     {
		     	if(s[i]<=h&&s[i]>=l){
		     		if(s[i]>max)max=s[i];
		     	}
		     	else if(s[i]>h)break;

		     }
		     printf("%d\n",max);
		}
	}
	return 0;
}


#include <stdio.h>
int main()
{
	long a,b;
	while(scanf("%ld%ld",&a,&b)!=EOF)
	{
		long n;
		scanf("%ld",&n);
		while(n--)
		{
			long low,high;
			scanf("%ld%ld",&low,&high);
			long i,temp=-1;
			for(i=low;i<=high;i++)
			{
				if(a%i==0&&b%i==0)
				temp=i;
			}
			printf("%ld\n",temp);
		}
	}
	return 0;
}


#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
using namespace std;
int yz[5000000],ans[100000];
bool cmp(int a,int b)
{
	return a>b;
}
int main()
{
	int i,j,k,a,b,n,l,h,m,mi,ma;
	while(~scanf("%d%d",&a,&b))
	{
	//	memset(yz,0,sizeof(yz));
		mi=min(a,b);
		ma=max(a,b);
		k=0;
		for(i=1;i<=sqrt(mi);i++)
		{
			if(mi%i==0&&ma%i==0)
			{
				yz[k++]=i;
				m=mi/i;
				if(ma%m==0)
				yz[k++]=m;
			}
		}
		sort(yz,yz+k,cmp);
	//	for(i=0;i<k;i++)
	//	printf("aaaa  %d\n",yz[i]);
		scanf("%d",&n);

		for(i=0;i<n;i++)
		{
			scanf("%d%d",&l,&h);
			if(yz[0]<l)
			{
				ans[i]=-1;
			}
			else
			{
				for(j=0;j<k;j++)
				{
					if(yz[j]<=h&&yz[j]>=l)
					{
						ans[i]=yz[j];
						break;
					}

				}
				if(j>=k)
				ans[i]=-1;
			}

		}
		for(i=0;i<n;i++)
		printf("%d\n",ans[i]);
	}
	return 0;
}


#include<stdio.h>
#include<math.h>
int abs(int a)
{
	if(a<0)return -a;
	else return a;
}
int min(int a,int b)
{
	if(a<b)return a;
	else return b;
}
int kgcd(int a, int b)
{
    if (a == 0) return b;
    if (b == 0) return a;
    if (!(a & 1) && !(b & 1))
        return kgcd(a>>1, b>>1)<<1;
    else if (!(b & 1))
    return kgcd(a, b>>1);
    else if (!(a & 1)) return kgcd(a>>1, b);
    else return kgcd(abs(a - b), min(a, b));
}

int main()
{
	int a,b,x,y,t,i,n,j,c[10003],k;
	scanf("%d%d",&a,&b);
	if(a>b)
	{
		t=a;a=b;b=t;
	}
	j=2;c[0]=0;c[1]=1;
	t=kgcd(a,b);
	for(i=2;i<=t/2;i++)
	{
		if(t%i==0)
		{c[j]=i;j++;}
	}
	if(t!=1){c[j]=t;j++;}
	scanf("%d",&n);
	while(n--)
	{
		scanf("%d%d",&x,&y);
		for(i=j-1;c[i]>y&&c[i]!=0;i--);
		if(c[i]>=x)printf("%d\n",c[i]);
		else printf("-1\n");
	}
	return 0;
}


#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
using namespace std;
int yz[5000000],ans[100000];
bool cmp(int a,int b)
{
	return a>b;
}
int main()
{
	int i,j,k,a,b,n,l,h,m,mi,ma;
	while(~scanf("%d%d",&a,&b))
	{
	//	memset(yz,0,sizeof(yz));
		mi=min(a,b);
		ma=max(a,b);
		k=0;
		for(i=1;i<=sqrt(mi);i++)
		{
			if(mi%i==0)
			{
				if(ma%i==0)
				yz[k++]=i;
				m=mi/i;
				if(ma%m==0)
				yz[k++]=m;

			}
		}
		sort(yz,yz+k,cmp);
//		for(i=0;i<k;i++)
	//	printf("aaaa  %d\n",yz[i]);
		scanf("%d",&n);

		for(i=0;i<n;i++)
		{
			scanf("%d%d",&l,&h);
			if(yz[0]<l)
			{
				ans[i]=-1;
			}
			else
			{
				for(j=0;j<k;j++)
				{
					if(yz[j]<=h&&yz[j]>=l)
					{
						ans[i]=yz[j];
						break;
					}

				}
				if(j>=k)
				ans[i]=-1;
			}

		}
		for(i=0;i<n;i++)
		printf("%d\n",ans[i]);
	}
	return 0;
}


#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
using namespace std;
int yz[50000],ans[20000];
bool cmp(int a,int b)
{
	return a>=b;
}
int main()
{
	int i,j,k,a,b,n,l,h,m,mi,ma;
	while(~scanf("%d%d",&a,&b))
	{
		memset(yz,0,sizeof(yz));
		mi=min(a,b);
		ma=max(a,b);
		k=0;
		for(i=1;i<=mi;i++)
		{
			if(mi%i==0&&ma%i==0)
			{
				yz[k++]=i;

			}
		}
		sort(yz,yz+k,cmp);
	//	for(i=0;i<k;i++)
	//	printf("aaaa  %d\n",yz[i]);
		scanf("%d",&n);

		for(i=0;i<n;i++)
		{
			int flag=1;
			scanf("%d%d",&a,&b);
			l=min(a,b);
			h=max(a,b);
	//		if(yz[0]<l)
	//		{
			//	ans[i]=-1;
	//		}
	//		else
			{
				for(j=0;j<k;j++)
				{
					if(yz[j]<l)
					break;
					if((yz[j]<=h)&&(yz[j]>=l))
					{
						ans[i]=yz[j];
						flag=0;
						break;
					}
				}
				if(flag)
				ans[i]=-1;

			}

		}
		for(i=0;i<n;i++)
		printf("%d\n",ans[i]);
	}
	return 0;
}


/*
 * File:   main.cpp
 * Author: Administrator
 *
 * Created on 2012年3月21日, 下午2:56
 */

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define out(x) (std::cerr << #x << ": " << (x) << std::endl)
#define MP make_pair
using namespace std;

/*
 *
 */

template<typename _Tp>
inline bool Max(_Tp & a, const _Tp &b) {
	return (less<_Tp > ()(a, b)) ? a = b, true : false;
}

template<typename _Tp>
inline bool Min(_Tp & a, const _Tp &b) {
	return (!less<_Tp > ()(a, b)) ? a = b, true : false;
}

template<typename _Tp>_Tp gcd(_Tp a, _Tp b) {
	if (!a)return b;
	while (b) {
		_Tp t = a % b;
		a = b;
		b = t;
	}
	return a;
}

int find(const vector<int>&v, int x) {
	int l = 0, r = v.size();
	while (l <= r) {
		int mid = (l + r) / 2;
		if (v[mid] > x)r = mid - 1;
		else l = mid + 1;
	}
	return l;
}

int main(int argc, char** argv) {
	int x, y;
	while(cin >> x >> y){
	int gd = gcd(x, y);
	set<int>st;
	for (int i = 1; i <= gd / i; ++i) {
		if (gd % i)continue;
		st.insert(i);
		st.insert(gd / i);
	}
	vector<int>v;
	for (set<int>::iterator it = st.begin(); it != st.end(); ++it) {
		v.push_back(*it);
	}
	int n, a, b;
	cin >> n;
	while (n-- && cin >> a >> b) {
		int fa = find(v, a - 1), fb = find(v, b);
		if (fa >= fb)cout << "-1" << endl;
		else cout << v[fb - 1] << endl;
		//cout << find(v, a - 1) << ' ' << find(v, b) << endl;
	}}
	return EXIT_SUCCESS;
}


#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<iostream>
using namespace std;
int main()
{
	int a,b;
	while(scanf("%d%d",&a,&b)!=EOF)
	{
		int n,s[100025];
		scanf("%d",&n);
		int l,h,i,k;
		   if(a>b)
		   {
			  int t=a;
			  a=b;
			  b=t;
		    }
		    int d;
			 d=__gcd(a,b);
		     //printf("%d\n",d);
		     int j=0;

		     for(i=1;i<=d;i++)
		     {
		     	if(d%i==0) s[j++]=i;
		     }
	     sort(s,j+s);
     	while(n--)
	    {
		    int sum=0;
		    int max=-1;
	    	scanf("%d%d",&l,&h);
		     for(i=j-1;i>=0;i--)
		     {
		     	if(s[i]<=h){
		     		max=s[i];
		     		break;
		     	}

		     }
		     if(s[i]<l)printf("-1\n");
		     else printf("%d\n",max);
		}
	}
	return 0;
}


#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
using namespace std;
int yz[5000000],ans[20000];
bool cmp(int a,int b)
{
	return a>=b;
}
int main()
{
	int i,j,k,a,b,n,l,h,m,mi,ma;
	while(~scanf("%d%d",&a,&b))
	{
		memset(yz,0,sizeof(yz));
		mi=min(a,b);
		ma=max(a,b);
		k=0;
		for(i=1;i<=40000;i++)
		{
			if(mi%i==0&&ma%i==0)
			{
				yz[k++]=i;
				m=mi/i;
				if(ma%m==0)
				yz[k++]=m;
			}
		}
		sort(yz,yz+k,cmp);
	//	for(i=0;i<k;i++)
	//	printf("aaaa  %d\n",yz[i]);
		scanf("%d",&n);

		for(i=0;i<n;i++)
		{
			int flag=1;
			scanf("%d%d",&a,&b);
			l=min(a,b);
			h=max(a,b);
	//		if(yz[0]<l)
	//		{
			//	ans[i]=-1;
	//		}
	//		else
			{
				for(j=0;j<k;j++)
				{
					if(yz[j]<l)
					break;
					if((yz[j]<=h)&&(yz[j]>=l))
					{
						ans[i]=yz[j];
						flag=0;
						break;
					}
				}
				if(flag)
				ans[i]=-1;

			}

		}
		for(i=0;i<n;i++)
		printf("%d\n",ans[i]);
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

int gcd(int u, int v) {
    return (v != 0) ? gcd(v, u%v) : u;
}
int main(){
    int num1, num2, tmp;
    int n, up, down;
    cin >> num1 >> num2;
    tmp = gcd(num1, num2);

    cin >> n;
    for(unsigned i = 0; i < n; i++){
        int f = 0;
        int f2 = 0;
        cin >> down >> up;
        up = min(up, tmp);
        if(up < down)
		f2 = 1;
        for(unsigned j = up; j >= down && !f2;j--){
            if(num1 % j == 0 && num2 % j == 0)
                cout << j << endl;
				f = 1;
				break;
        }
        if(!f)
		cout << "-1" << endl;
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

int gcd(int u, int v) {
    return (v != 0) ? gcd(v, u%v) : u;
}
int main(){
    int num1, num2, tmp;
    int n, up, down;
    cin >> num1 >> num2;
    tmp = gcd(num1, num2);

    cin >> n;
    for(int i = 0; i < n; i++){
        int f = 0;
        int f2 = 0;
        cin >> down >> up;
        up = min(up, tmp);
        if(up < down)
		f2 = 1;
        for(int j = up; j >= down && !f2;j--){
            if(num1 % j == 0 && num2 % j == 0)
                cout << j << endl;
				f = 1;
				break;
        }
        if(!f)
		cout << "-1" << endl;
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#define ll long long
using namespace std;

int gcd(int u, int v) {
    return (v != 0) ? gcd(v, u%v) : u;
}
int main(){
    int num1, num2, tmp;
    int n, up, down;
    cin >> num1 >> num2;
    vector<long long int> v_gcd;
    tmp = gcd(num1, num2);

    cin >> n;
    for(unsigned i = 0; i < n; i++){
        int f = 0;
        int f2 = 0;
        cin >> down >> up;
        up = min(up, tmp);
        if(up < down)
		f2 = 1;
        for(int j = up; j >= down && !f2;j--){
            if(num1 % j == 0 && num2 % j == 0)
                cout << j << endl;
				f = 1;
				break;
        }
        if (!f)cout << "-1" << endl;
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;

int gcd(int u, int v) {
    return (v != 0) ? gcd(v, u%v) : u;
}
int a,b,q,l,r,gc,sz;
int f[30];
bool vis[30];
map <int, bool> seen;
vector <int> v;
void tap(int x){
	if(x == sz){
		int j = 1;
		for(int i = 0; i < sz; i++)
			if(vis[i]) j*=f[i];

		if seen[j] == 0){
			seen[j] = 1;
			v.push_back(j);
		}
		return;
	}
	for(int i = 0; i < 2; i++)
		vis[x] = i, tap(x+1);
}

int main(){
	cin >> a >> b >> q;
	gc = gcd(a,b);
	if (a > b)
		swap(a, b);
	int mx = a;
	for (int i = 2; i * i <= mx; i++){
		while (1){
			if (a%i == 0 && b%i == 0){
				while (a%i == 0 && b%i == 0)
					f[sz++] = i, a/=i, b/=i;
			}
			else if (a%i == 0 && b%i != 0){
				while (a%i == 0)
					a/=i;
			}
			else if (a%i != 0 && b%i == 0){
				while (b%i == 0)
					b/=i;
			}
			else
				break;
		}
	}
	if (b%a == 0)
		f[sz++] = a;
	tap(0);
	sort(v.begin(),v.end());
	while (q--) {
		cin >> l >> r;
		int k = lower_bound(v.begin(),v.end(), r)-v.begin();
		if(k == v.size())
		k--;
		else if(v[k] > r && k > 0)
		k--;
		if(v[k] >= l && v[k] <= r)
			cout << v[k] << "\n";
		else
			cout << "-1\n";
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;

int gcd(int u, int v) {
    return (v != 0) ? gcd(v, u%v) : u;
}
int a,b,q,l,r,gc,sz;
int f[30];
bool vis[30];
map <int, bool> seen;
vector <int> v;
void tap(int x){
	if(x == sz){
		int j = 1;
		for(int i = 0; i < sz; i++)
			if(vis[i]) j*=f[i];

		if (seen[j] == 0){
			seen[j] = 1;
			v.push_back(j);
		}
		return;
	}
	for(int i = 0; i < 2; i++)
		vis[x] = i, tap(x+1);
}

int main(){
	cin >> a >> b >> q;
	gc = gcd(a,b);
	if (a > b)
		swap(a, b);
	int mx = a;
	for (int i = 2; i * i <= mx; i++){
		while (1){
			if (a%i == 0 && b%i == 0){
				while (a%i == 0 && b%i == 0)
					f[sz++] = i, a/=i, b/=i;
			}
			else if (a%i == 0 && b%i != 0){
				while (a%i == 0)
					a/=i;
			}
			else if (a%i != 0 && b%i == 0){
				while (b%i == 0)
					b/=i;
			}
			else
				break;
		}
	}
	if (b%a == 0)
		f[sz++] = a;
	tap(0);
	sort(v.begin(),v.end());
	while (q--) {
		cin >> l >> r;
		int k = lower_bound(v.begin(),v.end(), r)-v.begin();
		if(k == v.size())
		k--;
		else if(v[k] > r && k > 0)
		k--;
		if(v[k] >= l && v[k] <= r)
			cout << v[k] << "\n";
		else
			cout << "-1\n";
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#define ll long long
using namespace std;

int gcd(int u, int v) {
    return (v != 0) ? gcd(v, u%v) : u;
}
int main(){
    int num1, num2, tmp;
    int n, up, down;
    cin >> num1 >> num2;
    vector<long long int> v_gcd;
    tmp = gcd(num1, num2);

    cin >> n;
    for(unsigned i = 0; i < n; i++){
        int f = 0;
        int f2 = 0;
        cin >> down >> up;
        up = min(up, tmp);
        if(up < down)
		f2 = 1;
        for(unsigned j = up; j >= down && !f2;j--){
            if(num1 % j == 0 && num2 % j == 0)
                cout << j << endl;
				f = 1;
				break;
        }
        if (!f)cout << "-1" << endl;
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#define ll long long
using namespace std;

int gcd(int u, int v) {
    return (v != 0) ? gcd(v, u%v) : u;
}
int main(){
    int num1, num2, tmp;
    int n, up, down;
    cin >> num1 >> num2;
    vector<long long int> v_gcd;
    tmp = gcd(num1, num2);

    cin >> n;
    for(int i = 0; i < n; i++){
        int f = 0;
        int f2 = 0;
        cin >> down >> up;
        up = min(up, tmp);
        if(up < down)
		f2 = 1;
        for(int j = up; j >= down && !f2;j--){
            if(num1 % j == 0 && num2 % j == 0){
                cout << j << endl;
				f = 1;
				break;
				}
        }
        if (!f)cout << "-1" << endl;
    }
    return 0;
}


#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 10010;

int num[maxn];
int tot;

void Swap(int &a, int &b) {
    int t = a;
    a = b; b = t;
}

void gao(int a, int b) {
    int i, sq = (int)sqrt(a);
    tot = 0;
    for(i=1; i<=sq; i++) {
        if(a%i==0) {
            if(b%i==0) num[tot++] = i;
            if(b%(a/i)==0) num[tot++] = a/i;
        }
    }
    sort(num, num+tot);
}

int bin(int a, int b) {
    int l = 0, r = tot-1, mid;
    int best = -1, i;
    while(l<=r) {
        mid = (l+r)>>1;
        if(num[mid] <= b) {
            best = num[mid];
            l = mid+1;
        }
        else r = mid-1;
    }
    if(best < a) best = -1;
    return best;
}

int main() {
    int n;
    int a, b;
    scanf("%d%d", &a, &b);
    if(a > b) Swap(a, b);
    gao(a, b);
    scanf("%d", &n);
    int x, y;
    for(int i=0; i<n; i++) {
        scanf("%d%d", &x, &y);
        printf("%d\n", bin(x, y));
    }
    return 0;
}


#include <cstdio>
#include <cstring>
const int maxn = 10010;

int num[maxn];
int tot;

void Swap(int &a, int &b) {
    int t = a;
    a = b; b = t;
}

void gao(int a, int b) {
    int i;
    tot = 0;
    for(i=2; i<=a; i++) {
        if(a%i==0) {
            num[tot++] = i;
        }
    }
    int tmp = tot;
    tot = 0;
    for(i=0; i<tmp; i++) {
        if(b%num[i]==0) {
            num[tot++] = num[i];
        }
    }
}

int bin(int a, int b) {
    int l = 0, r = tot-1, mid;
    int best = -1, i;
    while(l<=r) {
        mid = (l+r)>>1;
        if(num[mid] >= a) {
            best = num[mid];
            r = mid-1;
        }
        else l = mid+1;
    }
    if(best > b) best = -1;
    return best;
}

int main() {
    int n;
    int a, b;
    scanf("%d%d", &a, &b);
    if(a > b) Swap(a, b);
    gao(a, b);
    scanf("%d", &n);
    for(int i=0; i<n; i++) {
        scanf("%d%d", &a, &b);
        printf("%d\n", bin(a, b));
    }
    return 0;
}


#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 10010;

int num[maxn];
int tot;

void Swap(int &a, int &b) {
    int t = a;
    a = b; b = t;
}

void gao(int a, int b) {
    int i, sq = (int)sqrt(a);
    tot = 0;
    for(i=1; i<=sq; i++) {
        if(a%i==0 && b%i==0) {
            num[tot++] = i;
        }
        if(b%(a/i)==0) num[tot++] = a/i;
    }
    sort(num, num+tot);
    //for(i=0; i<tot; i++) printf("%d\n", num[i]);
}

int bin(int a, int b) {
    int l = 0, r = tot-1, mid;
    int best = -1, i;
    while(l<=r) {
        mid = (l+r)>>1;
        if(num[mid] <= b) {
            best = num[mid];
            l = mid+1;
        }
        else r = mid-1;
    }
    if(best < a) best = -1;
    return best;
}

int main() {
    int n;
    int a, b;
    scanf("%d%d", &a, &b);
    if(a > b) Swap(a, b);
    gao(a, b);
    scanf("%d", &n);
    for(int i=0; i<n; i++) {
        scanf("%d%d", &a, &b);
        printf("%d\n", bin(a, b));
    }
    return 0;
}


#include <cstdio>
#include <cstring>
const int maxn = 10010;

int num[maxn];
int tot;

void Swap(int &a, int &b) {
    int t = a;
    a = b; b = t;
}

void gao(int a, int b) {
    int i;
    tot = 0;
    for(i=2; i<=a; i++) {
        if(a%i==0) {
            num[tot++] = i;
        }
    }
    int tmp = tot;
    tot = 0;
    for(i=0; i<tmp; i++) {
        if(b%num[i]==0) {
            num[tot++] = num[i];
        }
    }
}

int bin(int a, int b) {
    int l = 0, r = tot-1, mid;
    int best = -1, i;
    while(l<=r) {
        mid = (l+r)>>1;
        if(num[mid] <= b) {
            best = num[mid];
            l = mid+1;
        }
        else r = mid-1;
    }
    if(best < a) best = -1;
    return best;
}

int main() {
    int n;
    int a, b;
    scanf("%d%d", &a, &b);
    if(a > b) Swap(a, b);
    gao(a, b);
    scanf("%d", &n);
    for(int i=0; i<n; i++) {
        scanf("%d%d", &a, &b);
        printf("%d\n", bin(a, b));
    }
    return 0;
}


#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 10010;

int num[maxn];
int tot;

void Swap(int &a, int &b) {
    int t = a;
    a = b; b = t;
}

void gao(int a, int b) {
    int i, sq = (int)sqrt(a);
    tot = 0;
    for(i=1; i<=sq; i++) {
        if(a%i==0 && b%i==0) {
            num[tot++] = i;
            if(b%(a/i)==0) num[tot++] = a/i;
        }
    }
    sort(num, num+tot);
}

int bin(int a, int b) {
    int l = 0, r = tot-1, mid;
    int best = -1, i;
    while(l<=r) {
        mid = (l+r)>>1;
        if(num[mid] <= b) {
            best = num[mid];
            l = mid+1;
        }
        else r = mid-1;
    }
    if(best < a) best = -1;
    return best;
}

int main() {
    int n;
    int a, b;
    scanf("%d%d", &a, &b);
    if(a > b) Swap(a, b);
    gao(a, b);
    scanf("%d", &n);
    for(int i=0; i<n; i++) {
        scanf("%d%d", &a, &b);
        printf("%d\n", bin(a, b));
    }
    return 0;
}


#include <cstdio>
#include <cstring>
const int maxn = 10010;

int num[maxn];
int tot;

void Swap(int &a, int &b) {
    int t = a;
    a = b; b = t;
}

void gao(int a, int b) {
    int i;
    tot = 0;
    for(i=2; i<=a; i++) {
        if(a%i==0 && b%i==0) {
            num[tot++] = i;
        }
    }
}

int bin(int a, int b) {
    int l = 0, r = tot-1, mid;
    int best = -1, i;
    while(l<=r) {
        mid = (l+r)>>1;
        if(num[mid] <= b) {
            best = num[mid];
            l = mid+1;
        }
        else r = mid-1;
    }
    if(best < a) best = -1;
    return best;
}

int main() {
    int n;
    int a, b;
    scanf("%d%d", &a, &b);
    if(a > b) Swap(a, b);
    gao(a, b);
    scanf("%d", &n);
    for(int i=0; i<n; i++) {
        scanf("%d%d", &a, &b);
        printf("%d\n", bin(a, b));
    }
    return 0;
}


#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>

#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657

const ull inf = 1LL << 61;
const double eps=1e-5;

using namespace std;

bool cmp(int a,int b){
    return a>b;
}
int arrgcd[maxn];int a,b;
int gcd(int c,int d)
{
    return d==0?c:gcd(d,c%d);
}
vector<int>x;
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int c,d,n;
    while(scanf("%d%d",&a,&b)!=EOF)
    {   int p=gcd(a,b);
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&c,&d);
            int mark=0;
            ///x.clear();
            for(int i=p;i>=2;i--)
            {
                if(p%i==0&&i<=d&&i>=c){mark=1;cout<<i<<endl;break;}///{x.push_back(i);}
            }
            if(p%d==0&&d!=1&&!mark){mark=1;cout<<d<<endl;}
            if(!mark)cout<<-1<<endl;
        }
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
vector<int> x;
int low, high, a, b, n, m, ans,i;
int gcd(int c,int d)
{
    return d==0?c:gcd(d,c%d);
}
int main() {
    scanf("%d%d", &a, &b);
    a = gcd(a, b);
    b = sqrt(a);
    x.clear();
	int count=0;
    for (i=1; i<=b; i++)
        if (a % i == 0) {
            x.push_back(i);
            x.push_back(a/i);
        }//求最大公约数的因数，并保存到x
    sort(x.begin(), x.end());
	/*for(int i=0;i<x.size();i++)
		cout<<x[i]<<"/";
		cout<<endl;*/
		int mark=0;
    scanf("%d", &n);
    for (i=0; i<n; i++) {
        scanf("%d%d", &low, &high);
		mark=0;
		for(int i=x.size();i>=1;i--)
			if(x[i]<=high&&x[i]>=low){mark=1;ans=x[i];break;}
			if(!mark)cout<<-1<<endl;
			else cout<<ans<<endl;
       /* m = upper_bound(x.begin(), x.end(), high) - x.begin() - 1;//找到比high大的第一个数的下标的位置
        ans = x[m];
        if (low > ans) puts("-1");
        else printf("%d\n", ans);*/
    }

    return 0;
}


#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>

#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657

const ull inf = 1LL << 61;
const double eps=1e-5;

using namespace std;

bool cmp(int a,int b){
    return a>b;
}
int arrgcd[maxn];int a,b;
void gcd(int c,int d)
{
    /*int low=c,high=d,mid;
    while(low<=high)
    {
       mid=(low+high)>>1;
       if(a%mid==0&&b%mid==0&&mid!=1)cout<<mid<<endl;

    }*/
    int mark=0;
    for(int i=c;i<=d;i++)
    {
        if(a%i==0&&b%i==0&&i!=1){mark=1;cout<<i<<endl;}
    }
    if(!mark)cout<<-1<<endl;
}
int main()
{
    freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int c,d,n;
    while(scanf("%d%d",&a,&b)!=EOF)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&c,&d);
            gcd(c,d);
        }
    }
    return 0;
}


#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>

#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657

const ull inf = 1LL << 61;
const double eps=1e-5;

using namespace std;

bool cmp(int a,int b){
    return a>b;
}
int arrgcd[maxn];int a,b;
void gcd(int c,int d)
{
    /*int low=c,high=d,mid;
    while(low<=high)
    {
       mid=(low+high)>>1;
       if(a%mid==0&&b%mid==0&&mid!=1)cout<<mid<<endl;

    }*/
    int mark=0;
    for(int i=c;i<=d;i++)
    {
        if(a%i==0&&b%i==0&&i!=1){mark=1;cout<<i<<endl;}
    }
    if(!mark)cout<<-1<<endl;
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int c,d,n;
    while(scanf("%d%d",&a,&b)!=EOF)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&c,&d);
            gcd(c,d);
        }
    }
    return 0;
}


#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>

#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657

const ull inf = 1LL << 61;
const double eps=1e-5;

using namespace std;

bool cmp(int a,int b){
    return a>b;
}
int arrgcd[maxn];int a,b;
void gcd(int c,int d)
{
    /*int low=c,high=d,mid;
    while(low<=high)
    {
       mid=(low+high)>>1;
       if(a%mid==0&&b%mid==0&&mid!=1)cout<<mid<<endl;

    }*/
    int mark=0;
    for(int i=c;i<=d;i++)
    {
        if(a%i==0&&b%i==0&&i!=1){mark=1;cout<<i<<endl;}
    }
    if(!mark)cout<<-1<<endl;
}
int main()
{

    int c,d,n;
    while(scanf("%d%d",&a,&b)!=EOF)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&c,&d);
            gcd(c,d);
        }
    }
    return 0;
}


#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>

#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657

const ull inf = 1LL << 61;
const double eps=1e-5;

using namespace std;

bool cmp(int a,int b){
    return a>b;
}
int arrgcd[maxn];int a,b;
int gcd(int c,int d)
{
    return d==0?c:gcd(d,c%d);
}
vector<int>x;
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int c,d,n;
    while(scanf("%d%d",&a,&b)!=EOF)
    {   int p=gcd(a,b);
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&c,&d);
            int mark=0;
            ///x.clear();
            for(int i=sqrt(p);i>=2;i--)
            {
                if(p%i==0&&i<=d&&i>=c){mark=1;cout<<i<<endl;break;}///{x.push_back(i);}
            }
            if(p%c==0&&c!=1&&!mark){mark=1;cout<<c<<endl;}
            if(!mark)cout<<-1<<endl;
        }
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
vector<int> x;
int low, high, a, b, n, m, ans,i;
int gcd(int c,int d)
{
    return d==0?c:gcd(d,c%d);
}
int main() {
    scanf("%d%d", &a, &b);
    a = gcd(a, b);
    b = sqrt(a);
    x.clear();
	int count=0;
    for (i=1; i<=b; i++)
        if (a % i == 0) {
            x.push_back(i);
            x.push_back(a/i);
        }//求最大公约数的因数，并保存到x
    sort(x.begin(), x.end());
	/*for(int i=0;i<x.size();i++)
		cout<<x[i]<<"/";
		cout<<endl;*/
		int mark=0;
    scanf("%d", &n);
    for (i=0; i<n; i++) {
        scanf("%d%d", &low, &high);
		mark=0;
		for(int i=x.size();i>=0;i--)
			if(x[i]<=high&&x[i]>=low){mark=1;ans=x[i];break;}
			if(!mark)cout<<-1<<endl;
			else cout<<ans<<endl;
       /* m = upper_bound(x.begin(), x.end(), high) - x.begin() - 1;//找到比high大的第一个数的下标的位置
        ans = x[m];
        if (low > ans) puts("-1");
        else printf("%d\n", ans);*/
    }

    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
vector<int> x;
int low, high, a, b, n, m, ans,i;
int gcd(int c,int d)
{
    return d==0?c:gcd(d,c%d);
}
int main() {
    scanf("%d%d", &a, &b);
    a = gcd(a, b);
    b = sqrt(a);
    x.clear();
	int count=0;
    for (i=1; i<=b; i++)
        if (a % i == 0) {
            x.push_back(i);
            x.push_back(a/i);
        }//求最大公约数的因数，并保存到x
    sort(x.begin(), x.end());
	/*for(int i=0;i<x.size();i++)
		cout<<x[i]<<"/";
		cout<<endl;*/
		int mark=0;
    scanf("%d", &n);
    for (i=0; i<n; i++) {
        scanf("%d%d", &low, &high);
		mark=0;
		for(int i=x.size()-1;i>=0;i--)
			if(x[i]<=high&&x[i]>=low){mark=1;ans=x[i];break;}
			if(!mark)cout<<-1<<endl;
			else cout<<ans<<endl;
        /*m = upper_bound(x.begin(), x.end(), high) - x.begin() - 1;//找到比high大的第一个数的下标的位置
        ans = x[m];
        if (low > ans) puts("-1");
        else printf("%d\n", ans);*/
    }

    return 0;
}


#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>

#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657

const ull inf = 1LL << 61;
const double eps=1e-5;

using namespace std;

bool cmp(int a,int b){
    return a>b;
}
int arrgcd[maxn];int a,b;
int gcd(int c,int d)
{
    return d==0?c:gcd(d,c%d);
}
vector<int>x;
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int c,d,n;
    while(scanf("%d%d",&a,&b)!=EOF)
    {   int p=gcd(a,b);
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&c,&d);
            int mark=0;
            ///x.clear();
            for(int i=2;i*i<=p;i++)
            {
                if(p%i==0&&i<=d&&i>=c){mark=1;cout<<i<<endl;break;}///{x.push_back(i);}
            }
            if(p%c==0&&c!=1){mark=1;cout<<c<<endl;}
            if(!mark)cout<<-1<<endl;
        }
    }
    return 0;
}


#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>

#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657

const ull inf = 1LL << 61;
const double eps=1e-5;

using namespace std;

bool cmp(int a,int b){
    return a>b;
}
int arrgcd[maxn];int a,b;
int gcd(int c,int d)
{
    return d==0?c:gcd(d,c%d);
}
vector<int>x;
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int c,d,n;
    while(scanf("%d%d",&a,&b)!=EOF)
    {   int p=gcd(a,b);
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&c,&d);
            int mark=0;
            ///x.clear();
            for(int i=sqrt(p);i>=2;i--)
            {
                if(p%i==0&&i<=d&&i>=c){mark=1;cout<<i<<endl;break;}///{x.push_back(i);}
            }
            if(p%c==0&&c!=1){mark=1;cout<<c<<endl;}
            if(!mark)cout<<-1<<endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 9;

int a, b, n, dv[N], l, r, sz;

int bs(int l, int r) {
	int bas = 0;
	int son = sz + 1;
	while (bas + 1 < son) {
		int orta = bas + son >> 1;
		if (dv[orta] < l) bas = orta;
		else if (dv[orta] > r) son = orta;
		else if (dv[orta] <= r and dv[orta] >= l) bas = orta;
	}
//	cout << l << " " << r << " " << dv[bas] << endl;
	return dv[bas] < l ? -1 : dv[bas];
}

int main() {

	cin >> a >> b >> n;

	for (int i = 1; i * i <= max(a, b); i++) {
		if (a % i == 0 and b % i == 0) {
			dv[++sz] = i;
			if (b % (a / i) == 0 and i * i != min(a, b))
				dv[++sz] = a / i;
		}
	}

	sort(dv + 1, dv + sz + 1);

	for (int i = 1; i <= n; i++) {
		cin >> l >> r;
		cout << bs(l, r) << endl;
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e5 + 9;

ll a, b, n, dv[N], l, r, sz;

ll bs(ll l, ll r) {
    ll bas = 0;
    ll son = sz + 1;
    while (bas + 1 < son) {
        ll orta = bas + son >> 1;
        if (dv[orta] < l) bas = orta;
        else if (dv[orta] > r) son = orta;
        else if (dv[orta] <= r and dv[orta] >= l) bas = orta;
    }
    return (dv[bas] < l or dv[bas] > r) ? -1 : dv[bas];
}

int main() {

    cin >> a >> b >> n;

    for (ll i = 1; i * i <= max(a, b); i++) {
        if (a % i == 0 and b % i == 0) dv[++sz] = i;
        if (a % (b / i) == 0 and b % i == 0 and i * i != b) dv[++sz] = b / i;
        if (b % (a / i) == 0 and a % i == 0 and i * i != a) dv[++sz] = a / i;
    }

    sort(dv + 1, dv + sz + 1);

    for (ll i = 1; i <= n; i++) {
        cin >> l >> r;
        cout << bs(l, r) << endl;
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e5 + 9;

ll a, b, n, dv[N], l, r, sz;

ll bs(ll l, ll r) {
	ll bas = 0;
	ll son = sz + 1;
	while (bas + 1 < son) {
		ll orta = bas + son >> 1;
		if (dv[orta] < l) bas = orta;
		else if (dv[orta] > r) son = orta;
		else if (dv[orta] <= r and dv[orta] >= l) bas = orta;
	}
//	cout << l << " " << r << " " << dv[bas] << endl;
	return (dv[bas] < l or dv[bas] > r) ? -1 : dv[bas];
}

int main() {

	cin >> a >> b >> n;

	for (ll i = 1; i * i <= max(a, b); i++) {
		if (a % i == 0 and b % i == 0) {
			dv[++sz] = i;
			if (b % (a / i) == 0 and i * i != max(a, b))
				dv[++sz] = a / i;
			if (a % (b / i) == 0)
				dv[++sz] = b / i;
		}
		else if (a % (b / i) == 0 and b % i == 0) dv[++sz] = b / i;
		else if (b % (a / i) == 0 and a % i == 0) dv[++sz] = a / i;
	}

	sort(dv + 1, dv + sz + 1);

	for (ll i = 1; i <= n; i++) {
		cin >> l >> r;
		cout << bs(l, r) << endl;
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 9;

int a, b, n, dv[N], l, r, sz;

int bs(int l, int r) {
	int bas = 0;
	int son = sz + 1;
	while (bas + 1 < son) {
		int orta = bas + son >> 1;
		if (dv[orta] < l) bas = orta;
		else if (dv[orta] > r) son = orta;
		else if (dv[orta] <= r and dv[orta] >= l) bas = orta;
	}
//	cout << l << " " << r << " " << dv[bas] << endl;
	return (dv[bas] < l or dv[bas] > r) ? -1 : dv[bas];
}

int main() {

	cin >> a >> b >> n;

	for (int i = 1; i * i <= max(a, b); i++) {
		if (a % i == 0 and b % i == 0) {
			dv[++sz] = i;
			if (b % (a / i) == 0 and i * i != max(a, b))
				dv[++sz] = a / i;
			else if (a % (b / i) == 0)
				dv[++sz] = b / i;
		}
	}

	sort(dv + 1, dv + sz + 1);

	for (int i = 1; i <= n; i++) {
		cin >> l >> r;
		cout << bs(l, r) << endl;
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 9;

int a, b, n, dv[N], l, r, sz;

int bs(int l, int r) {
	int bas = 0;
	int son = sz + 1;
	while (bas + 1 < son) {
		int orta = bas + son >> 1;
		if (dv[orta] < l) bas = orta;
		else if (dv[orta] > r) son = orta;
		else if (dv[orta] <= r and dv[orta] >= l) bas = orta;
	}
//	cout << l << " " << r << " " << dv[bas] << endl;
	return (dv[bas] < l or dv[bas] > r) ? -1 : dv[bas];
}

int main() {

	cin >> a >> b >> n;

	for (int i = 1; i * i <= max(a, b); i++) {
		if (a % i == 0 and b % i == 0) {
			dv[++sz] = i;
			if (b % (a / i) == 0 and i * i != max(a, b))
				dv[++sz] = a / i;
			if (a % (b / i) == 0)
				dv[++sz] = b / i;
		}
	}

	sort(dv + 1, dv + sz + 1);

	for (int i = 1; i <= n; i++) {
		cin >> l >> r;
		cout << bs(l, r) << endl;
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 9;

int a, b, n, dv[N], l, r, sz;

int bs(int l, int r) {
	int bas = 0;
	int son = sz + 1;
	while (bas + 1 < son) {
		int orta = bas + son >> 1;
		if (dv[orta] < l) bas = orta;
		else if (dv[orta] > r) son = orta;
		else if (dv[orta] <= r and dv[orta] >= l) bas = orta;
	}
	return dv[bas] < l ? -1 : dv[bas];
}

int main() {

	cin >> a >> b >> n;

	for (int i = 1; i * i <= min(a, b); i++) {
		if (a % i == 0 and b % i == 0) {
			dv[++sz] = i;
			if (b % (a / i) == 0)
				dv[++sz] = a / i;
		}
	}

	sort(dv + 1, dv + sz + 1);

	for (int i = 1; i <= n; i++) {
		cin >> l >> r;
		cout << bs(l, r) << endl;
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs
#include <cstdio>	// require printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream
#include <cstring>	// require memset
#include <cctype>	// require tolower, toupper
#include <fstream>	// require freopen
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()

typedef long long ll;

int l[10005], r[10005];

using namespace std;

int main()
{
//	cut here before submit
//	freopen ("testcase.MGCD3", "r", stdin );
	int a, b;
	cin >> a >> b;

	int g = __gcd (a, b );

	set <int> cand;
	cand.insert (g );

	if (a != b ){
		if (a > b )
			swap (a, b );
		for (int i = 2; i < a; i++ ){
			if (a % i == 0 && b % i == 0 )
				cand.insert (i );
		} // end for
	} // end if

	int n;
	cin >> n;
	rep (i, n ){
		cin >> l[i] >> r[i];
	} // end loop

	rep (i, n ){
		set <int>::iterator it = cand.begin();
		int res = 0;
		for (; it != cand.end(); it++ ){
			int m = (*it);
			if (l[i] <= m && m <= r[i] ){
				res = m;
			} // end if
		} // end for
		cout << (res ? res : -1 ) << endl;
	} // end rep

	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs
#include <cstdio>	// require printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream
#include <cstring>	// require memset
#include <cctype>	// require tolower, toupper
#include <fstream>	// require freopen
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()

typedef long long ll;

int l[10005], r[10005];

using namespace std;

int main()
{
//	cut here before submit
//	freopen ("testcase.MGCD3", "r", stdin );
	int a, b;
	cin >> a >> b;

	int g = __gcd (a, b );

	set <int> cand;
	cand.insert (1 );
	cand.insert (g );

	if (a != b ){
		if (a > b )
			swap (a, b );
		for (int i = 2; i < a; i++ ){
			if (a % i == 0 && b % i == 0 )
				cand.insert (i );
		} // end for
	} // end if

	int n;
	cin >> n;
	rep (i, n ){
		cin >> l[i] >> r[i];
	} // end loop

	rep (i, n ){
		set <int>::iterator it = cand.begin();
		int res = 0;
		for (; it != cand.end(); it++ ){
			int m = (*it);
			if (l[i] <= m && m <= r[i] ){
				res = m;
			} // end if
		} // end for
		cout << (res ? res : -1 ) << endl;
	} // end rep

	return 0;
}


#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

int main()
{
	int a, b;
	cin >> a >> b;

	int g = __gcd (a, b );

	set <int> cand;
	for (int i = 1; i*i <= g; i++ ){
		if (g % i == 0 ){
				cand.insert (i );
				cand.insert (g/i );
		} // end if
	} // end for

	vector <int> num (cand.begin(), cand.end() );
	sort (num.begin(), num.end() );

	int n;
	cin >> n;
	rep (i, n ){
		int p, q;
		cin >> p >> q;
		if (p <= num[upper_bound( num.begin(), num.end(),q ) - num.begin()-1] ){
			cout << num[upper_bound( num.begin(), num.end(),q ) - num.begin()-1] << endl;
		}else{
			cout << -1 << endl;
		} // end if
	} // end rep

	return 0;
}


#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

int main()
{
	int a, b;
	cin >> a >> b;

	int g = __gcd (a, b );

	set <int> cand;
	for (int i = 1; i*i <= g; i++ ){
		if (g % i == 0 ){
				cand.insert (i );
				cand.insert (g/i );
		} // end if
	} // end for

	vector <int> num (cand.begin(), cand.end() );
	sort (num.begin(), num.end() );

	int n;
	cin >> n;
	rep (i, n ){
		int p, q;
		cin >> p >> q;
		if (p <= num[upper_bound( num.begin(), num.end(),q ) - num.begin()-1] ){
			cout << num[lower_bound( num.begin(), num.end(),q ) - num.begin()-1] << endl;
		}else{
			cout << -1 << endl;
		} // end if
	} // end rep

	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs
#include <cstdio>	// require printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream
#include <cstring>	// require memset
#include <cctype>	// require tolower, toupper
#include <fstream>	// require freopen
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()

typedef long long ll;

int l[10005], r[10005];

using namespace std;

int main()
{
//	cut here before submit
//	freopen ("testcase.MGCD", "r", stdin );
	int a, b;
	cin >> a >> b;

	int g = __gcd (a, b );

	set <int> cand;
	cand.insert (g );

	if (a > b )
		swap (a, b );
	for (int i = 2; i < a; i++ ){
		if (a % i == 0 )
			cand.insert (i );
	} // end for

	int n;
	cin >> n;
	rep (i, n ){
		cin >> l[i] >> r[i];
	} // end loop

	rep (i, n ){
		set <int>::iterator it = cand.begin();
		int res = 0;
		for (; it != cand.end(); it++ ){
			int m = (*it);
			if (l[i] <= m && m <= r[i] ){
				res = m;
			} // end if
		} // end for
		cout << (res ? res : -1 ) << endl;
	} // end rep

	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs
#include <cstdio>	// require printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream
#include <cstring>	// require memset
#include <cctype>	// require tolower, toupper
#include <fstream>	// require freopen
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()

typedef long long ll;

int l[10005], r[10005];

using namespace std;

int main()
{
//	cut here before submit
//	freopen ("testcase.MGCD", "r", stdin );
	int a, b;
	cin >> a >> b;

	int g = __gcd (a, b );

	set <int> cand;
	cand.insert (g );

	if (a > b )
		swap (a, b );
	for (int i = 2; i < a; i++ ){
		if (a % i == 0 )
			cand.insert (i );
	} // end for

	int n;
	cin >> n;
	rep (i, n ){
		cin >> l[i] >> r[i];
	} // end loop

	rep (i, n ){
		set <int>::iterator it = cand.begin();
		int res = 0;
		for (; it != cand.end(); it++ ){
			int m = (*it);
			if (l[i] <= m && m <= r[i] ){
				res = m;
				break;
			} // end if
		} // end for
		cout << (res ? res : -1 ) << endl;
	} // end rep

	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs
#include <cstdio>	// require printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream
#include <cstring>	// require memset
#include <cctype>	// require tolower, toupper
#include <fstream>	// require freopen
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()

typedef long long ll;

int l[10005], r[10005];

using namespace std;

int main()
{
//	cut here before submit
//	freopen ("testcase.MGCD3", "r", stdin );
	int a, b;
	cin >> a >> b;

	int g = __gcd (a, b );

	set <int> cand;
	cand.insert (g );

	if (a > b )
		swap (a, b );
	for (int i = 2; i < a; i++ ){
		if (a % i == 0 && b % i == 0 )
			cand.insert (i );
	} // end for

	int n;
	cin >> n;
	rep (i, n ){
		cin >> l[i] >> r[i];
	} // end loop

	rep (i, n ){
		set <int>::iterator it = cand.begin();
		int res = 0;
		for (; it != cand.end(); it++ ){
			int m = (*it);
			if (l[i] <= m && m <= r[i] ){
				res = m;
			} // end if
		} // end for
		cout << (res ? res : -1 ) << endl;
	} // end rep

	return 0;
}


#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <numeric>
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <fstream>
using namespace std;

class GCD
{
public:
	int lcm (int, int );
	int gcd (int, int );
private:

};
int GCD::gcd (int a, int b)
{
	if (a > b){
		swap(a,b);
	} // end if
	while (b % a != 0){
		b = b % a;
		if (a > b){
			swap(a,b);
		} // end if
	} // end while

	return a;
}
int GCD::lcm (int a, int b)
{
	return (a*b/lcm(a,b));
}

int main()
{
	GCD gcd;
//	cut here before submit
//	freopen("testcase.gcd", "r", stdin);
	string str = "";
	int a,b;

	getline (cin, str );
	stringstream ssm(str);
	ssm >> a >> b;
	getline (cin, str );
	int n;
	stringstream ssn (str );
	ssn >> n;

	int i;
	for (i = 0; i < n; ++i){
		int res = 0;
		int low, high;
		getline (cin, str );
		stringstream ss(str);
		ss >> low >> high;

//		cout << "a: " << a << " b: " << b << endl;

		int g = gcd.gcd (a, b );
		int i;
//		cout << "g: " << g << endl;

		if (low <= g && g <= high ){
			cout << g << endl;
		}else{
			for (i = low; i <= high; ++i){
				if (g % i == 0 ){
					res = i;
				} // end if
			} // end for

			if (res != 0 ){
				cout << res << endl;
			}else{
				cout << "-1" << endl;
			} // end if
		} // end if

	} // end for

	return 0;
}


#define fr(i, a, x) for(int(i) = a; i <= x; ++i)
#define rfr(i, a, x) for(int(i) = a; i >= x; --i)
#define all(a) a.begin(), a.end()
#define Min(a, b) (a < b) ? a : b
#define Max(a, b) (a > b) ? a : b
#define pb push_back
//#define MY_DEBUG 1

#ifdef MY_DEBUG
#include <fstream>
std::ifstream cin("input.txt");
std::ofstream cout("output.txt");
#else
#include <iostream>
#endif

#include <string>
#include <map>
#include <ctime>
#include <stack>
#include <deque>
#include <cstdlib>
#include <set>
#include <cmath>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;

typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector< vi > vvi;
typedef vector< vb > vvb;
typedef long long ll;
typedef unsigned long long ull;

const double pi = acos(-1.0);
const int inf = 2 * 1e9;
const ll linf = 1e18;
const int prost = 51;

void re(int & x, int i, int k)
{
	if (i == 0)
		x = (x / 10) * 10 + k;
	else if (i == 1)
		x = x - ((x % 100) / 10) * 10 + k * 10;
	else if (i == 2)
		x = x - ((x % 1000) / 100) * 100 + k * 100;
	else if (i == 3)
		x = x - (x / 1000) * 1000 + k * 1000;
}

long long nod(long long a, long long b)
{
	while(a != 0 && b != 0)
	{
		if (a > b)
			a %= b;
		else
			b %= a;
	}
	return (a + b);
}

int main()
{
	long long a, b, nok, m, left, right, mn;
	cin >> a >> b;
	nok = nod(a, b);
	cin >> m;
	for(int i = 0; i < m; ++i)
	{
		cin >> left >> right;
		mn = 0;
		bool p = false;
		for(long long j = 1; j * j <= nok; ++j)
			if (nok % j == 0)
			{
				if (j >= left && j <= right && j > mn)
				{
					mn = j;
					p = true;
				}
				else if (p) break;
				if (nok / j >= left && nok / j <= right && nok / j > mn)
				{
					mn = nok / j;
					break;
				}
			}
		if (mn == 0)
			cout << -1 << '\n';
		else
			cout << mn << '\n';
	}
	return 0;
}


#define fr(i, a, x) for(int(i) = a; i <= x; ++i)
#define rfr(i, a, x) for(int(i) = a; i >= x; --i)
#define all(a) a.begin(), a.end()
#define Min(a, b) (a < b) ? a : b
#define Max(a, b) (a > b) ? a : b
#define pb push_back
#define MY_DEBUG 1

#ifdef MY_DEBUG
#include <fstream>
std::ifstream cin("input.txt");
std::ofstream cout("output.txt");
#else
#include <iostream>
#endif

#include <string>
#include <map>
#include <ctime>
#include <stack>
#include <deque>
#include <cstdlib>
#include <set>
#include <cmath>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;

typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector< vi > vvi;
typedef vector< vb > vvb;
typedef long long ll;
typedef unsigned long long ull;

const double pi = acos(-1.0);
const int inf = 2 * 1e9;
const ll linf = 1e18;
const int prost = 51;

void re(int & x, int i, int k)
{
	if (i == 0)
		x = (x / 10) * 10 + k;
	else if (i == 1)
		x = x - ((x % 100) / 10) * 10 + k * 10;
	else if (i == 2)
		x = x - ((x % 1000) / 100) * 100 + k * 100;
	else if (i == 3)
		x = x - (x / 1000) * 1000 + k * 1000;
}

long long nod(long long a, long long b)
{
	while(a != 0 && b != 0)
	{
		if (a > b)
			a %= b;
		else
			b %= a;
	}
	return (a + b);
}

int main()
{
	long long a, b, nok, m, left, right, mn;
	cin >> a >> b;
	nok = (a / nod(a, b)) * b;
	cin >> m;
	for(int i = 0; i < m; ++i)
	{
		cin >> left >> right;
		mn = 0;
		for(long long j = 1; j * j <= nok; ++j)
			if (nok % j == 0)
			{
				if (j >= left && j <= right && j > mn)
					mn = j;
				if (nok / j >= left && nok / j <= right && nok / j > mn)
				{
					mn = nok / j;
					break;
				}
			}
		if (mn == 0)
			cout << -1 << '\n';
		else
			cout << mn << '\n';
	}
	return 0;
}


#define fr(i, a, x) for(int(i) = a; i <= x; ++i)
#define rfr(i, a, x) for(int(i) = a; i >= x; --i)
#define all(a) a.begin(), a.end()
#define Min(a, b) (a < b) ? a : b
#define Max(a, b) (a > b) ? a : b
#define pb push_back
//#define MY_DEBUG 1

#ifdef MY_DEBUG
#include <fstream>
std::ifstream cin("input.txt");
std::ofstream cout("output.txt");
#else
#include <iostream>
#endif

#include <string>
#include <map>
#include <ctime>
#include <stack>
#include <deque>
#include <cstdlib>
#include <set>
#include <cmath>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;

typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector< vi > vvi;
typedef vector< vb > vvb;
typedef long long ll;
typedef unsigned long long ull;

const double pi = acos(-1.0);
const int inf = 2 * 1e9;
const ll linf = 1e18;
const int prost = 51;

void re(int & x, int i, int k)
{
	if (i == 0)
		x = (x / 10) * 10 + k;
	else if (i == 1)
		x = x - ((x % 100) / 10) * 10 + k * 10;
	else if (i == 2)
		x = x - ((x % 1000) / 100) * 100 + k * 100;
	else if (i == 3)
		x = x - (x / 1000) * 1000 + k * 1000;
}

long long nod(long long a, long long b)
{
	while(a != 0 && b != 0)
	{
		if (a > b)
			a %= b;
		else
			b %= a;
	}
	return (a + b);
}

int main()
{
	long long a, b, nok, m, left, right, mn;
	cin >> a >> b;
	nok = nod(a, b);
	cin >> m;
	for(int i = 0; i < m; ++i)
	{
		cin >> left >> right;
		mn = 0;
		for(long long j = 1; j * j <= nok; ++j)
			if (nok % j == 0)
			{
				if (j >= left && j <= right && j > mn)
					mn = j;
				if (nok / j >= left && nok / j <= right && nok / j > mn)
				{
					mn = nok / j;
					break;
				}
			}
		if (mn == 0)
			cout << -1 << '\n';
		else
			cout << mn << '\n';
	}
	return 0;
}


//#define MY

#pragma comment(linker, "/STACK:64000000")

#include <string>
#include <vector>
#include <queue>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <set>
#include <iomanip>

#ifdef MY
#include <fstream>
using namespace std;
ifstream cin("input.txt");
ofstream cout("output.txt");
#else
#include <iostream>
using namespace std;
#endif

long long nod(long long a, long long b){
	while(a != 0 && b != 0){
		if (a > b)
			a %= b;
		else
			b %= a;
	}
	return (a + b);
}

vector<long long> h;

long long find(long long x){
	long long ll = 0, rr = h.size() - 1, mm, ans = -1;
	while(ll < rr){
		mm = (ll + rr) / 2;
		if (h[mm] <= x){
			ll = mm + 1;
			ans = h[mm];
		}
		else
			rr = mm;
	}
	if (h[(ll + rr) / 2] <= x && h[(ll + rr) / 2] > ans)
		ans = h[(ll + rr) / 2];
	return ans;
}

int main(){
	long long a, b, n, x, right, left, mx, y;
	cin >> a >> b >> n;
	x = nod(a, b);
	for(int j = 1; j * j <= x; ++j){
		if (x % j == 0){
			h.push_back(x / j);
			h.push_back(j);
		}
	}
	sort(h.begin(), h.end());
	for(int i = 0; i < n; ++i){
		cin >> left >> right;
		y = find(right);
		if (y >= left)
			cout << y << '\n';
		else
			cout << -1 << '\n';
	}
}


#define fr(i, a, x) for(int(i) = a; i <= x; ++i)
#define rfr(i, a, x) for(int(i) = a; i >= x; --i)
#define all(a) a.begin(), a.end()
#define Min(a, b) (a < b) ? a : b
#define Max(a, b) (a > b) ? a : b
#define pb push_back
#define MY_DEBUG 1

#ifdef MY_DEBUG
#include <fstream>
std::ifstream cin("input.txt");
std::ofstream cout("output.txt");
#else
#include <iostream>
#endif

#include <string>
#include <map>
#include <ctime>
#include <stack>
#include <deque>
#include <cstdlib>
#include <set>
#include <cmath>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;

typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector< vi > vvi;
typedef vector< vb > vvb;
typedef long long ll;
typedef unsigned long long ull;

const double pi = acos(-1.0);
const int inf = 2 * 1e9;
const ll linf = 1e18;
const int prost = 51;

void re(int & x, int i, int k)
{
	if (i == 0)
		x = (x / 10) * 10 + k;
	else if (i == 1)
		x = x - ((x % 100) / 10) * 10 + k * 10;
	else if (i == 2)
		x = x - ((x % 1000) / 100) * 100 + k * 100;
	else if (i == 3)
		x = x - (x / 1000) * 1000 + k * 1000;
}

long long nod(long long a, long long b)
{
	while(a != 0 && b != 0)
	{
		if (a > b)
			a %= b;
		else
			b %= a;
	}
	return (a + b);
}

int main()
{
	long long a, b, nok, m, left, right, mn;
	cin >> a >> b;
	nok = nod(a, b);
	cin >> m;
	for(int i = 0; i < m; ++i)
	{
		cin >> left >> right;
		mn = 0;
		for(long long j = 1; j * j <= nok; ++j)
			if (nok % j == 0)
			{
				if (j >= left && j <= right && j > mn)
					mn = j;
				if (nok / j >= left && nok / j <= right && nok / j > mn)
				{
					mn = nok / j;
					break;
				}
			}
		if (mn == 0)
			cout << -1 << '\n';
		else
			cout << mn << '\n';
	}
	return 0;
}


#define fr(i, a, x) for(int(i) = a; i <= x; ++i)
#define rfr(i, a, x) for(int(i) = a; i >= x; --i)
#define all(a) a.begin(), a.end()
#define Min(a, b) (a < b) ? a : b
#define Max(a, b) (a > b) ? a : b
#define pb push_back
//#define MY_DEBUG 1

#ifdef MY_DEBUG
#include <fstream>
std::ifstream cin("input.txt");
std::ofstream cout("output.txt");
#else
#include <iostream>
#endif

#include <string>
#include <map>
#include <ctime>
#include <stack>
#include <deque>
#include <cstdlib>
#include <set>
#include <cmath>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;

typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector< vi > vvi;
typedef vector< vb > vvb;
typedef long long ll;
typedef unsigned long long ull;

const double pi = acos(-1.0);
const int inf = 2 * 1e9;
const ll linf = 1e18;
const int prost = 51;

void re(int & x, int i, int k)
{
	if (i == 0)
		x = (x / 10) * 10 + k;
	else if (i == 1)
		x = x - ((x % 100) / 10) * 10 + k * 10;
	else if (i == 2)
		x = x - ((x % 1000) / 100) * 100 + k * 100;
	else if (i == 3)
		x = x - (x / 1000) * 1000 + k * 1000;
}

long long nod(long long a, long long b)
{
	while(a != 0 && b != 0)
	{
		if (a > b)
			a %= b;
		else
			b %= a;
	}
	return (a + b);
}

int main()
{
	long long a, b, nok, m, left, right, mn;
	cin >> a >> b;
	nok = (a / nod(a, b)) * b;
	cin >> m;
	for(int i = 0; i < m; ++i)
	{
		cin >> left >> right;
		mn = 0;
		for(long long j = 1; j * j <= nok; ++j)
			if (nok % j == 0)
			{
				if (j >= left && j <= right && j > mn)
					mn = j;
				if (nok / j >= left && nok / j <= right && nok / j > mn)
				{
					mn = nok / j;
					break;
				}
			}
		if (mn == 0)
			cout << -1 << '\n';
		else
			cout << mn << '\n';
	}
	return 0;
}


//#define MY

#pragma comment(linker, "/STACK:64000000")

#include <string>
#include <vector>
#include <queue>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <set>
#include <iomanip>

#ifdef MY
#include <fstream>
using namespace std;
ifstream cin("input.txt");
ofstream cout("output.txt");
#else
#include <iostream>
using namespace std;
#endif

long long nod(long long a, long long b){
	while(a != 0 && b != 0){
		if (a > b)
			a %= b;
		else
			b %= a;
	}
	return (a + b);
}

vector<long long> h;

long long find(long long x){
	long long ll = 0, rr = h.size() - 1, mm, ans = -1;
	while(ll < rr){
		mm = (ll + rr) / 2;
		if (h[mm] <= x){
			ll = mm + 1;
			ans = h[mm];
		}
		else
			rr = mm;
	}
	if (h[(ll + rr) / 2] <= x && h[(ll + rr) / 2] > ans)
		ans = h[(ll + rr) / 2];
	return ans;
}

int main(){
	long long a, b, n, x, right, left, mx, y;
	cin >> a >> b >> n;
	x = nod(a, b);
	for(int j = 1; j * j <= x; ++j){
		if (x % j == 0){
			h.push_back(x / j);
			h.push_back(j);
		}
	}
	sort(h.begin(), h.end());
	for(int i = 0; i < n; ++i){
		cin >> left >> right;
		y = find(right);
		if (y >= left)
			cout << y << '\n';
		else
			cout << -1 << '\n';
	}
}


//#define MY

#pragma comment(linker, "/STACK:64000000")

#include <string>
#include <vector>
#include <queue>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <set>
#include <iomanip>

#ifdef MY
#include <fstream>
using namespace std;
ifstream cin("input.txt");
ofstream cout("output.txt");
#else
#include <iostream>
using namespace std;
#endif

long long nod(long long a, long long b){
	while(a != 0 && b != 0){
		if (a > b)
			a %= b;
		else
			b %= a;
	}
	return (a + b);
}

int main(){
	long long a, b, n, x, right, left, mx, y;
	cin >> a >> b >> n;
	x = nod(a, b);
	for(int i = 0; i < n; ++i){
		cin >> left >> right;
		mx = -1;
		for(int j = 1; j * j <= x; ++j){
			if (x % j == 0 && mx < j && j <= right && j >= left)
				mx = j;
			y = x / j;
			if (x % y == 0 && mx < y && y >= left && y <= right)
				mx = y;
		}
		cout << mx << '\n';
	}
}


#include "bits/stdc++.h"
using namespace std;
typedef unsigned long long ull;
typedef long long int ll;
typedef vector<long long int> vi;
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
    ll n,sum=0,count=0,m,flag=0,ans=0,k;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        ll x;
        cin>>x;
        count=max(x,count);
        sum+=x;
    }
    cout<<max(count,(ll)ceil(sum/n-1));
    return 0;
}

/* freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
 */


#include "bits/stdc++.h"
using namespace std;
typedef unsigned long long ull;
typedef long long int ll;
typedef vector<long long int> vi;
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
    ll n,sum=0,count=0,m,flag=0,ans=0,k;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        ll x;
        cin>>x;
        count=max(x,count);
        sum+=x;
    }
    cout<<(((sum-1)/(n-1))+1);
    return 0;
}


#include "bits/stdc++.h"
using namespace std;
typedef unsigned long long ull;
typedef long long int ll;
typedef vector<long long int> vi;
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
    ll n,sum=0,count=0,m,flag=0,ans=0,k;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        ll x;
        cin>>x;
        sum=max(x,sum);
    }
    cout<<sum+1;
    return 0;
}

/* freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
 */


#include "bits/stdc++.h"
using namespace std;
typedef unsigned long long ull;
typedef long long int ll;
typedef vector<long long int> vi;
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
    ll n,sum=0,count=0,m,flag=0,ans=0,k;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        ll x;
        cin>>x;
        count=max(x,count);
        sum+=x;
    }
    cout<<max(count,((sum-1)/(n-1))+1);
    return 0;
}

/* freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
 */


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
int main()
{
	ll int n;
    cin>>n;
    ll int i=0,a[n],sum=0;
    for(i=0;i<n;i++)
    {
    	cin>>a[i];
    	sum+=a[i];
	}
	sort(a,a+n,greater<int>());
	//cout<<sum;
	ll int ans=ceil(sum/(1.0*(n-1)));
	if(ans>=a[0])
	cout<<ans;
	else
	{
		cout<<a[0];
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
int main()
{
	ll int n;
    cin>>n;
    ll int i=0,a[n],sum=0;
    for(i=0;i<n;i++)
    {
    	cin>>a[i];
    	sum+=a[i];
	}
	sort(a,a+n,greater<int>());
	cout<<sum;
	ll int ans=ceil(sum/(1.0*(n-1)));
	if(ans>=a[0])
	cout<<ans;
	else
	{
		cout<<a[0];
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
int main()
{
	ll int n;
	cin>>n;
	ll int ans=0;
	ll int a[n];
	ll int i;
	for(i=0;i<n;i++)
	{
		cin>>a[i];
	}
	sort(a,a+n,greater<ll int>());
	ll int max=a[0];
	ll int min=a[n-1];
	if(a[n-2]==min)
	{
		ll int freq=1;
		i=n-2;
		while(i>=0)
		{
			if(a[i]==a[n-2])
			{
				freq++;
			}
			else
			{
				break;
			}
			i--;
		}
		if(freq==2)
		{
			a[0]-=(min+min-1);
			ans+=(min+min-1);
		}
		else if(min>=freq)
		{
			a[0]-=ceil((min-freq+1)/(1.0*(freq-1)));
			ans+=ceil((min-freq+1)/(1.0*(freq-1)));
		}
		else
		{
			a[0]-=min;
			ans+=min;
		}
		if(a[0]==0)
		a[0]=1;
		ans+=a[0];
	}
	else
	{
		ll int freq=0;
		i=n-2;
		while(i>=0)
		{
			if(a[i]==a[n-2])
			{
				freq++;
			}
			else
			{
				break;
			}
			i--;
		}
		ans+=abs(min-a[n-2]);
		a[0]-=ans;
		a[n-2]=min;
		freq+=1;
		if(freq==2)
		{
			a[0]-=(min+min-1);
			ans+=(min+min-1);
		}
		else if(min>=freq)
		{
			a[0]-=ceil((min-freq+1)/(1.0*(freq-1)));
			ans+=ceil((min-freq+1)/(1.0*(freq-1)));
		}
		else
		{
			a[0]-=min;
			ans+=min;
		}
		if(a[0]==0)
		a[0]=1;
		ans+=a[0];
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
int main()
{
	ll int n;
    cin>>n;
    ll int i=0,a[n],sum=0;
    for(i=0;i<n;i++)
    {
    	cin>>a[i];
    	sum+=a[i];
	}
	ll int ans=ceil(sum/(1.0*(n-1)));
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>
#define MAX (ll)(5e5 + 4)
#define MOD (ll)(1e9 + 7)
#define PI (double)(3.14159265)
#define IO ios_base :: sync_with_stdio (false), cin.tie (nullptr);

using namespace std;
using ll = long long;
using ld = long double;

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    long long int n,i,j=0,k;
    cin>>n;
    vector<int>a(n);
    for(i=0;i<n;i++)
    {
        cin>>a[i];
        j+=a[i];
    }
    k=*max_element(a.begin(),a.end());
    j=(j-1)/(n-1);
    j++;
    cout<<max(k,j);
    return 0;
}


#include <bits/stdc++.h>
#define MAX (ll)(5e5 + 4)
#define MOD (ll)(1e9 + 7)
#define PI (double)(3.14159265)
#define IO ios_base :: sync_with_stdio (false), cin.tie (nullptr);

using namespace std;
using ll = long long;
using ld = long double;

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    long long int n,i,j=0,k;
    cin>>n;
    vector<int>a(n);
    for(i=0;i<n;i++)
    {
        cin>>a[i];
        j+=a[i];
    }
    k=*max_element(a.begin(),a.end());
    j=j/(n-1);
    j++;
    cout<<max(k,j);
    return 0;
}


#include <bits/stdc++.h>
#define MAX (ll)(5e5 + 4)
#define MOD (ll)(1e9 + 7)
#define PI (double)(3.14159265)
#define IO ios_base :: sync_with_stdio (false), cin.tie (nullptr);

using namespace std;
using ll = long long;
using ld = long double;

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    long long int n,i,j=0,k;
    cin>>n;
    vector<int>a(n);
    for(i=0;i<n;i++)
    {
        cin>>a[i];
        j+=a[i];
    }
    k=*max_element(a.begin(),a.end());
    j=(j+1)/(n-1);
    cout<<max(k,j);
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <fstream>
#include <unordered_map>
#include <map>
#include <iterator>
#include <iomanip>
#include <stack>
#include <math.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;

#define TASK "payment"
#define X first
#define Y second
#define mp make_pair
#define inb push_back

int main()
{
#ifdef _DEBUG
	freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#else
	//freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#endif
	int n;
	cin >> n;
	int ans = -2e9;
	for (int i = 0; i < n; ++i)
	{
		int x;
		scanf("%d", &x);
		ans = max(ans, x);
	}
	cout << ans + 1;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <fstream>
#include <unordered_map>
#include <map>
#include <iterator>
#include <iomanip>
#include <stack>
#include <math.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;

#define TASK "payment"
#define X first
#define Y second
#define mp make_pair
#define inb push_back

int a[1000000];

set<pii> s;

int main()
{
#ifdef _DEBUG
	freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#else
	//freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#endif
	int n;
	cin >> n;
	for (int i = 0; i < n; ++i)
	{
		scanf("%d", &a[i]);
		s.insert(mp(a[i], i));
	}
	int k = 0;
	while (s.begin()->X != s.rbegin()->X)
	{
		set<pii> s1;
		for (int i = 0; i < n; ++i)
			if (i != s.begin()->Y && a[i])
				a[i]--, s1.insert(mp(a[i], i));
			else
				s1.insert(mp(a[i], i));
		s = s1;
		++k;
	}
	if (s.begin()->X)
		cout << k + s.begin()->X + 1;
	else
		cout << k;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <fstream>
#include <unordered_map>
#include <map>
#include <iterator>
#include <iomanip>
#include <stack>
#include <math.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;

#define TASK "payment"
#define X first
#define Y second
#define mp make_pair
#define inb push_back

ll a[1000000];

int main()
{
#ifdef _DEBUG
	freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#else
	//freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#endif
	int n;
	cin >> n;
	ll mx = 0, sm = 0;
	for (int i = 0; i < n; ++i)
	{
		scanf("%I64d", &a[i]);
		mx = max(mx, a[i]);
		sm += a[i];
	}
	cout << max(mx, (sm + n - 2) / (n - 1));
}


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pi pair<int, int>
#define pii pair<int, pi>
#define F first
#define S second
#define pb push_back
#define PI acos(-1.0)
using namespace std;
ll n, a[100100], Ans, Sum, Max;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    // freopen("in.in", "r", stdin);
    // freopen("out.out", "w", stdout);

    cin >> n;
    for (int i = 0; i < n; i++){
        cin >> a[i];
        Sum += a[i];
        Max = max(Max, a[i]);
    }

    Ans = Sum/(n-1) + (Sum%(n-1) != 0);
    Ans = max(Ans, Max);

    cout << Ans << '\n';
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pi pair<int, int>
#define pii pair<int, pi>
#define F first
#define S second
#define pb push_back
#define PI acos(-1.0)
using namespace std;
ll n, a[100100], Ans, Sum, Max;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    // freopen("in.in", "r", stdin);
    // freopen("out.out", "w", stdout);

    cin >> n;
    for (int i = 0; i < n; i++){
        cin >> a[i];
        Sum += a[i];
        Max = max(Max, a[i]);
    }

    Ans = ceil(Sum/(n-1));
    Ans = max(Ans, Max + (n <= 3));

    cout << Ans << '\n';
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pi pair<int, int>
#define pii pair<int, pi>
#define F first
#define S second
#define pb push_back
#define PI acos(-1.0)
using namespace std;
ll n, a[100100], Ans, Sum, Max;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    // freopen("in.in", "r", stdin);
    // freopen("out.out", "w", stdout);

    cin >> n;
    for (int i = 0; i < n; i++){
        cin >> a[i];
        Sum += a[i];
        Max = max(Max, a[i]);
    }

    Ans = ceil(Sum/(n-1));
    Ans = max(Ans, Max + 1);

    cout << Ans << '\n';
    return 0;
}