#include<bits/stdc++.h>
#define mx 0x3f3f3f3f

using namespace std;

int make[1000005]={0,1,2,3},cnt[105];

int main()
{

    int test,n,m,k,i,j,kcnt,ans,x;

    scanf("%d",&test);

    for(i=1;i<=test;i++)
    {
        scanf("%d%d%d",&n,&m,&k);

        for(j=4;j<=n;j++)
        {
            make[j] = (make[j-1] + make[j-2] + make[j-3]) % m + 1;
        }

        ans = mx;

        kcnt = 0;

        x = 1;

        for(j=1;j<=n;j++)
        {
            if(make[j]<=k)
            {
                if(!cnt[make[j]])
                {
                    kcnt++;
                }

                cnt[make[j]]++;
            }

            if(kcnt==k)
            {
                while(make[x]>k || cnt[make[x]]>1 )
                {
                    if(make[x]<=k && cnt[make[x]]>1)
                    {
                        cnt[make[x]]--;
                    }

                    x++;
                }

                ans = min(ans,j-x+1);
            }
        }

        if(ans==mx)
        {
            printf("Case %d: sequence nai\n",i);
        }
        else
        {
            printf("Case %d: %d\n",i,ans);
        }

        memset(cnt,0,sizeof cnt);
    }

    return 0 ;
}
