#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long ll;
double z[10];
void exgcd(ll a,ll b,ll &d,ll &x,ll &y){
if(!b){d=a;x=1;y=0;return;}
exgcd(b,a%b,d,y,x);y-=x*(a/b);
}
int main(){
ll a,b,c,p,q,r,s,x,y,d,k;
int n;cin>>n;
while(n--){
cin>>a>>b>>c>>p>>q>>r>>s;c=-c;
if(p>q||r>s||(!a&&!b&&c)){cout<<0<<endl;continue;}
if(!a||!b){
if(!a) a=q-p+1;
else if(c%a==0&&(c/a)<=q&&(c/a)>=p) a=1;
else a=0;
if(!b) b=s-r+1;
else if(c%b==0&&(c/b)<=s&&(c/b)>=r) b=1;
else b=0;
cout<<a*b<<endl;
continue;
}
exgcd(a,b,d,x,y);
if(c%d!=0){cout<<0<<endl;continue;}
k=c/d;x*=k;y*=k;
a=a/d;b=b/d;
z[0]=(q-x)*1.0/b;
z[1]=(p-x)*1.0/b;
z[2]=(y-s)*1.0/a;
z[3]=(y-r)*1.0/a;
sort(z,z+4);
a=floor(z[2]);b=ceil(z[1]);
cout<<a-b+1<<endl;
}
return 0;
}
#include<cstdio>
#include<iostream>
#define ll long long
using namespace std;
ll n,a,b,c,p,q,r,l,ans;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a>>b>>c>>p>>q>>l>>r;
ans=0;
if(b==0){
for(int x=p;x<=q;x++){
if(x*a==-c) ans++;
}
cout<<ans*(r-l+1)<<endl;
}
else{
for(int x=p;x<=q;x++){
if((-c-a*x)%b==0&&(-c-a*x)*1.0/b>=l&&(-c-a*x)*1.0/b<=r) ans++;
}
cout<<ans<<endl;
}
}
return 0;
}