#include<bits/stdc++.h>
#define sz 100002
#define pb push_back
#define ff first
#define ss second
#define ll long long
#define P push
#define PQ priority_queue<double, vector<double>, greater<double> >q
#define _sort(v) sort(v.begin(),v.end())
#define MP map<ll int,ll int>mp
#define MEM(x) memset(x,0,sizeof(x))
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define VEC(n) ll int x; vector<ll int>v;for(int i=0;i<n;i++){cin>>x;v.pb(x);}
#define TEST() int test;cin>>test; while(test--)
#define FAST() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
using namespace std;
//Upper_bound   auto x=upper_bound(v[a].begin(),v[a].end(),les);les=*x; if(x==v[a].end())break;
//ll int LCM(ll int a,ll int b) { ll int gcd=__gcd(a,b);ll int lcm=a*b/gcd;return lcm;}
//ll int bigmod(ll int b, ll int p, ll int m){if(p==0)return 1;if(p%2!=0) return b%m*fun(b,p-1,m)%m;if(p%2==0){ ll int res=fun(b,p/2,m);return (res%m*res%m)%m;}}
//ll mod_inverse(ll a, ll mod){return bigmod(a,mod-2,mod);}
//void seive() { prime[np++]=2;for(i=3; i<=sz; i+=2){if(mark[i]==0){ prime[np++]=i;for(j=i*i; j<=sz; j+=i) mark[j]=1; } }}
//string add(string s1,string s2) {string s;int j=s1.size()-1,c=0,m; for(int i=s2.size()-1; i>=0; i--) {if(j>=0)m=s2[i]-'0'+s1[j]-'0'+c;else m=s2[i]-'0'+c; if(m>9) { s+=m%10+'0';c=m/10; } else {  c=0;s+=m+'0'; }j--; } if(c!=0) {s+=c+'0'; }reverse(s.begin(),s.end());return s;}
//int pos;string mul(int n) {string s;for(int i=0; i<pos; i++) { s+="0"; }pos++;int c=0,m; for(int i=s1.size()-1; i>=0; i--) { m=(s1[i]-'0')*n+c;if(m>9) { s+=m%10+'0';c=m/10;} else { c=0; s+=m+'0';} }while(c!=0) { s+=c%10+'0';c=c/10;}    reverse(s.begin(),s.end());return s;}
//string add(string s,int n){ string res; int c=n,x; for(int i=s.size()-1;i>=0;i--)   { x=s[i]-'0'+c;res+=(x%10+'0'); c=x/10; }    while(c)    { res+=(c%10+'0'); c=c/10;} reverse(res.begin(),res.end()); return res;}

