uses math;
type
big=record
num,typ:longint;
end;
const
max1=(trunc(sqrt(2000000000))+1);
var
prhash:array[1..max1] of longint;
prs:array[1..max1] of longint;
prnum,i,j,maxx,minx,k:longint;
a0,a1,b0,b1,biga0,biga1,bigb0,bigb1,n,ans,x:longint;
a0num,a1num,b0num,b1num:array[1..max1+4] of longint;
temp2:array[1..4] of longint;
temp:array[1..4] of big;
t:big;
flag:boolean;
label 9;
function comp(a,b:longint):longint;
begin
if a>b then exit(1);
if a=b then exit(0);
exit(-1);
end;
procedure getnum(a:longint;var num:array of longint;var bignum:longint);
var i,j,sqt:longint;
begin
for i:=1 to prnum do
begin
while a mod prs[i]=0 do
begin
inc(num[i]);
a:=a div prs[i];
end;
if a=1 then break;
if (a<max1) and (prhash[a]>0) then begin inc(num[prhash[a]]);break;end;
end;
if a>prs[prnum] then bignum:=a
else bignum:=1;
end;
begin
filldword(prhash,sizeof(prhash),1);
prhash[1]:=0;
for i:=2 to trunc(sqrt(max1)) do
begin
if prhash[i]>0
then begin
for j:=i to (max1 div i) do
prhash[j*i]:=0;
end;
end;
prnum:=0;
for i:=1 to max1 do
if prhash[i]>0
then begin
prnum:=prnum+1;
prs[prnum]:=i;
prhash[i]:=prnum;
//write(i,' ');
end;
readln(n);
for i:=1 to n do
begin
readln(a0,a1,b0,b1);
fillchar(a1num,sizeof(a1num),0);
fillchar(a0num,sizeof(a0num),0);
fillchar(b1num,sizeof(b1num),0);
fillchar(b0num,sizeof(b0num),0);
getnum(a1,a1num,temp[1].num);
temp[1].typ:=1;
getnum(a0,a0num,temp[2].num);
temp[2].typ:=2;
getnum(b0,b0num,temp[3].num);
temp[3].typ:=3;
getnum(b1,b1num,temp[4].num);
temp[4].typ:=4;
for j:=1 to 4 do
for k:=j+1 to 4 do
if temp[j].num>temp[k].num
then begin
t:=temp[j];
temp[j]:=temp[k];
temp[k]:=t;
end;
x:=prnum+1;
for j:=1 to 4 do
begin
if (j<>1) and (temp[j].num<>temp[j-1].num) then inc(x);
case temp[j].typ of
1:a1num[x]:=1;
2:a0num[x]:=1;
3:b0num[x]:=1;
4:b1num[x]:=1;
end;
temp2[x-prnum]:=temp[j].num;
end;
flag:=false;
ans:=1;
for j:=1 to prnum+4 do
begin
if (j>prnum) and (temp2[j-prnum]=1) then continue;
case comp(a0num[j],a1num[j]) of
1:begin
maxx:=a1num[j];
minx:=a1num[j];
end;
0:begin
maxx:=maxlongint;
minx:=a1num[j];
end;
-1:begin
//writeln(0);
flag:=true;
break;
end;
end;
case comp(b0num[j],b1num[j]) of
1:begin
//writeln(0);
flag:=true;
break;
end;
0:maxx:=min(maxx,b0num[j]);
-1:begin
maxx:=min(maxx,b1num[j]);
minx:=max(minx,b1num[j]);
end;
end;
ans:=ans*(maxx-minx+1);
//if ans<0 then break;
end;
if (ans<0) or flag then begin writeln(0);continue;end;
writeln(ans);
end;
end.