ll@t;
ll d[1344],i;
rep(t){
	ll@a,@b;
	i=Divisor(b-a,d);
	if(d[--i]<a){
		wt(-1);
	}else{
		while(i&&d[i-1]>=a){
			--i;
		}
		wt(d[i]-a);
	}
}