int main()
{
    //FAST();
    ll int n,m,x,a,b,gcd;
    cin>>n>>m;
    gcd=__gcd(n,m);
    ll int sq=sqrt(gcd);
    cin>>x;
    while(x--)
    {
        cin>>a>>b;
        ll int res=-1;
        for(ll int i=1;i<=sq;i++)
        {
            if(gcd%i==0)
            {
                if(i>=a&&i<=b)
                {
                    res=max(res,i);
                }
                if(gcd/i>=a&&gcd/i<=b)
                {
                    res=max(res,gcd/i);
                    break;
                }
            }
        }
        cout<<res<<endl;

    }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 998244353

/*ll binpow(ll a, ll b) {

    ll res=1;
    while (b > 0) {
        if (b & 1)
            res = (res * a);
        a = (a*a);
        b >>= 1;
    }
    return res;
}
*/

/*
ll cal(ll c,ll k)
{
    ll res=fact[c];

    ll res1=((fact[k]%mod)*(fact[(c-k)]%mod))%mod;

    ll res0;
    res0=binpow(res1,mod-2,mod);
    return ((res*res0)%mod);
}*/

/*bool cmp(const pair<int,int> &a,
              const pair<int,int> &b)
{
    if(a.first==b.first)
    {
        return(a.second<b.second);
    }
    else
    {
        return (a.first<b.first);
    }
}*/

int main() {
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    ll x,y;
    cin>>x>>y;
    ll n;
    cin>>n;

    vector<ll>v;
    for(ll i=2;i*i<=max(x,y);i++)
    {
        if(x%i==0 && y%i==0)
        {
           // cout<<"i"<<i<<endl;
            v.push_back(i);
                if(y%(x/i)==0)
                {
                v.push_back((x/i));
                }
                if(x%(y/i)==0)
                {
                v.push_back((y/i));
                }
        }
    }

    sort(v.begin(),v.end());

    for(ll i=0;i<n;i++)
    {
        ll a,b;
        cin>>a>>b;
        auto it=lower_bound(v.begin(),v.end(),b);
        ll f=-1;
        if(v.size()!=0)
        {
            if(*it!=b && it!=v.begin())
            {
                it--;
            }
            f=(*it);
        }

      // cout<<f<<endl;
        if(f>=a && f<=b)
        {
            cout<<f<<endl;
        }
        else
        {
            cout<<-1<<endl;
        }
    }

	return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Read questions carefully.


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 998244353

/*ll binpow(ll a, ll b) {

    ll res=1;
    while (b > 0) {
        if (b & 1)
            res = (res * a);
        a = (a*a);
        b >>= 1;
    }
    return res;
}
*/

/*
ll cal(ll c,ll k)
{
    ll res=fact[c];

    ll res1=((fact[k]%mod)*(fact[(c-k)]%mod))%mod;

    ll res0;
    res0=binpow(res1,mod-2,mod);
    return ((res*res0)%mod);
}*/

/*bool cmp(const pair<int,int> &a,
              const pair<int,int> &b)
{
    if(a.first==b.first)
    {
        return(a.second<b.second);
    }
    else
    {
        return (a.first<b.first);
    }
}*/

int main() {
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    ll x,y;
    cin>>x>>y;
    ll n;
    cin>>n;

    vector<ll>v;
    for(ll i=1;i*i<=max(x,y);i++)
    {
        if(x%i==0 && y%i==0)
        {
           // cout<<"i"<<i<<endl;
            v.push_back(i);
                if(y%(x/i)==0)
                {
                v.push_back((x/i));
                }
                if(x%(y/i)==0)
                {
                v.push_back((y/i));
                }
        }
    }

    sort(v.begin(),v.end());

    for(ll i=0;i<n;i++)
    {
        ll a,b;
        cin>>a>>b;
        auto it=lower_bound(v.begin(),v.end(),b);
        ll f=-1;
        if(v.size()!=0)
        {
            if(*it!=b && it!=v.begin())
            {
                it--;
            }
            f=(*it);
        }

      // cout<<f<<endl;
        if(f>=a && f<=b)
        {
            cout<<f<<endl;
        }
        else
        {
            cout<<-1<<endl;
        }
    }

	return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Read questions carefully.


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 998244353

/*ll binpow(ll a, ll b) {

    ll res=1;
    while (b > 0) {
        if (b & 1)
            res = (res * a);
        a = (a*a);
        b >>= 1;
    }
    return res;
}
*/

/*
ll cal(ll c,ll k)
{
    ll res=fact[c];

    ll res1=((fact[k]%mod)*(fact[(c-k)]%mod))%mod;

    ll res0;
    res0=binpow(res1,mod-2,mod);
    return ((res*res0)%mod);
}*/

/*bool cmp(const pair<int,int> &a,
              const pair<int,int> &b)
{
    if(a.first==b.first)
    {
        return(a.second<b.second);
    }
    else
    {
        return (a.first<b.first);
    }
}*/

int main() {
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    ll x,y;
    cin>>x>>y;
    ll n;
    cin>>n;

    vector<ll>v;
    for(ll i=2;i*i<=max(x,y);i++)
    {
        if(x%i==0 && y%i==0)
        {
           // cout<<"i"<<i<<endl;
            v.push_back(i);
                if(y%(x/i)==0)
                {
                v.push_back((x/i));
                }
                if(x%(y/i)==0)
                {
                v.push_back((y/i));
                }
        }
    }

    sort(v.begin(),v.end());

    for(ll i=0;i<n;i++)
    {
        ll a,b;
        cin>>a>>b;
        auto it=lower_bound(v.begin(),v.end(),b);
        it--;
        ll f=(*it);
      // cout<<f<<endl;
        if(it==v.end() || f<a)
        {
            cout<<-1<<endl;
        }
        else
        {
            cout<<f<<endl;
        }
    }

	return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Read questions carefully.


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 998244353

/*ll binpow(ll a, ll b) {

    ll res=1;
    while (b > 0) {
        if (b & 1)
            res = (res * a);
        a = (a*a);
        b >>= 1;
    }
    return res;
}
*/

/*
ll cal(ll c,ll k)
{
    ll res=fact[c];

    ll res1=((fact[k]%mod)*(fact[(c-k)]%mod))%mod;

    ll res0;
    res0=binpow(res1,mod-2,mod);
    return ((res*res0)%mod);
}*/

/*bool cmp(const pair<int,int> &a,
              const pair<int,int> &b)
{
    if(a.first==b.first)
    {
        return(a.second<b.second);
    }
    else
    {
        return (a.first<b.first);
    }
}*/

int main() {
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    ll x,y;
    cin>>x>>y;
    ll n;
    cin>>n;

    vector<ll>v;
    for(ll i=1;i*i<=max(x,y);i++)
    {
        if(x%i==0)
        {
            if(y%i==0)
            {
                v.push_back(i);
            }
                if(y%(x/i)==0 )
                {
                    v.push_back((x/i));
                }
        }
        if(y%i==0)
        {
                if(x%(y/i)==0)
                {
                    v.push_back((y/i));
                }
        }
    }

    sort(v.begin(),v.end());

  /*  for(auto x:v)
    {
        cout<<"x"<<x<<endl;
    }*/

    for(ll i=0;i<n;i++)
    {
        ll a,b;
        cin>>a>>b;
        auto it=upper_bound(v.begin(),v.end(),b);
        ll f=-1;
        if(v.size()!=0)
        {
            if(it!=v.begin())
            {
                it--;
            }
            f=(*it);
        }

      // cout<<f<<endl;
        if(f>=a && f<=b)
        {
            cout<<f<<endl;
        }
        else
        {
            cout<<-1<<endl;
        }
    }

	return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Read questions carefully.


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 998244353

/*ll binpow(ll a, ll b) {

    ll res=1;
    while (b > 0) {
        if (b & 1)
            res = (res * a);
        a = (a*a);
        b >>= 1;
    }
    return res;
}
*/

/*
ll cal(ll c,ll k)
{
    ll res=fact[c];

    ll res1=((fact[k]%mod)*(fact[(c-k)]%mod))%mod;

    ll res0;
    res0=binpow(res1,mod-2,mod);
    return ((res*res0)%mod);
}*/

/*bool cmp(const pair<int,int> &a,
              const pair<int,int> &b)
{
    if(a.first==b.first)
    {
        return(a.second<b.second);
    }
    else
    {
        return (a.first<b.first);
    }
}*/

int main() {
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    ll x,y;
    cin>>x>>y;
    ll n;
    cin>>n;

    vector<ll>v;
    for(ll i=2;i*i<=max(x,y);i++)
    {
        if(x%i==0 && y%i==0)
        {
           // cout<<"i"<<i<<endl;
            v.push_back(i);

                v.push_back((x/i));
                v.push_back((y/i));
        }
    }

    sort(v.begin(),v.end());

    for(ll i=0;i<n;i++)
    {
        ll a,b;
        cin>>a>>b;
        auto it=lower_bound(v.begin(),v.end(),b);
        it--;
        ll f=(*it);
      // cout<<f<<endl;
        if(it==v.end() || f<a)
        {
            cout<<-1<<endl;
        }
        else
        {
            cout<<f<<endl;
        }
    }

	return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Read questions carefully.


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 998244353

/*ll binpow(ll a, ll b) {

    ll res=1;
    while (b > 0) {
        if (b & 1)
            res = (res * a);
        a = (a*a);
        b >>= 1;
    }
    return res;
}
*/

/*
ll cal(ll c,ll k)
{
    ll res=fact[c];

    ll res1=((fact[k]%mod)*(fact[(c-k)]%mod))%mod;

    ll res0;
    res0=binpow(res1,mod-2,mod);
    return ((res*res0)%mod);
}*/

/*bool cmp(const pair<int,int> &a,
              const pair<int,int> &b)
{
    if(a.first==b.first)
    {
        return(a.second<b.second);
    }
    else
    {
        return (a.first<b.first);
    }
}*/

int main() {
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    ll x,y;
    cin>>x>>y;
    ll n;
    cin>>n;

    vector<ll>v;
    for(ll i=2;i*i<=max(x,y);i++)
    {
        if(x%i==0 && y%i==0)
        {
           // cout<<"i"<<i<<endl;
            v.push_back(i);
                if(y%(x/i)==0)
                {
                v.push_back((x/i));
                }
                if(x%(y/i)==0)
                {
                v.push_back((y/i));
                }
        }
    }

    sort(v.begin(),v.end());

    for(ll i=0;i<n;i++)
    {
        ll a,b;
        cin>>a>>b;
        auto it=lower_bound(v.begin(),v.end(),b);
        ll f=-1;
        if(it==v.begin())
        {
            it--;

        }
        else
        {
            it--;
            f=(*it);
        }
      // cout<<f<<endl;
        if(it==v.end() || f<a)
        {
            cout<<-1<<endl;
        }
        else
        {
            cout<<f<<endl;
        }
    }

	return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Read questions carefully.


/********************/
/* K B G D E L T A */
/*     DA-IICT      */
/********************/

#include<bits/stdc++.h>
using namespace std;
using   ll=long long;
using   ld=long double;
#define fast           ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define endl           "\n"
#define F(i,n)         for(ll i=0;i<n;i++)
#define rep(i,a,b)     for(ll i=a;i<b;i++)
#define repp(i,a,b)    for(ll i=a;i>=b;i--)
#define pb             push_back
#define mp             make_pair
#define vll            vector<ll>
#define f              first
#define s              second
#define pll            pair<ll,ll>
#define ALL(v)         v.begin(),v.end()
#define ALLR(v)        v.rbegin(),v.rend()
#define pii            3.14159265358979323

void solve()
{

	ll a,b,q,x,y,ans,tt=1;

	cin>>a>>b;

	ll gcd=__gcd(a,b);

	vector<ll> dd;

	for(int i=1;i<=sqrt(gcd);i++)
	{
		if(gcd%i==0)
		{
							dd.pb(i);
			if(gcd/i!=i)	dd.pb(gcd/i);
		}
	}

	sort(ALL(dd));

	cin>>q;

	while(q--)
	{
		cin>>x>>y;

		if(x<=gcd)
		{
			auto it=lower_bound(ALL(dd),x);

			if(it==dd.end())
			{
				cout<<-1<<endl;
			}
			else
			{
				if((*it)>y)
				{
					cout<<-1<<endl;
				}
				else
				{
					cout<<(*it)<<endl;
				}
			}
		}
		else
		{
			ll left=x/gcd-(x%gcd==0);
			ll right=y/gcd;

			if(right-left==0)
			{
				cout<<-1<<endl;
			}
			else
			{
				cout<<(ceil(x/(ld)gcd))*gcd<<endl;
			}
		}
	}
}

signed main(){

    fast;

    ll t=1;

    while(t--)
    {
        solve();
    }

    return 0;
}


/********************/
/* K B G D E L T A */
/*     DA-IICT      */
/********************/

#include<bits/stdc++.h>
using namespace std;
using   ll=long long;
using   ld=long double;
#define fast           ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define endl           "\n"
#define F(i,n)         for(ll i=0;i<n;i++)
#define rep(i,a,b)     for(ll i=a;i<b;i++)
#define repp(i,a,b)    for(ll i=a;i>=b;i--)
#define pb             push_back
#define mp             make_pair
#define vll            vector<ll>
#define f              first
#define s              second
#define pll            pair<ll,ll>
#define ALL(v)         v.begin(),v.end()
#define ALLR(v)        v.rbegin(),v.rend()
#define pii            3.14159265358979323

void solve()
{

	ll a,b,q,x,y,ans;

	cin>>a>>b;

	ll gcd=__gcd(a,b);

	ans=gcd;

	for(int i=2;i<=sqrt(gcd);i++)
	{
		if(gcd%i==0)
		{
			ans=i;
			break;
		}
	}

	cin>>q;

	while(q--)
	{
		cin>>x>>y;

		ll left=x/ans-(x%ans==0);
		ll right=y/ans;

		if(right-left==0)
		{
			cout<<-1<<endl;
		}
		else
		{
			cout<<(ceil(x/(ld)ans))*ans<<endl;
		}
	}
}

signed main(){

    fast;

    ll t=1;

    while(t--)
    {
        solve();
    }

    return 0;
}


/********************/
/* K B G D E L T A */
/*     DA-IICT      */
/********************/

#include<bits/stdc++.h>
using namespace std;
using   ll=long long;
using   ld=long double;
#define fast           ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define endl           "\n"
#define F(i,n)         for(ll i=0;i<n;i++)
#define rep(i,a,b)     for(ll i=a;i<b;i++)
#define repp(i,a,b)    for(ll i=a;i>=b;i--)
#define pb             push_back
#define mp             make_pair
#define vll            vector<ll>
#define f              first
#define s              second
#define pll            pair<ll,ll>
#define ALL(v)         v.begin(),v.end()
#define ALLR(v)        v.rbegin(),v.rend()
#define pii            3.14159265358979323

void solve()
{

	ll a,b,q,x,y,ans,tt=1;

	cin>>a>>b;

	ll gcd=__gcd(a,b);

	vector<ll> dd;

	for(int i=1;i<=sqrt(gcd);i++)
	{
		if(gcd%i==0)
		{
							dd.pb(i);
			if(gcd/i!=i)	dd.pb(gcd/i);
		}
	}

	sort(ALL(dd));

	cin>>q;

	while(q--)
	{
		cin>>x>>y;

		if(x<=gcd)
		{
			auto it=lower_bound(ALL(dd),y);

			it--;

			if(it==dd.end())
			{
				cout<<-1<<endl;
			}
			else
			{
				if((*it)>y)
				{
					cout<<-1<<endl;
				}
				else
				{
					cout<<(*it)<<endl;
				}
			}
		}
		else
		{
			ll left=x/gcd-(x%gcd==0);
			ll right=y/gcd;

			if(right-left==0)
			{
				cout<<-1<<endl;
			}
			else
			{
				cout<<(ceil(x/(ld)gcd))*gcd<<endl;
			}
		}
	}
}

signed main(){

    fast;

    ll t=1;

    while(t--)
    {
        solve();
    }

    return 0;
}


/********************/
/* K B G D E L T A */
/*     DA-IICT      */
/********************/

#include<bits/stdc++.h>
using namespace std;
using   ll=long long;
using   ld=long double;
#define fast           ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define endl           "\n"
#define F(i,n)         for(ll i=0;i<n;i++)
#define rep(i,a,b)     for(ll i=a;i<b;i++)
#define repp(i,a,b)    for(ll i=a;i>=b;i--)
#define pb             push_back
#define mp             make_pair
#define vll            vector<ll>
#define f              first
#define s              second
#define pll            pair<ll,ll>
#define ALL(v)         v.begin(),v.end()
#define ALLR(v)        v.rbegin(),v.rend()
#define pii            3.14159265358979323

void solve()
{

	ll a,b,q,x,y,ans,tt=1;

	cin>>a>>b;

	ll gcd=__gcd(a,b);

	vector<ll> dd;

	for(int i=1;i<=sqrt(gcd);i++)
	{
		if(gcd%i==0)
		{
							dd.pb(i);
			if(gcd/i!=i)	dd.pb(gcd/i);
		}
	}

	sort(ALL(dd));

	cin>>q;

	while(q--)
	{
		cin>>x>>y;

		if(x<=gcd)
		{
			auto it=upper_bound(ALL(dd),y);

			it--;

			if(it==dd.end() || (*it)<x)
			{
				cout<<-1<<endl;
			}
			else
			{
				if((*it)>y)
				{
					cout<<-1<<endl;
				}
				else
				{
					cout<<(*it)<<endl;
				}
			}
		}
		else
		{
			if(gcd>=x && gcd<=y)
			{
				cout<<gcd<<endl;
			}
			else
			{
				cout<<-1<<endl;
			}
		}
	}
}

signed main(){

    fast;

    ll t=1;

    while(t--)
    {
        solve();
    }

    return 0;
}


/********************/
/* K B G D E L T A */
/*     DA-IICT      */
/********************/

#include<bits/stdc++.h>
using namespace std;
using   ll=long long;
using   ld=long double;
#define fast           ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define endl           "\n"
#define F(i,n)         for(ll i=0;i<n;i++)
#define rep(i,a,b)     for(ll i=a;i<b;i++)
#define repp(i,a,b)    for(ll i=a;i>=b;i--)
#define pb             push_back
#define mp             make_pair
#define vll            vector<ll>
#define f              first
#define s              second
#define pll            pair<ll,ll>
#define ALL(v)         v.begin(),v.end()
#define ALLR(v)        v.rbegin(),v.rend()
#define pii            3.14159265358979323

void solve()
{

	ll a,b,q,x,y,ans,tt=1;

	cin>>a>>b;

	ll gcd=__gcd(a,b);

	vector<ll> dd;

	for(int i=1;i<=sqrt(gcd);i++)
	{
		if(gcd%i==0)
		{
							dd.pb(i);
			if(gcd/i!=i)	dd.pb(gcd/i);
		}
	}

	sort(ALL(dd));

	cin>>q;

	while(q--)
	{
		cin>>x>>y;

		if(x<=gcd)
		{
			auto it=lower_bound(ALL(dd),y);

			it--;

			if(it==dd.end() || (*it)<x)
			{
				cout<<-1<<endl;
			}
			else
			{
				if((*it)>y)
				{
					cout<<-1<<endl;
				}
				else
				{
					cout<<(*it)<<endl;
				}
			}
		}
		else
		{
			if(gcd>=x && gcd<=y)
			{
				cout<<gcd<<endl;
			}
			else
			{
				cout<<-1<<endl;
			}
		}
	}
}

signed main(){

    fast;

    ll t=1;

    while(t--)
    {
        solve();
    }

    return 0;
}


/********************/
/* K B G D E L T A */
/*     DA-IICT      */
/********************/

#include<bits/stdc++.h>
using namespace std;
using   ll=long long;
using   ld=long double;
#define fast           ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define endl           "\n"
#define F(i,n)         for(ll i=0;i<n;i++)
#define rep(i,a,b)     for(ll i=a;i<b;i++)
#define repp(i,a,b)    for(ll i=a;i>=b;i--)
#define pb             push_back
#define mp             make_pair
#define vll            vector<ll>
#define f              first
#define s              second
#define pll            pair<ll,ll>
#define ALL(v)         v.begin(),v.end()
#define ALLR(v)        v.rbegin(),v.rend()
#define pii            3.14159265358979323

void solve()
{

	ll a,b,q,x,y,ans;

	cin>>a>>b;

	ll gcd=__gcd(a,b);

	ans=gcd;

	cin>>q;

	while(q--)
	{
		cin>>x>>y;

		ll left=x/ans-(x%ans==0);
		ll right=y/ans;

		if(right-left==0)
		{
			cout<<-1<<endl;
		}
		else
		{
			cout<<(ceil(x/(ld)ans))*ans<<endl;
		}
	}
}

signed main(){

    fast;

    ll t=1;

    while(t--)
    {
        solve();
    }

    return 0;
}


/********************/
/* K B G D E L T A */
/*     DA-IICT      */
/********************/

#include<bits/stdc++.h>
using namespace std;
using   ll=long long;
using   ld=long double;
#define fast           ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define endl           "\n"
#define F(i,n)         for(ll i=0;i<n;i++)
#define rep(i,a,b)     for(ll i=a;i<b;i++)
#define repp(i,a,b)    for(ll i=a;i>=b;i--)
#define pb             push_back
#define mp             make_pair
#define vll            vector<ll>
#define f              first
#define s              second
#define pll            pair<ll,ll>
#define ALL(v)         v.begin(),v.end()
#define ALLR(v)        v.rbegin(),v.rend()
#define pii            3.14159265358979323

void solve()
{

	ll a,b,q,x,y,ans,tt=1;

	cin>>a>>b;

	ll gcd=__gcd(a,b);

	vector<ll> dd;

	for(int i=1;i<=sqrt(gcd);i++)
	{
		if(gcd%i==0)
		{
							dd.pb(i);
			if(gcd/i!=i)	dd.pb(gcd/i);
		}
	}

	sort(ALL(dd));

	cin>>q;

	while(q--)
	{
		cin>>x>>y;

		if(x<=gcd)
		{
			auto it=lower_bound(ALL(dd),y);

			it--;

			if(it==dd.end())
			{
				cout<<-1<<endl;
			}
			else
			{
				if((*it)>y)
				{
					cout<<-1<<endl;
				}
				else
				{
					cout<<(*it)<<endl;
				}
			}
		}
		else
		{
			if(gcd>=x && gcd<=y)
			{
				cout<<gcd<<endl;
			}
			else
			{
				cout<<-1<<endl;
			}
		}
	}
}

signed main(){

    fast;

    ll t=1;

    while(t--)
    {
        solve();
    }

    return 0;
}


#include<bits/stdc++.h>
#define eb push_back
#define pb pop_back
#define pf push_front
#define M 1000000007
#define int long long int
#define pii pair <int,int>
#define mii map <int,int>
#define mp make_pair
#define endl "\n"
#define forn(i,x,n) for(int i=x;i<n;i++)
#define gcd __gcd
#define vii vector <int>
#define ms1(x) memset(x,-1,sizeof(x))
#define ms0(x) memset(x,0,sizeof(x))
#define bsh binary_search
#define ub upper_bound
#define lb lower_bound
using namespace std;
main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int a,b;
    cin>>a>>b;
    int c=min(a,b);
    //cout<<c<<endl;
    int x=sqrt(c);
    vii v;
    forn(i,1,x)
    {
        if(a%i==0 && b%i==0)
        {
            v.eb(i);
            v.eb(c/i);
            //cout<<i<<" "<<c/i<<endl;
        }
    }

    if(x*x==c)
    {
        if(a%x==0 && b%x==0)
        v.eb(x);
    }
    sort(v.begin(),v.end());
    //forn(i,0,v.size())
    //cout<<v[i]<<" ";
    //cout<<endl;
    int www=1;
    cin>>www;
    while(www--)
    {
        int q1,q2;
        cin>>q1>>q2;
        int flag=0;
        auto it=ub(v.begin(),v.end(),q2);
        if(it==v.end())
        it=v.end()-1;
        while(1)
        {
            if(*it<=q2 && *it>=q1)
            {
                cout<<*it<<endl;
                break;
            }
            else if(*it<q1|| it==v.begin())
            {
                flag=1;
                break;
            }
            it--;
        }
        if(flag)
        cout<<-1<<endl;
    }
}


#include<bits/stdc++.h>
#define eb push_back
#define pb pop_back
#define pf push_front
#define M 1000000007
#define int long long int
#define pii pair <int,int>
#define mii map <int,int>
#define mp make_pair
#define endl "\n"
#define forn(i,x,n) for(int i=x;i<n;i++)
#define gcd __gcd
#define vii vector <int>
#define ms1(x) memset(x,-1,sizeof(x))
#define ms0(x) memset(x,0,sizeof(x))
#define bsh binary_search
#define ub upper_bound
#define lb lower_bound
using namespace std;
main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int a,b;
    cin>>a>>b;
    int c=gcd(a,b);
    //cout<<c<<endl;
    //cout<<c<<endl;
    int x=sqrt(c);
    if(c!=1)
    {
        if(x==1)
        x=2;
    }
    vii v;
    //cout<<x<<endl;
    forn(i,1,x+1)
    {
        if(c%i==0)
        {
            v.eb(i);
            v.eb(c/i);
            //cout<<i<<" "<<c/i<<endl;
        }
    }

    if(x*x==c)
    {
        v.eb(x);
    }
    sort(v.begin(),v.end());
    //forn(i,0,v.size())
    //cout<<v[i]<<" ";
    //cout<<endl;
    int www=1;
    cin>>www;
    while(www--)
    {
        int q1,q2;
        cin>>q1>>q2;
        int flag=0;
        if(v.empty())
        {
            cout<<-1<<endl;
            continue;
        }
        auto it=ub(v.begin(),v.end(),q2);
        if(it==v.end())
        it=v.end()-1;
        while(1)
        {
            if(*it<=q2 && *it>=q1)
            {
                cout<<*it<<endl;
                break;
            }
            else if(*it<q1|| it==v.begin())
            {
                flag=1;
                break;
            }
            it--;
        }
        if(flag)
        cout<<-1<<endl;
    }
}


#include<bits/stdc++.h>
#define eb push_back
#define pb pop_back
#define pf push_front
#define M 1000000007
#define int long long int
#define pii pair <int,int>
#define mii map <int,int>
#define mp make_pair
#define endl "\n"
#define forn(i,x,n) for(int i=x;i<n;i++)
#define gcd __gcd
#define vii vector <int>
#define ms1(x) memset(x,-1,sizeof(x))
#define ms0(x) memset(x,0,sizeof(x))
#define bsh binary_search
#define ub upper_bound
#define lb lower_bound
using namespace std;
main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int a,b;
    cin>>a>>b;
    int c=gcd(a,b);
    //cout<<c<<endl;
    int x=sqrt(c);
    vii v;
    //cout<<x<<endl;
    forn(i,1,x)
    {
        if(c%i==0)
        {
            v.eb(i);
            //cout<<i<<" "<<c/i<<endl;
        }
        int j=c/i;
        if(a%j==0 && b%j==0)
        v.eb(j);
    }

    if(x*x==c)
    {
        if(a%x==0 && b%x==0)
        v.eb(x);
    }
    sort(v.begin(),v.end());
    //forn(i,0,v.size())
    //cout<<v[i]<<" ";
    //cout<<endl;
    int www=1;
    cin>>www;
    while(www--)
    {
        int q1,q2;
        cin>>q1>>q2;
        int flag=0;
        auto it=ub(v.begin(),v.end(),q2);
        if(it==v.end())
        it=v.end()-1;
        while(1)
        {
            if(*it<=q2 && *it>=q1)
            {
                cout<<*it<<endl;
                break;
            }
            else if(*it<q1|| it==v.begin())
            {
                flag=1;
                break;
            }
            it--;
        }
        if(flag)
        cout<<-1<<endl;
    }
}


#include<bits/stdc++.h>
#define eb push_back
#define pb pop_back
#define pf push_front
#define M 1000000007
#define int long long int
#define pii pair <int,int>
#define mii map <int,int>
#define mp make_pair
#define endl "\n"
#define forn(i,x,n) for(int i=x;i<n;i++)
#define gcd __gcd
#define vii vector <int>
#define ms1(x) memset(x,-1,sizeof(x))
#define ms0(x) memset(x,0,sizeof(x))
#define bsh binary_search
#define ub upper_bound
#define lb lower_bound
using namespace std;
main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int a,b;
    cin>>a>>b;
    int c=gcd(a,b);
    //cout<<c<<endl;
    int x=sqrt(c);
    vii v;
    //cout<<x<<endl;
    forn(i,1,x)
    {
        if(c%i==0)
        {
            v.eb(i);
            //cout<<i<<" "<<c/i<<endl;
        }
        int j=c/i;
        if(a%j==0 && b%j==0)
        v.eb(j);
    }

    if(x*x==c)
    {
        if(a%x==0 && b%x==0)
        v.eb(x);
    }
    sort(v.begin(),v.end());
    //forn(i,0,v.size())
    //cout<<v[i]<<" ";
    //cout<<endl;
    int www=1;
    cin>>www;
    while(www--)
    {
        int q1,q2;
        cin>>q1>>q2;
        int flag=0;
        if(v.empty())
        {
            cout<<-1<<endl;
            continue;
        }
        auto it=ub(v.begin(),v.end(),q2);
        if(it==v.end())
        it=v.end()-1;
        while(1)
        {
            if(*it<=q2 && *it>=q1)
            {
                cout<<*it<<endl;
                break;
            }
            else if(*it<q1|| it==v.begin())
            {
                flag=1;
                break;
            }
            it--;
        }
        if(flag)
        cout<<-1<<endl;
    }
}


#include<bits/stdc++.h>
#define eb push_back
#define pb pop_back
#define pf push_front
#define M 1000000007
#define int long long int
#define pii pair <int,int>
#define mii map <int,int>
#define mp make_pair
#define endl "\n"
#define forn(i,x,n) for(int i=x;i<n;i++)
#define gcd __gcd
#define vii vector <int>
#define ms1(x) memset(x,-1,sizeof(x))
#define ms0(x) memset(x,0,sizeof(x))
#define bsh binary_search
#define ub upper_bound
#define lb lower_bound
using namespace std;
main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int a,b;
    cin>>a>>b;
    int c=gcd(a,b);
    cout<<c<<endl;
    //cout<<c<<endl;
    int x=sqrt(c);
    if(c!=1)
    {
        if(x==1)
        x=2;
    }
    vii v;
    //cout<<x<<endl;
    forn(i,1,x+1)
    {
        if(c%i==0)
        {
            v.eb(i);
            v.eb(c/i);
            //cout<<i<<" "<<c/i<<endl;
        }
    }

    if(x*x==c)
    {
        v.eb(x);
    }
    sort(v.begin(),v.end());
    forn(i,0,v.size())
    cout<<v[i]<<" ";
    cout<<endl;
    int www=1;
    cin>>www;
    while(www--)
    {
        int q1,q2;
        cin>>q1>>q2;
        int flag=0;
        if(v.empty())
        {
            cout<<-1<<endl;
            continue;
        }
        auto it=ub(v.begin(),v.end(),q2);
        if(it==v.end())
        it=v.end()-1;
        while(1)
        {
            if(*it<=q2 && *it>=q1)
            {
                cout<<*it<<endl;
                break;
            }
            else if(*it<q1|| it==v.begin())
            {
                flag=1;
                break;
            }
            it--;
        }
        if(flag)
        cout<<-1<<endl;
    }
}


#include<bits/stdc++.h>
#define eb push_back
#define pb pop_back
#define pf push_front
#define M 1000000007
#define int long long int
#define pii pair <int,int>
#define mii map <int,int>
#define mp make_pair
#define endl "\n"
#define forn(i,x,n) for(int i=x;i<n;i++)
#define gcd __gcd
#define vii vector <int>
#define ms1(x) memset(x,-1,sizeof(x))
#define ms0(x) memset(x,0,sizeof(x))
#define bsh binary_search
#define ub upper_bound
#define lb lower_bound
using namespace std;
main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int a,b;
    cin>>a>>b;
    int c=gcd(a,b);
    //cout<<c<<endl;
    //cout<<c<<endl;
    int x=sqrt(c);
    if(c!=1)
    {
        if(x==1)
        x=2;
    }
    vii v;
    //cout<<x<<endl;
    forn(i,1,x)
    {
        if(c%i==0)
        {
            v.eb(i);
            v.eb(c/i);
            //cout<<i<<" "<<c/i<<endl;
        }
    }

    if(x*x==c)
    {
        v.eb(x);
    }
    sort(v.begin(),v.end());
    //forn(i,0,v.size())
    //cout<<v[i]<<" ";
    //cout<<endl;
    int www=1;
    cin>>www;
    while(www--)
    {
        int q1,q2;
        cin>>q1>>q2;
        int flag=0;
        if(v.empty())
        {
            cout<<-1<<endl;
            continue;
        }
        auto it=ub(v.begin(),v.end(),q2);
        if(it==v.end())
        it=v.end()-1;
        while(1)
        {
            if(*it<=q2 && *it>=q1)
            {
                cout<<*it<<endl;
                break;
            }
            else if(*it<q1|| it==v.begin())
            {
                flag=1;
                break;
            }
            it--;
        }
        if(flag)
        cout<<-1<<endl;
    }
}


                               ///****SaJib_ShaYan****///

#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;
#define pb push_back
#define pf push_front
#define mk make_pair
#define pu push
#define po pop
//const int MAX=1000005;

//vector<ll>grap[MAX],vc;
//bool visit[MAX];

void solve()
{
        ll a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,t,u,v,w,x,y,z;

	  vector<ll>vc,vt;
	   ///  vector<pair<ll,char> >vt;
	 string st,ss,sp="";

	 cin>>a>>b>>n;
	 e=__gcd(a,b);
	 vc.pb(e);
	 for(j=1;j*j<=e;j++)
			{
				if(e%j==0){

						vc.pb(j);
				f=e/j;
		      vc.pb(f);
				}
			}
			sort(vc.begin(),vc.end());
	 for(p=1;p<=n;p++)
	 {
	 	cin>>c>>d;

	 	//e=__gcd(a,b);
	 	if(e<c)cout<<-1<<endl;
	 	else if(e<=d)cout<<e<<endl;
	    else
		{
			i=0;
			j=vc.size()-1;
			ll mid=0;
			 vt.clear();
			while(i<=j)
			{
				mid=(i+j)/2;
			  f=vc[mid];
			  if(f<=d)
			  {
			  	if(f<=d&&f>=c)vt.pb(f);
			  	i=mid+1;
			  }
			  else j=mid-1;
			}
			sort(vt.begin(),vt.end());
			if(vt.size()>0)cout<<vt[vt.size()-1]<<endl;
			else cout<<-1<<endl;
		}

	 }

}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
     ll tc;
     //cin>>tc;
     int c=1,i;

        //while(tc--)
        //{
            solve();
        //}

    return 0;
}


                               ///****SaJib_ShaYan****///

