#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll MM,MOD;
ll sum(ll A,ll B){
A=(A+MOD)%MOD;
B=(B+MOD)%MOD;
ll ans = (A+B)%MOD;
ans=(ans+MOD)%MOD;
return ans;
}
ll f(ll a,ll n){
ll res=a,ans=0;
while(n){
if(n%2) ans=(ans+res)%MOD;
res=(res+res)%MOD;
n/=2;
}
return ans;
}
struct matran{
ll a[3][3];
void print(){
for(ll i=0;i<3;i++){
for(ll j=0;j<3;j++) cout<<a[i][j]<<" ";
cout<<'\n';
}
}
} mot,M;
struct boba{
ll f_n2,f_n1,f_n;
void print(){
cout<<f_n2<<" "<<f_n1<<" "<<f_n<<'\n';
}
} init;
matran prod(matran A,matran B){
matran C;
for(ll i=0;i<3;i++)
for(ll j=0;j<3;j++) C.a[i][j]=0;
for(ll i=0;i<3;i++){
for(ll j=0;j<3;j++){
for(ll k=0;k<3;k++) C.a[i][j]=sum(C.a[i][j],f(A.a[i][k],B.a[k][j]));
}
}
return C;
}
matran po(matran A,ll n){
matran res=A,ans=mot;
while(n){
if(n%2) ans=prod(ans,res);
res=prod(res,res);
n/=2;
}
return ans;
}
boba prod1(boba p,matran A){
boba ans;
for(ll j=0;j<3;j++){
if(j==0){
ans.f_n2 = sum(sum(f(p.f_n2,A.a[0][j]),f(p.f_n1,A.a[1][j])),f(p.f_n,A.a[2][j]));
}
else if(j==1){
ans.f_n1 = sum(sum(f(p.f_n2,A.a[0][j]),f(p.f_n1,A.a[1][j])),f(p.f_n,A.a[2][j]));
}
else ans.f_n = sum(sum(f(p.f_n2,A.a[0][j]),f(p.f_n1,A.a[1][j])),f(p.f_n,A.a[2][j]));
}
return ans;
}
int main(){
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
ll tc;
cin>>tc;
for(ll u=1;u<=tc;u++){
ll tmp[3][3]={ {2,1,0},{0,0,1},{1,0,0}};
ll donvi[3][3]={{1,0,0},{0,1,0},{0,0,1}};
for(ll i=0;i<3;i++)
for(ll j=0;j<3;j++) {
M.a[i][j]=tmp[i][j];
mot.a[i][j]=donvi[i][j];
}
init.f_n2=4;
init.f_n1=2;
init.f_n=1;
// init.print();
ll n;
MM = 10007;
cin>>n;
MOD=MM;
if(n==1){
cout<<"Case "<<u<<": "<<1<<'\n';
continue;
}
if(n==2){
cout<<"Case "<<u<<": "<<2<<'\n';
continue;
}
boba ress = prod1(init,po(M,n-2));
ll aans = (ress.f_n2%MOD-(ress.f_n1%MOD)+MOD)%MOD;
cout<<"Case "<<u<<": "<<aans<<'\n';
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBsbCBsb25nIGxvbmcKbGwgTU0sTU9EOwpsbCBzdW0obGwgQSxsbCBCKXsKICAgICAgICBBPShBK01PRCklTU9EOwogICAgICAgIEI9KEIrTU9EKSVNT0Q7CiAgICAgICAgbGwgYW5zID0gKEErQiklTU9EOwogICAgICAgIGFucz0oYW5zK01PRCklTU9EOwogICAgICAgIHJldHVybiBhbnM7Cn0KbGwgZihsbCBhLGxsIG4pewogICAgICAgIGxsIHJlcz1hLGFucz0wOwogICAgICAgIHdoaWxlKG4pewogICAgICAgICAgICAgICAgaWYobiUyKSBhbnM9KGFucytyZXMpJU1PRDsKICAgICAgICAgICAgICAgIHJlcz0ocmVzK3JlcyklTU9EOwogICAgICAgICAgICAgICAgbi89MjsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGFuczsKfQpzdHJ1Y3QgbWF0cmFuewogICAgICAgIGxsIGFbM11bM107CiAgICAgICAgdm9pZCBwcmludCgpewogICAgICAgICAgICAgICAgZm9yKGxsIGk9MDtpPDM7aSsrKXsKICAgICAgICAgICAgICAgICAgICAgICAgZm9yKGxsIGo9MDtqPDM7aisrKSBjb3V0PDxhW2ldW2pdPDwiICI7CiAgICAgICAgICAgICAgICAgICAgICAgIGNvdXQ8PCdcbic7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgfQp9IG1vdCxNOwpzdHJ1Y3QgYm9iYXsKICAgICAgICBsbCBmX24yLGZfbjEsZl9uOwogICAgICAgIHZvaWQgcHJpbnQoKXsKICAgICAgICAgICAgICAgIGNvdXQ8PGZfbjI8PCIgIjw8Zl9uMTw8IiAiPDxmX248PCdcbic7CiAgICAgICAgfQp9IGluaXQ7Cm1hdHJhbiBwcm9kKG1hdHJhbiBBLG1hdHJhbiBCKXsKICAgICAgICBtYXRyYW4gQzsKICAgICAgICBmb3IobGwgaT0wO2k8MztpKyspCiAgICAgICAgICAgZm9yKGxsIGo9MDtqPDM7aisrKSBDLmFbaV1bal09MDsKICAgICAgICBmb3IobGwgaT0wO2k8MztpKyspewogICAgICAgICAgICAgICAgZm9yKGxsIGo9MDtqPDM7aisrKXsKICAgICAgICAgICAgICAgICAgICAgICAgZm9yKGxsIGs9MDtrPDM7aysrKSBDLmFbaV1bal09c3VtKEMuYVtpXVtqXSxmKEEuYVtpXVtrXSxCLmFba11bal0pKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIEM7Cn0KbWF0cmFuIHBvKG1hdHJhbiBBLGxsIG4pewogICAgICAgIG1hdHJhbiByZXM9QSxhbnM9bW90OwogICAgICAgIHdoaWxlKG4pewogICAgICAgICAgICAgICAgaWYobiUyKSBhbnM9cHJvZChhbnMscmVzKTsKICAgICAgICAgICAgICAgIHJlcz1wcm9kKHJlcyxyZXMpOwogICAgICAgICAgICAgICAgbi89MjsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGFuczsKfQpib2JhIHByb2QxKGJvYmEgcCxtYXRyYW4gQSl7CiAgICAgICAgYm9iYSBhbnM7CiAgICAgCiAgICAgICAgZm9yKGxsIGo9MDtqPDM7aisrKXsKICAgICAgICAgICAgICAgIGlmKGo9PTApewogICAgICAgICAgICAgICAgICAgICAgICBhbnMuZl9uMiA9IHN1bShzdW0oZihwLmZfbjIsQS5hWzBdW2pdKSxmKHAuZl9uMSxBLmFbMV1bal0pKSxmKHAuZl9uLEEuYVsyXVtqXSkpOyAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZWxzZSBpZihqPT0xKXsKICAgICAgICAgICAgICAgICAgICAgICAgYW5zLmZfbjEgPSBzdW0oc3VtKGYocC5mX24yLEEuYVswXVtqXSksZihwLmZfbjEsQS5hWzFdW2pdKSksZihwLmZfbixBLmFbMl1bal0pKTsgCiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBlbHNlIGFucy5mX24gPSBzdW0oc3VtKGYocC5mX24yLEEuYVswXVtqXSksZihwLmZfbjEsQS5hWzFdW2pdKSksZihwLmZfbixBLmFbMl1bal0pKTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGFuczsKfQppbnQgbWFpbigpewogICAgLy8gZnJlb3BlbigiaW5wdXQudHh0IiwiciIsc3RkaW4pOwogICAgLy8gZnJlb3Blbigib3V0cHV0LnR4dCIsInciLHN0ZG91dCk7CiAgICBsbCB0YzsKICAgIGNpbj4+dGM7CiAgICBmb3IobGwgdT0xO3U8PXRjO3UrKyl7CiAgICAgbGwgdG1wWzNdWzNdPXsgezIsMSwwfSx7MCwwLDF9LHsxLDAsMH19OwogICAgIGxsIGRvbnZpWzNdWzNdPXt7MSwwLDB9LHswLDEsMH0sezAsMCwxfX07CiAgICAgZm9yKGxsIGk9MDtpPDM7aSsrKQogICAgICAgIGZvcihsbCBqPTA7ajwzO2orKykgewogICAgICAgICAgICAgICAgTS5hW2ldW2pdPXRtcFtpXVtqXTsKICAgICAgICAgICAgICAgIG1vdC5hW2ldW2pdPWRvbnZpW2ldW2pdOwogICAgICAgICAgICAgICAgfQogICAgIGluaXQuZl9uMj00OwogICAgIGluaXQuZl9uMT0yOwogICAgIGluaXQuZl9uPTE7Ci8vICAgICAgaW5pdC5wcmludCgpOwogICAgIGxsIG47CiAgICAgTU0gPSAxMDAwNzsKICAgICBjaW4+Pm47CiAgICAgTU9EPU1NOwogICAgIGlmKG49PTEpewogICAgICAgICBjb3V0PDwiQ2FzZSAiPDx1PDwiOiAiPDwxPDwnXG4nOwogICAgICAgICBjb250aW51ZTsKICAgICB9CiAgICAgaWYobj09Mil7CiAgICAgICAgIGNvdXQ8PCJDYXNlICI8PHU8PCI6ICI8PDI8PCdcbic7CiAgICAgICAgIGNvbnRpbnVlOwogICAgIH0KICAgICBib2JhIHJlc3MgPSBwcm9kMShpbml0LHBvKE0sbi0yKSk7CiAgICAgbGwgYWFucyA9IChyZXNzLmZfbjIlTU9ELShyZXNzLmZfbjElTU9EKStNT0QpJU1PRDsKICAgICBjb3V0PDwiQ2FzZSAiPDx1PDwiOiAiPDxhYW5zPDwnXG4nOwogICAgfQp9