#include <bits/stdc++.h>
using namespace std;
template <typename T>
T mmul(T a, T b, T m)
{
a %= m;
T result = 0;
while (b)
{
if (b % 2) result = (result + a) % m;
a = (a + a) % m;
b /= 2;
}
return result;
}
template <typename T>
T mpow(T a, T b, T m)
{
a %= m;
T result = 1;
while (b)
{
if (b % 2) result = mmul(result, a, m);
a = mmul(a, a, m);
b /= 2;
}
return result;
}
int main()
{
unsigned long long ts,cs=1;
cin>>ts;
while(ts--)
{
unsigned long long a,b1,c;
cin>>a>>b1>>c;
printf("Case %llu: ",cs++);
cout<<mpow(a,b1,c)<<endl;
}
}
using namespace std;
template <typename T>
T mmul(T a, T b, T m)
{
a %= m;
T result = 0;
while (b)
{
if (b % 2) result = (result + a) % m;
a = (a + a) % m;
b /= 2;
}
return result;
}
template <typename T>
T mpow(T a, T b, T m)
{
a %= m;
T result = 1;
while (b)
{
if (b % 2) result = mmul(result, a, m);
a = mmul(a, a, m);
b /= 2;
}
return result;
}
int main()
{
unsigned long long ts,cs=1;
cin>>ts;
while(ts--)
{
unsigned long long a,b1,c;
cin>>a>>b1>>c;
printf("Case %llu: ",cs++);
cout<<mpow(a,b1,c)<<endl;
}
}