#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;
#define pb push_back
#define pf push_front
#define mk make_pair
#define pu push
#define po pop
//const int MAX=1000005;

//vector<ll>grap[MAX],vc;
//bool visit[MAX];

void solve()
{
        ll a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,t,u,v,w,x,y,z;

	  vector<ll>vc;
	   ///  vector<pair<ll,char> >vt;
	 string st,ss,sp="";

	 cin>>a>>b>>n;

	 for(i=1;i<=n;i++)
	 {
	 	cin>>c>>d;

	 	e=__gcd(a,b);
	 	if(e<c)cout<<-1<<endl;
	 	else if(e<=d)cout<<e<<endl;
	    else
		{
			vc.pb(e);
			for(i=1;i*i<=e;i++)
			{
				if(e%i==0)vc.pb(i);
			}
			sort(vc.begin(),vc.end());
			bool ans=false;

			for(i=vc.size()-1;i>=0;i--)
			{
				if(vc[i]>=c&&vc[i]<=d)
				{
					cout<<vc[i]<<endl;
					ans=true;
					break;
				}
			}
			if(!ans)cout<<-1<<endl;
		}

	 }

}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
     ll tc;
     //cin>>tc;
     int c=1,i;

        //while(tc--)
        //{
            solve();
        //}

    return 0;
}


                               ///****SaJib_ShaYan****///

#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;
#define pb push_back
#define pf push_front
#define mk make_pair
#define pu push
#define po pop
//const int MAX=1000005;

//vector<ll>grap[MAX],vc;
//bool visit[MAX];

void solve()
{
        ll a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,t,u,v,w,x,y,z;

	  vector<ll>vc;
	   ///  vector<pair<ll,char> >vt;
	 string st,ss,sp="";

	 cin>>a>>b>>n;

	 for(i=1;i<=n;i++)
	 {
	 	cin>>c>>d;

	 	e=__gcd(a,b);
	 	if(e<c)cout<<-1<<endl;
	 	else if(e<=d)cout<<e<<endl;
	    else
		{
			vc.clear();
			if(e<=d)vc.pb(e);
			for(j=1;j*j<=e;j++)
			{
				if(e%j==0){

						if(j>=c&&j<=d){vc.pb(j);}
				f=e/j;
				if(f>=c&&f<=d){vc.pb(f);}
				}
			}
			sort(vc.begin(),vc.end());
			if(vc.size()>0)cout<<vc[vc.size()-1]<<endl;
			else cout<<-1<<endl;
		}

	 }

}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
     ll tc;
     //cin>>tc;
     int c=1,i;

        //while(tc--)
        //{
            solve();
        //}

    return 0;
}


                               ///****SaJib_ShaYan****///

