int n; int pri[maxn],pcnt,nop[maxn],mu[maxn]; int k;
void GetPrime() { mu[1]=1,nop[1]=1; for(register int i=2;i<=N;++i) { if(!nop[i])pri[++pcnt]=i,mu[i]=-1; for(register int j=1;j<=pcnt && i*pri[j]<=N;++j) { nop[i*pri[j]]=1; if(i%pri[j]==0)break; else mu[i*pri[j]]=-mu[i]; } } for(register int i=1;i<=N;++i)mu[i]+=mu[i-1]; }
int Calc(int a,int b) { int re=0,up=min(a,b)/k; for(register int l=1,r;l<=up;l=r+1) { r=min(a/(a/l),b/(b/l)); re+=(mu[r]-mu[l-1])*(a/(l*k))*(b/(l*k)); } return re; }
int main() { GetPrime(); read(n); while(n--) { int a,b,c,d; read(a),read(b),read(c),read(d),read(k); printf("%d\n",Calc(b,d)-Calc(b,c-1)-Calc(a-1,d)+Calc(a-1,c-1)); } return 0; }