#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 100000 + 20;
int prime[maxn];//这个记得用int,他保存的是质数,可以不用开maxn那么大
bool check[maxn];
int total;
bool is_prime[maxn];
int his[maxn][50 + 20];
int Size[maxn];
void initprime() {
for (int i = 2; i <= maxn - 20; i++) {
if (!check[i]) { //是质数了
prime[++total] = i; //只能这样记录,因为后面要用
is_prime[i] = true;
}
for (int j = 1; j <= total; j++) { //质数或者合数都进行的
if (i * prime[j] > maxn - 20) break;
check[i * prime[j]] = 1;
if (i % prime[j] == 0) break;
//关键,使得它只被最小的质数筛去。例如i等于6的时候。
//当时的质数只有2,3,5。6和2结合筛去了12,就break了
//18留下等9的时候,9*2=18筛去
}
}
// Size[1] = 1;
// his[1][1] = 1;
for (int i = 1; i <= maxn - 20; ++i) {
if (is_prime[i]) {
Size[i] = 1;
his[i][1] = i;
continue;
}
int t = i;
for (int j = 1; j <= total; ++j) {
if (prime[j] > t) break;
if (t % prime[j] == 0) {
his[i][++Size[i]] = prime[j];
t /= prime[j];
while (t % prime[j] == 0) {
t /= prime[j];
}
}
}
}
return ;
}
int phi[maxn];
void init_phi() {
phi[1] = 1;
for (int i = 2; i <= maxn - 20; i++) {
if (!phi[i]) {
for (int j = i; j <= maxn - 20; j += i) {
if (!phi[j]) phi[j] = j;
phi[j] = phi[j] / i * (i - 1);
}
}
}
return ;
}
LL calc(int up, int cur, int number, int tobuild, int flag) {
LL ans = 0;
for (int i = cur; i <= Size[number]; ++i) {
if (flag == 0) {
ans += up / (his[number][i] * tobuild) + calc(up, i + 1, number, tobuild * his[number][i], !flag);
} else ans += -up / (his[number][i] * tobuild) + calc(up, i + 1, number, tobuild * his[number][i], !flag);
}
return ans;
}
int f;
void work() {
int L1, R1, L2, R2, k;
scanf("%d%d%d%d%d", &L1, &R1, &L2, &R2, &k);
if (k == 0) {
printf("Case %d: 0\n", ++f);
return;
}
R1 /= k;
R2 /= k;
if (R1 < R2) swap(R1, R2);
LL ans = 0;
for (int i = 1; i <= R2; ++i) {
ans += phi[i];
}
for (int i = R2 + 1; i <= R1; ++i) {
ans += R2 - calc(R2, 1, i, 1, 0);
}
printf("Case %d: %I64d\n", ++f, ans);
}
int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
initprime();
init_phi();
// cout << 24 - calc(24, 1, 12, 1, 0) << endl;
// cout << 12 - calc(12, 1, 12, 1, 0) << endl;
// for (int i = 1; i <= Size[10]; ++i) {
// printf("%d ", his[10][i]);
// }
int t;
scanf("%d", &t);
while (t--) {
work();
}
return 0;
}