#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;
#define pb push_back
#define pf push_front
#define mk make_pair
#define pu push
#define po pop
//const int MAX=1000005;

//vector<ll>grap[MAX],vc;
//bool visit[MAX];

void solve()
{
        ll a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,t,u,v,w,x,y,z;

	  vector<ll>vc;
	   ///  vector<pair<ll,char> >vt;
	 string st,ss,sp="";

	 cin>>a>>b>>n;

	 for(i=1;i<=n;i++)
	 {
	 	cin>>c>>d;

	 	e=__gcd(a,b);
	 	if(e<c)cout<<-1<<endl;
	 	else if(e<=d)cout<<e<<endl;
	    else
		{
			vc.pb(e);
			for(i=1;i<=e;i++)
			{
				if(e%i==0){
						vc.pb(i);
						vc.pb(e/i);
				}
			}
			sort(vc.begin(),vc.end());
			bool ans=false;

			for(i=vc.size()-1;i>=0;i--)
			{
				if(vc[i]>=c&&vc[i]<=d)
				{
					cout<<vc[i]<<endl;
					ans=true;
					break;
				}
			}
			if(!ans)cout<<-1<<endl;
		}

	 }

}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
     ll tc;
     //cin>>tc;
     int c=1,i;

        //while(tc--)
        //{
            solve();
        //}

    return 0;
}


                               ///****SaJib_ShaYan****///

#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;
#define pb push_back
#define pf push_front
#define mk make_pair
#define pu push
#define po pop
//const int MAX=1000005;

//vector<ll>grap[MAX],vc;
//bool visit[MAX];

void solve()
{
        ll a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,t,u,v,w,x,y,z;

	  vector<ll>vc,vt;
	   ///  vector<pair<ll,char> >vt;
	 string st,ss,sp="";

	 cin>>a>>b>>n;
	 e=__gcd(a,b);
	 if(e<=d)vc.pb(e);
	 for(j=1;j*j<=e;j++)
			{
				if(e%j==0){

						vc.pb(j);
				f=e/j;
		      vc.pb(f);
				}
			}
			sort(vc.begin(),vc.end());
	 for(p=1;p<=n;p++)
	 {
	 	cin>>c>>d;

	 	e=__gcd(a,b);
	 	if(e<c)cout<<-1<<endl;
	 	else if(e<=d)cout<<e<<endl;
	    else
		{
			i=0;
			j=vc.size()-1;
			ll mid=0;
			 vt.clear();
			while(i<=j)
			{
				mid=(i+j)/2;
			  f=vc[mid];
			  if(f<=d)
			  {
			  	vt.pb(f);
			  	i=mid+1;
			  }
			  else j=mid-1;
			}
			sort(vt.begin(),vt.end());
			if(vt.size()>0)cout<<vt[vt.size()-1]<<endl;
			else cout<<-1<<endl;
		}

	 }

}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
     ll tc;
     //cin>>tc;
     int c=1,i;

        //while(tc--)
        //{
            solve();
        //}

    return 0;
}


                               ///****SaJib_ShaYan****///

#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;
#define pb push_back
#define pf push_front
#define mk make_pair
#define pu push
#define po pop
//const int MAX=1000005;

//vector<ll>grap[MAX],vc;
//bool visit[MAX];

void solve()
{
        ll a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,t,u,v,w,x,y,z;

	  vector<ll>vc;
	   ///  vector<pair<ll,char> >vt;
	 string st,ss,sp="";

	 cin>>a>>b>>n;

	 for(i=1;i<=n;i++)
	 {
	 	cin>>c>>d;

	 	e=__gcd(a,b);
	 	if(e<c)cout<<-1<<endl;
	 	else if(e<=d)cout<<e<<endl;
	    else
		{
			vc.pb(e);
			for(j=1;j*j<=e;j++)
			{
				if(e%j==0){
						vc.pb(j);
						vc.pb(e/j);
				}
			}
			sort(vc.begin(),vc.end());
			bool ans=false;

			for(j=vc.size()-1;j>=0;j--)
			{
				if(vc[j]>=c&&vc[j]<=d)
				{
					cout<<vc[j]<<endl;
					ans=true;
					break;
				}
			}
			if(!ans)cout<<-1<<endl;
		}

	 }

}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
     ll tc;
     //cin>>tc;
     int c=1,i;

        //while(tc--)
        //{
            solve();
        //}

    return 0;
}


                               ///****SaJib_ShaYan****///

#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;
#define pb push_back
#define pf push_front
#define mk make_pair
#define pu push
#define po pop
//const int MAX=1000005;

//vector<ll>grap[MAX],vc;
//bool visit[MAX];

void solve()
{
        ll a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,t,u,v,w,x,y,z;

	  vector<ll>vc,vt;
	   ///  vector<pair<ll,char> >vt;
	 string st,ss,sp="";

	 cin>>a>>b>>n;
	 e=__gcd(a,b);
	 vc.pb(e);
	 for(j=1;j*j<=e;j++)
			{
				if(e%j==0){

						vc.pb(j);
				f=e/j;
		      vc.pb(f);
				}
			}
			sort(vc.begin(),vc.end());
	 for(p=1;p<=n;p++)
	 {
	 	cin>>c>>d;

	 	//e=__gcd(a,b);
	 	if(e<c)cout<<-1<<endl;
	 	else if(e<=d)cout<<e<<endl;
	    else
		{
			i=0;
			j=vc.size()-1;
			ll mid=0;
			 vt.clear();
			while(i<=j)
			{
				mid=(i+j)/2;
			  f=vc[mid];
			  if(f<=d)
			  {
			  	vt.pb(f);
			  	i=mid+1;
			  }
			  else j=mid-1;
			}
			sort(vt.begin(),vt.end());
			if(vt.size()>0)cout<<vt[vt.size()-1]<<endl;
			else cout<<-1<<endl;
		}

	 }

}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
     ll tc;
     //cin>>tc;
     int c=1,i;

        //while(tc--)
        //{
            solve();
        //}

    return 0;
}


                               ///****SaJib_ShaYan****///

#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;
#define pb push_back
#define pf push_front
#define mk make_pair
#define pu push
#define po pop
//const int MAX=1000005;

//vector<ll>grap[MAX],vc;
//bool visit[MAX];

void solve()
{
        ll a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,t,u,v,w,x,y,z;

	  vector<ll>vc;
	   ///  vector<pair<ll,char> >vt;
	 string st,ss,sp="";

	 cin>>a>>b>>n;

	 for(i=1;i<=n;i++)
	 {
	 	cin>>c>>d;

	 	e=__gcd(a,b);
	 	if(e<c)cout<<-1<<endl;
	 	else if(e<=d)cout<<e<<endl;
	    else
		{
			vc.clear();
			if(e<=d)vc.pb(e);
			ll mi=max(c,d);
			mi=min(mi,e);
			for(j=1;j*j<=e&&j<=mi;j++)
			{
				if(e%j==0){

						if(j>=c&&j<=d){vc.pb(j);}
				f=e/j;
				if(f>=c&&f<=d){vc.pb(f);}
				}
			}
			sort(vc.begin(),vc.end());
			if(vc.size()>0)cout<<vc[vc.size()-1]<<endl;
			else cout<<-1<<endl;
		}

	 }

}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
     ll tc;
     //cin>>tc;
     int c=1,i;

        //while(tc--)
        //{
            solve();
        //}

    return 0;
}


                               ///****SaJib_ShaYan****///

#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;
#define pb push_back
#define pf push_front
#define mk make_pair
#define pu push
#define po pop
//const int MAX=1000005;

//vector<ll>grap[MAX],vc;
//bool visit[MAX];

void solve()
{
        ll a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,t,u,v,w,x,y,z;

	  vector<ll>vc,vt;
	   ///  vector<pair<ll,char> >vt;
	 string st,ss,sp="";

	 cin>>a>>b>>n;
	 e=__gcd(a,b);
	 vc.pb(e);
	 for(j=1;j*j<=e;j++)
			{
				if(e%j==0){

						vc.pb(j);
				f=e/j;
		      vc.pb(f);
				}
			}
			sort(vc.begin(),vc.end());
	 for(p=1;p<=n;p++)
	 {
	 	cin>>c>>d;

	 	//e=__gcd(a,b);
	 	if(e<c)cout<<-1<<endl;
	 	else if(e<=d)cout<<e<<endl;
	    else
		{
			i=0;
			j=vc.size()-1;
			ll mid=0;
			 vt.clear();
			while(i<=j)
			{
				mid=(i+j)/2;
			  f=vc[mid];
			  if(f<=d&&f>=c)
			  {
			  	vt.pb(f);
			  	i=mid+1;
			  }
			  else j=mid-1;
			}
			sort(vt.begin(),vt.end());
			if(vt.size()>0)cout<<vt[vt.size()-1]<<endl;
			else cout<<-1<<endl;
		}

	 }

}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
     ll tc;
     //cin>>tc;
     int c=1,i;

        //while(tc--)
        //{
            solve();
        //}

    return 0;
}


#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<list>
#include <algorithm>
#include<vector>
#include<set>
#include <cctype>
#include <cstring>
#include <cstdio>
#include<queue>
#include<stack>
#include<bitset>
#include<time.h>
#include<fstream>

/******************************************************/

using namespace std;

/********************define***************************/
#define ll 				long long
#define ld				long double
#define all(v)				((v).begin()), ((v).end())
#define for_(vec) 		for(int i=0;i<(int)vec.size();i++)
#define lp(j,n)		for(int i=j;i<(int)(n);i++)
#define rlp(j,n)		for(int i=j;i>=(int)(n);i--)
#define clr(arr,x)  memset(arr,x,sizeof(arr))
#define fillstl(arr,X)    fill(arr.begin(),arr.end(),X)
#define pb         push_back
#define mp	   		make_pair
#define print_vector(X)  for(int i=0;i<X.size();i++)

/********************************************************/
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
typedef vector<vector<int> > vvi;
typedef vector<ll> vl;
/***********************function************************/
int dx[4]= {1,-1,0,0};
int dy[4]= {0,0,1,-1};

void fast()
{
    ios_base::sync_with_stdio(NULL);
    cin.tie(0);
    cout.tie(0);
}
void online_judge()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
}
const int flag_max=0x3f3f3f3f;
const ll OO=2e9+10 ;
const double EPS = (1e-8);
int dcmp(double x, double y)
{
    return fabs(x-y) <= EPS ? 0 : x < y ? -1 : 1;
}
ll gcd(ll a, ll b)
{
    return !b ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
    return (a / gcd(a, b)) * b;
}

const ll  MOD=1e9+7;
ll Power(ll n,ll deg)
{
    if(!deg)return 1;
    else  if(deg & 1) return (Power(n,deg-1)*n);
    else
    {
        ll half=Power(n,deg/2);
        return ((half*half));
    }
}

/***********************main_problem***************************************/

vector<ll>w;
void so(ll a, ll b){

    vector<ll>A,B;
    for(ll i=2;i*i<=a;i++){

        if(a%i==0){

            A.push_back(i);

            if(a/i!=i){
                A.push_back(a/i);
            }

        }

    }

    for(ll i=2;i*i<=b;i++){

        if(b%i==0){

            B.push_back(i);

            if(b/i!=i){
                B.push_back(b/i);
            }

        }

    }

    sort(all(A));
    sort(all(B));
    for(int i=0;i<A.size();i++){

        if(binary_search(B.begin(),B.end(),A[i])){
            w.push_back(A[i]);
        }

    }

    if(max(a,b)%min(a,b)==0)w.push_back(min(a,b));
    w.push_back(1);

}

/***********𝓢𝓣𝓞𝓟 𝓦𝓗𝓔𝓝 𝓨𝓞𝓤 𝓡𝓔𝓐𝓒𝓗 𝓣𝓗𝓔 𝓒𝓞𝓝𝓒𝓔𝓟𝓣 𝓞𝓕 𝓓𝓞𝓝'𝓣 𝓢𝓣𝓞𝓟******************/

int main()
{

    fast();

    ll a,b;
    cin>>a>>b;
    so(a,b);
    sort(all(w));

    int q;
    cin>>q;
    while(q--){

    int l,r;
    cin>>l>>r;
    std::vector<ll>::iterator upper1;

    upper1 = std::upper_bound(w.begin(), w.end(),r);

    if((upper1-w.begin())==0)
        cout<<-1<<'\n';
    else if((upper1-w.begin())>0 && w[(upper1-w.begin())-1]>=l)
        cout<<w[(upper1-w.begin())-1]<<'\n';

    else
        cout<<-1<<'\n';

    }

    return 0;

}


#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<list>
#include <algorithm>
#include<vector>
#include<set>
#include <cctype>
#include <cstring>
#include <cstdio>
#include<queue>
#include<stack>
#include<bitset>
#include<time.h>
#include<fstream>

/******************************************************/

using namespace std;

