#include<stdio.h>
int t,num,l,r,a,b,i,max,flag;int temp;int gcd(int n,int m){ if(n<m) { t=n; n=m; m=t; } while(n!=0) { r=m%n; m=n; n=r; } return (m);}int main(){ while(~scanf("%d%d",&a,&b)) { temp=gcd(a,b); scanf("%d",&num); while(num--) { max=-1; scanf("%d%d",&l,&r); for(i=r<temp?r:temp;i>=l;i--) if(temp%i==0) { max=i; flag=1; break; } if(flag) printf("%d\n",max); else printf("-1\n"); } } }