/********************define***************************/
#define ll 				long long
#define ld				long double
#define all(v)				((v).begin()), ((v).end())
#define for_(vec) 		for(int i=0;i<(int)vec.size();i++)
#define lp(j,n)		for(int i=j;i<(int)(n);i++)
#define rlp(j,n)		for(int i=j;i>=(int)(n);i--)
#define clr(arr,x)  memset(arr,x,sizeof(arr))
#define fillstl(arr,X)    fill(arr.begin(),arr.end(),X)
#define pb         push_back
#define mp	   		make_pair
#define print_vector(X)  for(int i=0;i<X.size();i++)

/********************************************************/
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
typedef vector<vector<int> > vvi;
typedef vector<ll> vl;
/***********************function************************/
int dx[4]= {1,-1,0,0};
int dy[4]= {0,0,1,-1};

void fast()
{
    ios_base::sync_with_stdio(NULL);
    cin.tie(0);
    cout.tie(0);
}
void online_judge()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
}
const int flag_max=0x3f3f3f3f;
const ll OO=2e9+10 ;
const double EPS = (1e-8);
int dcmp(double x, double y)
{
    return fabs(x-y) <= EPS ? 0 : x < y ? -1 : 1;
}
ll gcd(ll a, ll b)
{
    return !b ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
    return (a / gcd(a, b)) * b;
}

const ll  MOD=1e9+7;
ll Power(ll n,ll deg)
{
    if(!deg)return 1;
    else  if(deg & 1) return (Power(n,deg-1)*n);
    else
    {
        ll half=Power(n,deg/2);
        return ((half*half));
    }
}

/***********************main_problem***************************************/

vector<ll>w;
void so(ll a, ll b){

    for(ll i=2;i*i<=max(a,b);i++){

        if(a%i==0 && b%i==0){

            w.push_back(i);

            if(a/i!=i && b%(a/i)==0){
                w.push_back(a/i);
            }

            if(b/i!=i && a%(b/i)==0)
                w.push_back(b/i);

        }

    }

    if(max(a,b)%min(a,b)==0)w.push_back(min(a,b));

    w.push_back(1);
}

/***********𝓢𝓣𝓞𝓟 𝓦𝓗𝓔𝓝 𝓨𝓞𝓤 𝓡𝓔𝓐𝓒𝓗 𝓣𝓗𝓔 𝓒𝓞𝓝𝓒𝓔𝓟𝓣 𝓞𝓕 𝓓𝓞𝓝'𝓣 𝓢𝓣𝓞𝓟******************/

int main()
{

    fast();

    ll a,b;
    cin>>a>>b;
    so(a,b);
    sort(all(w));
/*
    for(auto X:w){
        cout<<X<<' ';
    }
  */
    int q;
    cin>>q;
    while(q--){

    int l,r;
    cin>>l>>r;
    std::vector<ll>::iterator upper1;

    upper1 = std::upper_bound(w.begin(), w.end(),r);

    if((upper1-w.begin())==0)
        cout<<-1<<'\n';
    else if((upper1-w.begin())>0 && w[(upper1-w.begin())-1]>=l)
        cout<<w[(upper1-w.begin())-1]<<'\n';

    else
        cout<<-1<<'\n';

    }

    return 0;

}


#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<list>
#include <algorithm>
#include<vector>
#include<set>
#include <cctype>
#include <cstring>
#include <cstdio>
#include<queue>
#include<stack>
#include<bitset>
#include<time.h>
#include<fstream>

/******************************************************/

using namespace std;

/********************define***************************/
#define ll 				long long
#define ld				long double
#define all(v)				((v).begin()), ((v).end())
#define for_(vec) 		for(int i=0;i<(int)vec.size();i++)
#define lp(j,n)		for(int i=j;i<(int)(n);i++)
#define rlp(j,n)		for(int i=j;i>=(int)(n);i--)
#define clr(arr,x)  memset(arr,x,sizeof(arr))
#define fillstl(arr,X)    fill(arr.begin(),arr.end(),X)
#define pb         push_back
#define mp	   		make_pair
#define print_vector(X)  for(int i=0;i<X.size();i++)

/********************************************************/
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
typedef vector<vector<int> > vvi;
typedef vector<ll> vl;
/***********************function************************/
int dx[4]= {1,-1,0,0};
int dy[4]= {0,0,1,-1};

void fast()
{
    ios_base::sync_with_stdio(NULL);
    cin.tie(0);
    cout.tie(0);
}
void online_judge()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
}
const int flag_max=0x3f3f3f3f;
const ll OO=2e9+10 ;
const double EPS = (1e-8);
int dcmp(double x, double y)
{
    return fabs(x-y) <= EPS ? 0 : x < y ? -1 : 1;
}
ll gcd(ll a, ll b)
{
    return !b ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
    return (a / gcd(a, b)) * b;
}

const ll  MOD=1e9+7;
ll Power(ll n,ll deg)
{
    if(!deg)return 1;
    else  if(deg & 1) return (Power(n,deg-1)*n);
    else
    {
        ll half=Power(n,deg/2);
        return ((half*half));
    }
}

/***********************main_problem***************************************/

vector<ll>w;
void so(ll a, ll b){

    for(ll i=1;i*i<=max(a,b);i++){

        if(max(a,b)%i!=0 && min(a,b)%i==0 && max(a,b)%(min(a,b)/i)==0)
                                                    w.push_back(min(a,b)/i);

        if(a%i==0 && b%i==0){

            w.push_back(i);

            if(a/i!=i && b%(a/i)==0){
                w.push_back(a/i);
            }

            if(b/i!=i && a%(b/i)==0)
                w.push_back(b/i);

        }

    }

}

/***********𝓢𝓣𝓞𝓟 𝓦𝓗𝓔𝓝 𝓨𝓞𝓤 𝓡𝓔𝓐𝓒𝓗 𝓣𝓗𝓔 𝓒𝓞𝓝𝓒𝓔𝓟𝓣 𝓞𝓕 𝓓𝓞𝓝'𝓣 𝓢𝓣𝓞𝓟******************/

int main()
{

    fast();

    ll a,b;
    cin>>a>>b;
    so(a,b);
    sort(all(w));
/*
    for(auto X:w){
        cout<<X<<' ';
    }
*/
    int q;
    cin>>q;
    while(q--){

    int l,r;
    cin>>l>>r;
    std::vector<ll>::iterator upper1;

    upper1 = std::upper_bound(w.begin(), w.end(),r);

    if((upper1-w.begin())==0)
        cout<<-1<<'\n';
    else if((upper1-w.begin())>0 && w[(upper1-w.begin())-1]>=l)
        cout<<w[(upper1-w.begin())-1]<<'\n';

    else
        cout<<-1<<'\n';

    }

    return 0;

}


#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<list>
#include <algorithm>
#include<vector>
#include<set>
#include <cctype>
#include <cstring>
#include <cstdio>
#include<queue>
#include<stack>
#include<bitset>
#include<time.h>
#include<fstream>

/******************************************************/

using namespace std;

/********************define***************************/
#define ll 				long long
#define ld				long double
#define all(v)				((v).begin()), ((v).end())
#define for_(vec) 		for(int i=0;i<(int)vec.size();i++)
#define lp(j,n)		for(int i=j;i<(int)(n);i++)
#define rlp(j,n)		for(int i=j;i>=(int)(n);i--)
#define clr(arr,x)  memset(arr,x,sizeof(arr))
#define fillstl(arr,X)    fill(arr.begin(),arr.end(),X)
#define pb         push_back
#define mp	   		make_pair
#define print_vector(X)  for(int i=0;i<X.size();i++)

/********************************************************/
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
typedef vector<vector<int> > vvi;
typedef vector<ll> vl;
/***********************function************************/
int dx[4]= {1,-1,0,0};
int dy[4]= {0,0,1,-1};

void fast()
{
    ios_base::sync_with_stdio(NULL);
    cin.tie(0);
    cout.tie(0);
}
void online_judge()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
}
const int flag_max=0x3f3f3f3f;
const ll OO=2e9+10 ;
const double EPS = (1e-8);
int dcmp(double x, double y)
{
    return fabs(x-y) <= EPS ? 0 : x < y ? -1 : 1;
}
ll gcd(ll a, ll b)
{
    return !b ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
    return (a / gcd(a, b)) * b;
}

const ll  MOD=1e9+7;
ll Power(ll n,ll deg)
{
    if(!deg)return 1;
    else  if(deg & 1) return (Power(n,deg-1)*n);
    else
    {
        ll half=Power(n,deg/2);
        return ((half*half));
    }
}

/***********************main_problem***************************************/

vector<ll>w;
void so(ll a, ll b)
{

    ll f=gcd(a,b);
    for(ll i=1; i*i<=f; i++)
    {

        if(f%i==0)
        {

            w.push_back(i);

            if(a/i!=i)
                w.push_back(f/i);

        }

    }

}

/***********𝓢𝓣𝓞𝓟 𝓦𝓗𝓔𝓝 𝓨𝓞𝓤 𝓡𝓔𝓐𝓒𝓗 𝓣𝓗𝓔 𝓒𝓞𝓝𝓒𝓔𝓟𝓣 𝓞𝓕 𝓓𝓞𝓝'𝓣 𝓢𝓣𝓞𝓟******************/

int main()
{

    fast();

    ll a,b;
    cin>>a>>b;
    so(a,b);
    sort(all(w));
    /*
        for(auto X:w){
            cout<<X<<' ';
        }
    */
    int q;
    cin>>q;
    while(q--)
    {

        int l,r;
        cin>>l>>r;
        std::vector<ll>::iterator upper1;

        upper1 = std::upper_bound(w.begin(), w.end(),r);

        if((upper1-w.begin())==0)
            cout<<-1<<'\n';
        else if((upper1-w.begin())>0 && w[(upper1-w.begin())-1]>=l)
            cout<<w[(upper1-w.begin())-1]<<'\n';

        else
            cout<<-1<<'\n';

    }

    return 0;

}


#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<list>
#include <algorithm>
#include<vector>
#include<set>
#include <cctype>
#include <cstring>
#include <cstdio>
#include<queue>
#include<stack>
#include<bitset>
#include<time.h>
#include<fstream>

/******************************************************/

using namespace std;

/********************define***************************/
#define ll 				long long
#define ld				long double
#define all(v)				((v).begin()), ((v).end())
#define for_(vec) 		for(int i=0;i<(int)vec.size();i++)
#define lp(j,n)		for(int i=j;i<(int)(n);i++)
#define rlp(j,n)		for(int i=j;i>=(int)(n);i--)
#define clr(arr,x)  memset(arr,x,sizeof(arr))
#define fillstl(arr,X)    fill(arr.begin(),arr.end(),X)
#define pb         push_back
#define mp	   		make_pair
#define print_vector(X)  for(int i=0;i<X.size();i++)

/********************************************************/
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
typedef vector<vector<int> > vvi;
typedef vector<ll> vl;
/***********************function************************/
int dx[4]= {1,-1,0,0};
int dy[4]= {0,0,1,-1};

void fast()
{
    ios_base::sync_with_stdio(NULL);
    cin.tie(0);
    cout.tie(0);
}
void online_judge()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
}
const int flag_max=0x3f3f3f3f;
const ll OO=2e9+10 ;
const double EPS = (1e-8);
int dcmp(double x, double y)
{
    return fabs(x-y) <= EPS ? 0 : x < y ? -1 : 1;
}
ll gcd(ll a, ll b)
{
    return !b ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
    return (a / gcd(a, b)) * b;
}

const ll  MOD=1e9+7;
ll Power(ll n,ll deg)
{
    if(!deg)return 1;
    else  if(deg & 1) return (Power(n,deg-1)*n);
    else
    {
        ll half=Power(n,deg/2);
        return ((half*half));
    }
}

/***********************main_problem***************************************/

vector<ll>w;
void so(ll a, ll b){

    for(ll i=2;i*i<=max(a,b);i++){

        if(max(a,b)%i!=0 && min(a,b)%i==0 && max(a,b)%(min(a,b)/i)==0)
                                                    w.push_back(min(a,b)/i);

        if(a%i==0 && b%i==0){

            w.push_back(i);

            if(a/i!=i && b%(a/i)==0){
                w.push_back(a/i);
            }

            if(b/i!=i && a%(b/i)==0)
                w.push_back(b/i);

        }

    }

    if(max(a,b)%min(a,b)==0)w.push_back(min(a,b));

    w.push_back(1);
}

/***********𝓢𝓣𝓞𝓟 𝓦𝓗𝓔𝓝 𝓨𝓞𝓤 𝓡𝓔𝓐𝓒𝓗 𝓣𝓗𝓔 𝓒𝓞𝓝𝓒𝓔𝓟𝓣 𝓞𝓕 𝓓𝓞𝓝'𝓣 𝓢𝓣𝓞𝓟******************/

int main()
{

    fast();

    ll a,b;
    cin>>a>>b;
    so(a,b);
    sort(all(w));
/*
    for(auto X:w){
        cout<<X<<' ';
    }
*/
    int q;
    cin>>q;
    while(q--){

    int l,r;
    cin>>l>>r;
    std::vector<ll>::iterator upper1;

    upper1 = std::upper_bound(w.begin(), w.end(),r);

    if((upper1-w.begin())==0)
        cout<<-1<<'\n';
    else if((upper1-w.begin())>0 && w[(upper1-w.begin())-1]>=l)
        cout<<w[(upper1-w.begin())-1]<<'\n';

    else
        cout<<-1<<'\n';

    }

    return 0;

}


#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<list>
#include <algorithm>
#include<vector>
#include<set>
#include <cctype>
#include <cstring>
#include <cstdio>
#include<queue>
#include<stack>
#include<bitset>
#include<time.h>
#include<fstream>

/******************************************************/

using namespace std;

/********************define***************************/
#define ll 				long long
#define ld				long double
#define all(v)				((v).begin()), ((v).end())
#define for_(vec) 		for(int i=0;i<(int)vec.size();i++)
#define lp(j,n)		for(int i=j;i<(int)(n);i++)
#define rlp(j,n)		for(int i=j;i>=(int)(n);i--)
#define clr(arr,x)  memset(arr,x,sizeof(arr))
#define fillstl(arr,X)    fill(arr.begin(),arr.end(),X)
#define pb         push_back
#define mp	   		make_pair
#define print_vector(X)  for(int i=0;i<X.size();i++)

/********************************************************/
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
typedef vector<vector<int> > vvi;
typedef vector<ll> vl;
/***********************function************************/
int dx[4]= {1,-1,0,0};
int dy[4]= {0,0,1,-1};

void fast()
{
    ios_base::sync_with_stdio(NULL);
    cin.tie(0);
    cout.tie(0);
}
void online_judge()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
}
const int flag_max=0x3f3f3f3f;
const ll OO=2e9+10 ;
const double EPS = (1e-8);
int dcmp(double x, double y)
{
    return fabs(x-y) <= EPS ? 0 : x < y ? -1 : 1;
}
ll gcd(ll a, ll b)
{
    return !b ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
    return (a / gcd(a, b)) * b;
}

const ll  MOD=1e9+7;
ll Power(ll n,ll deg)
{
    if(!deg)return 1;
    else  if(deg & 1) return (Power(n,deg-1)*n);
    else
    {
        ll half=Power(n,deg/2);
        return ((half*half));
    }
}

/***********************main_problem***************************************/

vector<ll>w;
void so(ll a, ll b){

    vector<ll>A,B;
    for(ll i=2;i*i<=a;i++){

        if(a%i==0){

            A.push_back(i);

            if(a/i!=i){
                A.push_back(a/i);
            }

        }

    }

    for(ll i=2;i*i<=b;i++){

        if(b%i==0){

            B.push_back(i);

            if(b/i!=i){
                B.push_back(b/i);
            }

        }

    }

    sort(all(A));
    sort(all(B));
    for(int i=0;i<A.size();i++){

        if(binary_search(B.begin(),B.end(),A[i])){
            w.push_back(A[i]);
        }

    }

    if(max(a,b)%min(a,b)==0)w.push_back(min(a,b));

}

/***********𝓢𝓣𝓞𝓟 𝓦𝓗𝓔𝓝 𝓨𝓞𝓤 𝓡𝓔𝓐𝓒𝓗 𝓣𝓗𝓔 𝓒𝓞𝓝𝓒𝓔𝓟𝓣 𝓞𝓕 𝓓𝓞𝓝'𝓣 𝓢𝓣𝓞𝓟******************/

int main()
{

    fast();

    ll a,b;
    cin>>a>>b;
    so(a,b);
    sort(all(w));

    int q;
    cin>>q;
    while(q--){

    int l,r;
    cin>>l>>r;
    std::vector<ll>::iterator upper1;

    upper1 = std::upper_bound(w.begin(), w.end(),r);

    if((upper1-w.begin())==0)
        cout<<-1<<'\n';
    else if((upper1-w.begin())>0 && w[(upper1-w.begin())-1]>=l)
        cout<<w[(upper1-w.begin())-1]<<'\n';

    else
        cout<<-1<<'\n';

    }

    return 0;

}


#include<bits/stdc++.h>
using namespace std;
unordered_map<int,int> m;
unordered_map<int,int> mm;
vector<int> vec;
int binary(int f,int s)
{
    int mid;
    int l=0;
    int r=vec.size()-1;
int ans=-1;
while(l<=r)
{
    mid=(l+r)/2;
    if(vec[mid]>=f&&vec[mid]<=s)
    {
        ans=vec[mid];
        l=mid+1;
    }
else if(vec[mid]<f)
l=mid+1;
else if(vec[mid]>s)
r=mid-1;

}
return ans;

}
int main()
{
    int f,s;
    cin>>f>>s;
    int i,j;
    for(i=1;i<=int(sqrt(f));i++)
    {
        if(f%i==0)
        {
            m[i]=1;
            if(f/i!=i)
            m[f/i]=1;
        }
    }
for(i=1;i<=int(sqrt(s));i++)
{
    if(s%i==0&&m.count(i)>0)
    {
        mm[s]=1;
        if(s/i!=i&&m.count(s/i)>0)
        mm[s/i]=1;
    }
}
    for(auto it=mm.begin();it!=mm.end();it++)
    vec.push_back(it->first);
    sort(vec.begin(),vec.end());
    int q;
    cin>>q;
    int k;
    while(q)
    {
        q--;
        cin>>f>>s;
        cout<<binary(f,s)<<"\n";
    }
}


#include<bits/stdc++.h>
using namespace std;
unordered_map<int,int> m;
unordered_map<int,int> mm;
vector<int> vec;
int binary(int f,int s)
{
    int mid;
    int l=0;
    int r=vec.size();
int ans=-1;
while(l<=r)
{
    mid=(l+r)/2;
    if(vec[mid]>=f&&vec[mid]<=s)
    {
        ans=mid;
        l=mid+1;
    }
else if(vec[mid]<f)
l=mid+1;
else if(vec[mid]>s)
r=mid-1;

}
return ans;

}
int main()
{
    int f,s;
    cin>>f>>s;
    int i,j;
    for(i=1;i<=int(sqrt(f));i++)
    {
        if(f%i==0)
        {
            m[i]=1;
            if(f/i!=i)
            m[f/i]=1;
        }
    }
for(i=1;i<=int(sqrt(s));i++)
{
    if(s%i==0&&m.count(i)>0)
    {
        mm[s]=1;
        if(s/i!=i&&m.count(s/i)>0)
        mm[s/i]=1;
    }
}
    for(auto it=mm.begin();it!=mm.end();it++)
    vec.push_back(it->first);
    sort(vec.begin(),vec.end());
    int q;
    cin>>q;
    int k;
    while(q)
    {
        q--;
        cin>>f>>s;
        cout<<binary(f,s)<<"\n";
    }
}


#include<bits/stdc++.h>
using namespace std;
unordered_map<int,int> m;
unordered_map<int,int> mm;
vector<int> vec;
int binary(int f,int s)
{
    int mid;
    int l=0;
    int r=vec.size();
int ans=-1;
while(l<=r)
{
    mid=(l+r)/2;
    if(mid>=f&&mid<=s)
    {
        ans=mid;
        l=mid+1;
    }
else if(mid<f)
l=mid+1;
else if(mid>s)
r=mid-1;

}
return ans;

}
int main()
{
    int f,s;
    cin>>f>>s;
    int i,j;
    for(i=1;i<=int(sqrt(f));i++)
    {
        if(f%i==0)
        {
            m[i]=1;
            if(f/i!=i)
            m[f/i]=1;
        }
    }
for(i=1;i<=int(sqrt(s));i++)
{
    if(s%i==0&&m.count(i)>0)
    {
        mm[s]=1;
        if(s/i!=i&&m.count(s/i)>0)
        mm[s/i]=1;
    }
}
    for(auto it=mm.begin();it!=mm.end();it++)
    vec.push_back(it->first);
    sort(vec.begin(),vec.end());
    int q;
    cin>>q;
    int k;
    while(q)
    {
        q--;
        cin>>f>>s;
        cout<<binary(f,s)<<"\n";
    }
}


#include<bits/stdc++.h>
using namespaec std;
unordered_map<int,int> m;
unordered_map<int,int> mm;
vector<int> vec;
int binary(int f,int s)
{
    int mid;
    int l=0;
    int r=vec.size();
int ans=-1;
while(l<=r)
{
    mid=(l+r)/2;
    if(mid>=f&&mid<=s)
    {
        ans=mid;
        l=mid+1;
    }
else if(mid<f)
l=mid+1;
else if(mid>s)
r=mid-1;

}
return ans;

}
int main()
{
    int f,s;
    cin>>f>>s;
    int i,j;
    for(i=1;i<=int(sqrt(f));i++)
    {
        if(f%i==0)
        {
            m[i]=1;
            if(f/i!=i)
            m[f/i]=1;
        }
    }
for(i=1;i<=int(sqrt(s));i++)
{
    if(s%i==0&&m.count(i)>0)
    {
        mm[s]=1;
        if(s/i!=i&&m.count(s/i)>0)
        mm[s/i]=1;
    }
}
    for(auto it=mm.begin();it!=mm.end();it++)
    vec.push_back(it->first);
    sort(vec.begin(),vec.end());
    int q;
    cin>>q;
    int k;
    while(q)
    {
        q--;
        cin>>f>>s;
        cout<<binary(f,s)<<"\n";
    }
}


#include<bits/stdc++.h>
using namespace std;
unordered_map<int,int> m;
unordered_map<int,int> mm;
vector<int> vec;
int binary(int f,int s)
{
    int mid;
    int l=0;
    int r=vec.size()-1;
int ans=-1;
while(l<=r)
{
    mid=(l+r)/2;
    if(vec[mid]>=f&&vec[mid]<=s)
    {
        ans=vec[mid];
        l=mid+1;
    }
else if(vec[mid]<f)
l=mid+1;
else if(vec[mid]>s)
r=mid-1;

}
return ans;

}
int main()
{
    int f,s;
    cin>>f>>s;
    int i,j;
    for(i=1;i<=int(sqrt(f));i++)
    {
        if(f%i==0)
        {
            m[i]=1;
            if(f/i!=i)
            m[f/i]=1;
        }
    }
for(i=1;i<=int(sqrt(s));i++)
{
    if(s%i==0&&m.count(i)>0)
    {
        mm[i]=1;

    }
    if(s%i==0&&m.count(s/i)>0)
    mm[s/i]=1;
}
    for(auto it=mm.begin();it!=mm.end();it++)
    vec.push_back(it->first);
    sort(vec.begin(),vec.end());

    int q;
    cin>>q;
    int k;
    while(q)
    {
        q--;
        cin>>f>>s;
        cout<<binary(f,s)<<"\n";
    }
}


#include<bits/stdc++.h>
using namespace std;
unordered_map<int,int> m;
unordered_map<int,int> mm;
vector<int> vec;
int binary(int f,int s)
{
    int mid;
    int l=0;
    int r=vec.size()-1;
int ans=-1;
while(l<=r)
{
    mid=(l+r)/2;
    if(vec[mid]>=f&&vec[mid]<=s)
    {
        ans=vec[mid];
        l=mid+1;
    }
else if(vec[mid]<f)
l=mid+1;
else if(vec[mid]>s)
r=mid-1;

}
return ans;

}
int main()
{
    int f,s;
    cin>>f>>s;
    int i,j;
    for(i=1;i<=int(sqrt(f));i++)
    {
        if(f%i==0)
        {
            m[i]=1;
            if(f/i!=i)
            m[f/i]=1;
        }
    }
for(i=1;i<=int(sqrt(s));i++)
{
    if(s%i==0&&m.count(i)>0)
    {
        mm[i]=1;
        if(s/i!=i&&m.count(s/i)>0)
        mm[s/i]=1;
    }
}
    for(auto it=mm.begin();it!=mm.end();it++)
    vec.push_back(it->first);
    sort(vec.begin(),vec.end());

    int q;
    cin>>q;
    int k;
    while(q)
    {
        q--;
        cin>>f>>s;
        cout<<binary(f,s)<<"\n";
    }
}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int a , b , c ;
vector <int> vc;
int main(){
fcin;
    cin >> a >> b;
    for(int i = 1 ; i <= min(a,b) ; i++){
        if(a%i==0 && b%i==0) vc.pb(i);
    }
    int q;
    cin >> q;
    while(q--){
        int l , r ;
        cin >> l >> r;
        auto x = lower_bound(vc.begin(),vc.end(),l);
        if(*x>=l && *x<=r) cout << *x << "\n";
        else
        cout  << "-1\n";
    }

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int a , b , c , l , r , indx;
vector <int> vc;
int main(){
fcin;
    cin >> a >> b;
    if(a>b) swap(a,b);
    for(int i = 1 ; i <= sqrt(a) ; i++){
        if(a%i==0){
        int x = i;
        int y = a/i;
        if(x==y){
            if(b%x==0) vc.pb(x);
        }
        else{
        if(b%x==0){vc.pb(x);}
        if(b%y==0){vc.pb(y);}
        }
        }
    }
    sort(vc.begin(),vc.end());
    int q;
    cin >> q;
    while(q--){
        cin >> l >> r;
        auto x = upper_bound(vc.begin(),vc.end(),r);
        x--;
        if(x==vc.begin() || x==vc.end()) cout <<"-1\n";
        else{
        if((*x) >= l && (*x) <= r) cout << (*x) << "\n";
        else cout <<"-1\n";
        }
    }

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int a , b , c , l , r , indx;
vector <int> vc;
int main(){
fcin;
    cin >> a >> b;
    for(int i = 1 ; i*i <= min(a,b) ; i++){
        if(a%i==0 && b%i==0){
         if(i*i==a) vc.pb(i);
         else{
         vc.pb(i);
         if(b%(a/i)==0) vc.pb(a/i);
            }
        }
    }
    sort(vc.begin(),vc.end());
    // vc.erase(unique(vc.begin(),vc.end()));
    int q;
    cin >> q;
    while(q--){
        cin >> l >> r;
        auto x = upper_bound(vc.begin(),vc.end(),r);
        x--;
        if(x==vc.begin() || x==vc.end()) cout <<"-1\n";
        else{
        if((*x) >= l && (*x) <= r) cout << (*x) << "\n";
        else cout <<"-1\n";
        }
    }

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int a , b , c , l , r , indx;
vector <int> vc;
int main(){
fcin;
    cin >> a >> b;
    for(int i = 1 ; i*i <= min(a,b) ; i++){
        if(a%i==0 && b%i==0){
         vc.pb(i);
         if(b%(a/i)==0) vc.pb(a/i);
        }
    }
    sort(vc.begin(),vc.end());
    vc.erase(unique(vc.begin(),vc.end()));
    int q;
    cin >> q;
    while(q--){
        cin >> l >> r;
        auto x = upper_bound(vc.begin(),vc.end(),r);
        x--;
        if(x==vc.begin() || x==vc.end()) cout <<"-1\n";
        else{
        if((*x) >= l && (*x) <= r) cout << (*x) << "\n";
        else cout <<"-1\n";
        }
    }

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int a , b , c , l , r , indx;
vector <int> vc;
int main(){
fcin;
    cin >> a >> b;
    for(int i = 1 ; i <= sqrt(max(a,b)) ; i++){
        if(i<min(a,b) && a%i==0 && b%i==0){
         if(i*i==a) vc.pb(i);
         else{
         vc.pb(i);
         if(b%(a/i)==0) vc.pb(a/i);
            }
        }
    }
    sort(vc.begin(),vc.end());
    int q;
    cin >> q;
    while(q--){
        cin >> l >> r;
        auto x = upper_bound(vc.begin(),vc.end(),r);
        x--;
        if(x==vc.begin() || x==vc.end()) cout <<"-1\n";
        else{
        if((*x) >= l && (*x) <= r) cout << (*x) << "\n";
        else cout <<"-1\n";
        }
    }

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int a , b , c , l , r , indx;
vector <int> vc;
int main(){
fcin;
    cin >> a >> b;
    if(a>b) swap(a,b);
    for(int i = 1 ; i <= sqrt(a) ; i++){
        if(a%i==0){
        int x = i;
        int y = a/i;
        if(x==y){
            if(b%x==0) vc.pb(x);
        }
        else{
        if(b%x==0){vc.pb(x);}
        if(b%y==0){vc.pb(y);}
        }
        }
    }
    sort(vc.begin(),vc.end());
    int q;
    cin >> q;
    while(q--){
        cin >> l >> r;
        auto x = upper_bound(vc.begin(),vc.end(),r);
        x--;
        if( x==vc.end()) cout <<"-1\n";
        else{
        if((*x) >= l && (*x) <= r) cout << (*x) << "\n";
        else cout <<"-1\n";
        }
    }

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int a , b , c , l , r , indx;
vector <int> vc;
int main(){
fcin;
    cin >> a >> b;
    for(int i = 1 ; i <= sqrt(max(a,b)) ; i++){
        if(a%i==0 && b%i==0){
         if(i*i==a) vc.pb(i);
         else{
         vc.pb(i);
         if(b%(a/i)==0) vc.pb(a/i);
            }
        }
    }
    // for(auto each : vc) cout<<each<<" ";
    sort(vc.begin(),vc.end());
    // vc.erase(unique(vc.begin(),vc.end()));
    int q;
    cin >> q;
    while(q--){
        cin >> l >> r;
        auto x = upper_bound(vc.begin(),vc.end(),r);
        x--;
        if(x==vc.begin() || x==vc.end()) cout <<"-1\n";
        else{
        if((*x) >= l && (*x) <= r) cout << (*x) << "\n";
        else cout <<"-1\n";
        }
    }

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int a , b , c , l , r , indx;
vector <int> vc;
int main(){
fcin;
    cin >> a >> b;
    for(int i = 1 ; i <= min(a,b) ; i++){
        if(a%i==0 && b%i==0) vc.pb(i);
    }
    int q;
    cin >> q;
    while(q--){
        cin >> l >> r;
        auto x = upper_bound(vc.begin(),vc.end(),r);
        x--;
        if(x==vc.begin() || x==vc.end()) cout <<"-1\n";
        else{
        if((*x) >= l && (*x) <= r) cout << (*x) << "\n";
        else cout <<"-1\n";
        }
    }

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int a , b , c , l , r , indx;
vector <int> vc;
int main(){
fcin;
    cin >> a >> b;
    for(int i = 1 ; i <= min(a,b) ; i++){
        if(a%i==0 && b%i==0) vc.pb(i);
    }
    int q;
    cin >> q;
    while(q--){
        cin >> l >> r;
        int tl = 0 , tr = vc.size();
        while(tl <= tr){
            int tmid = (tl + tr)/2;
            if(vc[tmid] > r){
                tr = tmid - 1;
            }
            else if(vc[tmid] < l){
                tl = tmid + 1;
            }
            else {
                indx = tmid;
                tl = tmid + 1;
            }
        }
        if(vc[indx] >= l && vc[indx] <= r) cout << vc[indx] << "\n";
        else cout <<"-1\n";
    }

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int a , b , c , l , r , indx;
vector <int> vc;
int main(){
fcin;
    cin >> a >> b;
    for(int i = 1 ; i <= min(a,b) ; i++){
        if(a%i==0 && b%i==0) vc.pb(i);
    }
    int q;
    cin >> q;
    while(q--){
        cin >> l >> r;
        auto x = upper_bound(vc.begin(),vc.end(),r);
        x--;
        // cout << indx << "\n";
        if((*x) >= l && (*x) <= r) cout << (*x) << "\n";
        else cout <<"-1\n";
    }

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int a , b , c , l , r , indx;
vector <int> vc;
int main(){
fcin;
    cin >> a >> b;
    for(int i = 1 ; i <= sqrt(min(a,b)) ; i++){
        if(a%i==0){
        int x = i;
        int y = a/i;
        if(x==y){
            if(b%x==0) vc.pb(x);
        }
        else{
        if(b%x==0){vc.pb(x);}
        if(b%y==0){vc.pb(y);}
        }
        }
    }
    sort(vc.begin(),vc.end());
    int q;
    cin >> q;
    while(q--){
        cin >> l >> r;
        auto x = upper_bound(vc.begin(),vc.end(),r);
        x--;
        if(x==vc.begin() || x==vc.end()) cout <<"-1\n";
        else{
        if((*x) >= l && (*x) <= r) cout << (*x) << "\n";
        else cout <<"-1\n";
        }
    }

}


#include <fstream>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_map>
#include <string>
using namespace std;

ifstream fin("input.txt");
ofstream fout("output.txt");
//int v[5010];

#include <vector>
#include <cmath>
#include <iostream>

int gcd(int a, int b){
    if(b==0)
        return a;
    else return gcd(b,a%b);
}

int main() { // main di test della struttura
    int a,b,n,mi,ma;
    cin>>a>>b>>n;
    vector<int> div;
    int gc = gcd(a,b);
    for(int i = 1; i <= sqrt(gc)+1; i++){
        if(a%i == 0 && b%i==0)
            div.push_back(i);
    }
    div.push_back(gc);
    for(int i = 0; i < n; i++){
        cin>>mi>>ma;
        int m = *(--upper_bound(div.begin(), div.end(), ma));
        if( m >= mi)
            cout<<m<<endl;
        else
            cout<<-1<<endl;

    }
}


#include <fstream>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_map>
#include <string>
using namespace std;

ifstream fin("input.txt");
ofstream fout("output.txt");
//int v[5010];

#include <vector>
#include <cmath>
#include <iostream>

int gcd(int a, int b){
    if(b==0)
        return a;
    else return gcd(b,a%b);
}

int main() { // main di test della struttura
    int a,b,n,mi,ma;
    cin>>a>>b>>n;
    vector<int> div;
    int gc = gcd(a,b);
    for(int i = 1; i <= sqrt(gc); i++){
        if(a%i == 0 && b%i==0){
            div.push_back(i);
            div.push_back(gc/i);
        }
    }
    if(div[div.size()-1] < gc)
    div.push_back(gc);

    sort(div.begin(),div.end());
    cout<<endl;
    for(int i = 0; i < n; i++){
        cin>>mi>>ma;
        int m = *(--upper_bound(div.begin(), div.end(), ma));
        if( m >= mi)
            cout<<m<<endl;
        else
            cout<<-1<<endl;

    }
}


#include <fstream>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_map>
#include <string>
using namespace std;

ifstream fin("input.txt");
ofstream fout("output.txt");
//int v[5010];

#include <vector>
#include <cmath>
#include <iostream>

int gcd(int a, int b){
    if(b==0)
        return a;
    else return gcd(b,a%b);
}

int main() { // main di test della struttura
    int a,b,n,mi,ma;
    cin>>a>>b>>n;
    vector<int> div;
    int gc = gcd(a,b);
    for(int i = 1; i <= gc; i++){
        if(a%i == 0 && b%i==0)
            div.push_back(i);
    }

    for(int i = 0; i < n; i++){
        cin>>mi>>ma;
        int m = *(--upper_bound(div.begin(), div.end(), ma));
        if( m >= mi)
            cout<<m<<endl;
        else
            cout<<-1<<endl;

    }
}


#include <fstream>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_map>
#include <string>
using namespace std;

ifstream fin("input.txt");
ofstream fout("output.txt");
//int v[5010];

#include <vector>
#include <cmath>
#include <iostream>

int gcd(int a, int b){
    if(b==0)
        return a;
    else return gcd(b,a%b);
}

int main() { // main di test della struttura
    int a,b,n,mi,ma;
    cin>>a>>b>>n;
    vector<int> div;
    int gc = gcd(a,b);
    for(int i = 1; i <= sqrt(gc); i++){
        if(a%i == 0 && b%i==0)
            div.push_back(i);
    }

    for(int i = 0; i < n; i++){
        cin>>mi>>ma;
        int m = *(--upper_bound(div.begin(), div.end(), ma));
        if( m >= mi)
            cout<<m<<endl;
        else
            cout<<-1<<endl;

    }
}


#include <fstream>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_map>
#include <string>
using namespace std;

ifstream fin("input.txt");
ofstream fout("output.txt");
//int v[5010];

#include <vector>
#include <cmath>
#include <iostream>

int gcd(int a, int b){
    if(b==0)
        return a;
    else return gcd(b,a%b);
}

int main() { // main di test della struttura
    int a,b,n,mi,ma;
    cin>>a>>b>>n;
    vector<int> div;
    int gc = gcd(a,b);
    for(int i = 1; i <= sqrt(gc); i++){
        if(a%i == 0 && b%i==0)
            div.push_back(i);
    }
    if(div[div.size()-1] < gc)
    div.push_back(gc);
    for(int i = 0; i < n; i++){
        cin>>mi>>ma;
        int m = *(--upper_bound(div.begin(), div.end(), ma));
        if( m >= mi)
            cout<<m<<endl;
        else
            cout<<-1<<endl;

    }
}


#include <fstream>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_map>
#include <string>
using namespace std;

ifstream fin("input.txt");
ofstream fout("output.txt");
//int v[5010];

#include <vector>
#include <cmath>
#include <iostream>

int gcd(int a, int b){
    if(b==0)
        return a;
    else return gcd(b,a%b);
}

int main() { // main di test della struttura
    int a,b,n,mi,ma;
    cin>>a>>b>>n;
    vector<int> div;
    int gc = gcd(a,b);
    for(int i = 1; i <= sqrt(gc); i++){
        if(a%i == 0 && b%i==0){
            div.push_back(i);
            div.push_back(gc/i);
        }
    }
    if(div[div.size()-1] < gc)
    div.push_back(gc);

    for(int i = 0; i < div.size(); i++){
        cout<<div[i]<< " ";
    }
    cout<<endl;
    for(int i = 0; i < n; i++){
        cin>>mi>>ma;
        int m = *(--upper_bound(div.begin(), div.end(), ma));
        if( m >= mi)
            cout<<m<<endl;
        else
            cout<<-1<<endl;

    }
}


#include <fstream>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_map>
#include <string>
using namespace std;

ifstream fin("input.txt");
ofstream fout("output.txt");
//int v[5010];

#include <vector>
#include <cmath>
#include <iostream>

int gcd(int a, int b){
    if(b==0)
        return a;
    else return gcd(b,a%b);
}

int main() { // main di test della struttura
    int a,b,n,mi,ma;
    cin>>a>>b>>n;
    vector<int> div;
    int gc = gcd(a,b);
    for(int i = 1; i <= sqrt(gc); i++){
        if(a%i == 0 && b%i==0){
            div.push_back(i);
            div.push_back(gc/i);
        }
    }
    if(div[div.size()-1] < gc)
    div.push_back(gc);

    cout<<endl;
    for(int i = 0; i < n; i++){
        cin>>mi>>ma;
        int m = *(--upper_bound(div.begin(), div.end(), ma));
        if( m >= mi)
            cout<<m<<endl;
        else
            cout<<-1<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("O3")
#define int long long
#define KOD ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
#define sz size()
#define S second
#define X(a,b) a = ((b) > (a)) ? (b) : (a);
#define N(a,b) a = ((b) > (a)) ? (a) : (b);
#define F first
#define ret exit(0);
#define SORT(x) sort((x).begin(),(x).end())
#define Rev(a) reverse((a).begin() , (a).end());
#define pb push_back
#define eb emplace_back
#define pre(x) cout.precision((x));
#define bit __builtin_popcount
#define is(a) insert((a));
#define all(x) (x).begin(),(x).end()
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout);
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef unordered_map<int,int> wi;
typedef unordered_set<int> si;
const int md = 1e9 + 7;
inline int pw(int a, int n, int res = 1){
	while (n) {
		if (n & 1) {
			res *= a;
			res%=md;
		}
		a *= a;
		a%=md;
		n >>= 1;
	}
	return res;
}
inline int read(){int s=0,f=1;char ch=getchar();
 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 while(ch>='0'&&ch<='9'){s=(s<<1)+(s<<3)+ch-'0';ch=getchar();}
 return s*f;}
const int N = 1e5+20;
const int E = 1e6+20;
const long long inf = 1e18;
vi v ;
int a , b , tt , lo ,hg , gcd1;
main(){
	cin >> a >> b;
	gcd1 = __gcd(a,b);
	v.pb(gcd1);
	for(int i = 1 ; i * i <= gcd1 ; i++){
		if(gcd1 % i == 0){
			v.pb(i);
			if(gcd1 / i != i) v.pb(gcd1/i);
		}
	}
	SORT(v);
	cin >> tt;
	while(tt--){
		cin >> lo >> hg;
		int c = lower_bound(all(v),hg) - v.begin();
		if(v[c] >= lo && v[c] <= hg) cout << v[c] << '\n';
		else if(v[c-1] >= lo && v[c-1] <= hg) cout << v[c-1] << '\n';
		else cout << "-1\n";
	}
}


#include<bits/stdc++.h>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("O3")
#define int long long
#define KOD ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
#define sz size()
#define S second
#define X(a,b) a = ((b) > (a)) ? (b) : (a);
#define N(a,b) a = ((b) > (a)) ? (a) : (b);
#define F first
#define ret exit(0);
#define SORT(x) sort((x).begin(),(x).end())
#define Rev(a) reverse((a).begin() , (a).end());
#define pb push_back
#define eb emplace_back
#define pre(x) cout.precision((x));
#define bit __builtin_popcount
#define is(a) insert((a));
#define all(x) (x).begin(),(x).end()
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout);
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef unordered_map<int,int> wi;
typedef unordered_set<int> si;
const int md = 1e9 + 7;
inline int pw(int a, int n, int res = 1){
	while (n) {
		if (n & 1) {
			res *= a;
			res%=md;
		}
		a *= a;
		a%=md;
		n >>= 1;
	}
	return res;
}
inline int read(){int s=0,f=1;char ch=getchar();
 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 while(ch>='0'&&ch<='9'){s=(s<<1)+(s<<3)+ch-'0';ch=getchar();}
 return s*f;}
const int N = 1e5+20;
const int E = 1e6+20;
const long long inf = 1e18;
vi v ;
int a , b , tt , lo ,hg , gcd1;
main(){
	cin >> a >> b;
	gcd1 = __gcd(a,b);
	v.pb(gcd1);
	for(int i = 1 ; i * i <= gcd1 ; i++){
		if(gcd1 % i == 0){
			v.pb(i);
			if(gcd1 / i != i) v.pb(gcd1/i);
		}
	}
	SORT(v);
	cin >> tt;
	while(tt--){
		cin >> lo >> hg;
		int c = *lower_bound(all(v),lo);
		if(c >= lo && c <= hg) cout << c << '\n';
		else cout << "-1\n";
	}
}


#include<bits/stdc++.h>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("O3")
#define int long long
#define KOD ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
#define sz size()
#define S second
#define X(a,b) a = ((b) > (a)) ? (b) : (a);
#define N(a,b) a = ((b) > (a)) ? (a) : (b);
#define F first
#define ret exit(0);
#define SORT(x) sort((x).begin(),(x).end())
#define Rev(a) reverse((a).begin() , (a).end());
#define pb push_back
#define eb emplace_back
#define pre(x) cout.precision((x));
#define bit __builtin_popcount
#define is(a) insert((a));
#define all(x) (x).begin(),(x).end()
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout);
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef unordered_map<int,int> wi;
typedef unordered_set<int> si;
const int md = 1e9 + 7;
inline int pw(int a, int n, int res = 1){
	while (n) {
		if (n & 1) {
			res *= a;
			res%=md;
		}
		a *= a;
		a%=md;
		n >>= 1;
	}
	return res;
}
inline int read(){int s=0,f=1;char ch=getchar();
 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 while(ch>='0'&&ch<='9'){s=(s<<1)+(s<<3)+ch-'0';ch=getchar();}
 return s*f;}
const int N = 1e5+20;
const int E = 1e6+20;
const long long inf = 1e18;
vi v ;
int a , b , tt , lo ,hg , gcd;
signed main(){KOD
	cin >> a >> b;
	gcd = __gcd(a,b);
	v.pb(gcd);
	for(int i = 1 ; i * i <=gcd ; i++){
		if(gcd % i == 0){
			v.pb(i);
			if(gcd / i != i) v.pb(gcd/i);
		}
	}
	SORT(v);
	cin >> tt;
	while(tt--){
		cin >> lo >> hg;
		int c = *lower_bound(all(v),lo);
		if(c >= lo && c <= hg) cout << c << '\n';
		else cout << "-1\n";
	}
}


#include<bits/stdc++.h>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("O3")
#define int long long
#define KOD ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
#define sz size()
#define S second
#define X(a,b) a = ((b) > (a)) ? (b) : (a);
#define N(a,b) a = ((b) > (a)) ? (a) : (b);
#define F first
#define ret exit(0);
#define SORT(x) sort((x).begin(),(x).end())
#define Rev(a) reverse((a).begin() , (a).end());
#define pb push_back
#define eb emplace_back
#define pre(x) cout.precision((x));
#define bit __builtin_popcount
#define is(a) insert((a));
#define all(x) (x).begin(),(x).end()
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout);
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef unordered_map<int,int> wi;
typedef unordered_set<int> si;
const int md = 1e9 + 7;
inline int pw(int a, int n, int res = 1){
	while (n) {
		if (n & 1) {
			res *= a;
			res%=md;
		}
		a *= a;
		a%=md;
		n >>= 1;
	}
	return res;
}
inline int read(){int s=0,f=1;char ch=getchar();
 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 while(ch>='0'&&ch<='9'){s=(s<<1)+(s<<3)+ch-'0';ch=getchar();}
 return s*f;}
const int N = 1e5+20;
const int E = 1e6+20;
const long long inf = 1e18;
vi v ;
int a , b , tt , lo ,hg , gcd;
signed main(){KOD
	cin >> a >> b;
	gcd1 = __gcd(a,b);
	v.pb(gcd1);
	for(int i = 1 ; i * i <= gcd1 ; i++){
		if(gcd1 % i == 0){
			v.pb(i);
			if(gcd1 / i != i) v.pb(gcd1/i);
		}
	}
	SORT(v);
	cin >> tt;
	while(tt--){
		cin >> lo >> hg;
		int c = *lower_bound(all(v),lo);
		if(c >= lo && c <= hg) cout << c << '\n';
		else cout << "-1\n";
	}
}


#include <bits/stdc++.h>
using namespace std;
#define loop(i,N) for(int i=0;i<N;i++)
#define lli long long int

int main() {
	lli n,k;
	cin>>n>>k;
	int ar[n];
	int ans=0;
	loop(i,n)	cin>>ar[i], ans=(ar[i]>ans)?ar[i]:ans;
	loop(i,n){
		int j=i+1;
		while(j<n and ar[i]>ar[j])
			j++;
		j-=1;
		if((j-i) >= k){
			ans=ar[i];
			break;
		}
	}
	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define loop(i,N) for(int i=0;i<N;i++)
#define lli long long int

int main() {
	lli n,k;
	cin>>n>>k;
	int ar[n],m=0;
	loop(i,n)	cin>>ar[i], m=(ar[i]>m)? ar[i]: m ;
	lli ans=0;
	loop(i,n){
		lli j=i+1;
		while(j<n and ar[i]>ar[j])
			j++;
		if((j-i)>=k){
			ans=1ll*ar[i];
			break;
		}
	}
	if(ans)
		cout<<ans<<endl;
	else
		cout<<m<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define loop(i,N) for(int i=0;i<N;i++)
#define lli long long int

int main() {
	lli n,k;
	cin>>n>>k;
	int ar[n],m=0;
	loop(i,n)	cin>>ar[i], m=(ar[i]>m)? ar[i]: m ;
	lli ans=0;
	loop(i,n){
		lli j=i+1;
		while(j<n and ar[i]>ar[j])
			j++;
		if(j>=k){
			ans=1ll*ar[i];
			break;
		}
	}
	if(ans)
		cout<<ans<<endl;
	else
		cout<<m<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define loop(i,N) for(int i=0;i<N;i++)
#define lli long long int

int main() {
	lli n,k;
	cin>>n>>k;
	int ar[n];
	int ans=0;
	loop(i,n)	cin>>ar[i], ans=(ar[i]>ans)?ar[i]:ans;
	loop(i,n){
		int j=0;
		while(j<n and ar[i]>ar[j])
			j++;
		j-=1;
		if(j-i >= k){
			ans=ar[i];
			break;
		}
	}
	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define loop(i,N) for(int i=0;i<N;i++)
#define lli long long int

int main() {
	lli n,k;
	cin>>n>>k;
	int ar[n];
	int ans=0;
	loop(i,n)	cin>>ar[i], ans=(ar[i]>ans)?ar[i]:ans;
	loop(i,n){
		int end=i+1,start=0;
		while(end<n and ar[end]<ar[i])
			end++;
		if(end==n)	end-=1;
		while(start<i and ar[start]<ar[i])
			start++;
		if(start==i) 	start-=1;
		if(1ll*(start+1+end-i) >= k){
			ans=ar[i];
			break;
		}
	}
	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define loop(i,N) for(int i=0;i<N;i++)
#define lli long long int

int main() {
	lli n,k;
	cin>>n>>k;
	int ar[n],m=0;
	loop(i,n)	cin>>ar[i], m=(ar[i]>m)? ar[i]: m ;
	lli ans=0;
	if(k<n){
	loop(i,n){
		lli j=i+1;
		while(j<n and ar[i]>ar[j])
			j++;
		if(j<n and (j-1ll*i)>=k){
			ans=1ll*ar[i];
			break;
		}
		else if(j==n and (j-1ll*i-1)>=k){
			ans=1ll*ar[i];
			break;
		}
	}
	if(ans)
		cout<<ans<<endl;
	else
		cout<<m<<endl;
	}
	else
		cout<<m<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define loop(i,N) for(int i=0;i<N;i++)
#define lli long long int

int main() {
	lli n,k;
	cin>>n>>k;
	int ar[n],m=0;
	loop(i,n)	cin>>ar[i], m=(ar[i]>m)? ar[i]: m ;
	lli ans=0;
	if(k<n){
	loop(i,n){
		lli j=i+1;
		while(j<n and ar[i]>ar[j])
			j++;
		if(j<n and (j-1ll*i)>=k){
			ans=1ll*ar[i];
			break;
		}
		else if(j>n and (j-1ll*k-1)>=k){
			ans=1ll*ar[i];
			break;
		}
	}
	if(ans)
		cout<<ans<<endl;
	else
		cout<<m<<endl;
	}
	else
		cout<<m<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define loop(i,N) for(int i=0;i<N;i++)
#define lli long long int

int main() {
	lli n,k;
	cin>>n>>k;
	int ar[n];
	int ans=0;
	loop(i,n)	cin>>ar[i], ans=(ans>ar[i])?ans:ar[i];
	int q[n];
	int back=-1;
	int s=0;
	loop(i,n){
		if(ar[s]<ar[i]){
			q[0]=ar[s];
			s=i;
			back=0;
		}
		else if(ar[s]>ar[i]){
			q[++back]=ar[i];
		}
		if((1ll*(back+1))==k){
			ans=ar[s];
			break;
		}
	}
	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define loop(i,N) for(int i=0;i<N;i++)
#define lli long long int

int main() {
	lli n,k;
	cin>>n>>k;
	int ar[n],m=0;
	loop(i,n)	cin>>ar[i], m=(ar[i]>m)? ar[i]: m ;
	lli ans=0;
	if(k<n){
	loop(i,n){
		lli j=i+1;
		while(j<n and ar[i]>ar[j])
			j++;
		if((j-1ll*i)>=k){
			ans=1ll*ar[i];
			break;
		}
	}
	cout<<ans<<endl;
	}
	else
		cout<<m<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define loop(i,N) for(int i=0;i<N;i++)
#define lli long long int

int main() {
	lli n,k;
	cin>>n>>k;
	int ar[n];
	int ans=0;
	loop(i,n)	cin>>ar[i], ans=(ar[i]>ans)?ar[i]:ans;
	loop(i,n){
		int end=i+1,start=0;
		while(end<n and ar[end]<ar[i])
			end++;
		if(end==n)	end-=1;
		while(start<i and ar[start]<ar[i])
			start++;
		if(start==i) 	start-=1;
		if(start+end >= k){
			ans=ar[i];
			break;
		}
	}
	cout<<ans<<endl;
	return 0;
}