#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int n,m;
int main()
{
    int T;
    scanf("%d",&T);
    for(int cas=1;cas<=T;++cas)
    {
        printf("Case #%d:\n",cas);
        scanf("%d%d",&n,&m);
        int s=1;
        for(int i=1;i<=3;++i){
            if((i+i+n-1)*n/2%3==0){
                s=i;
                break;
            }
        }
        for(int i=s;i<s+n;++i)
        {
            printf("%d %d %d\n",i-s+1,(i-s+1)%n+1,i);
        }
        int num[3];
        memset(num,0,sizeof num);
        for(int i=1;i<s;++i) ++num[i%3],cout<<i<<endl;
        for(int i=s+n;i<=m;++i) ++num[i%3],cout<<i<<endl;
        cout<<"s="<<s<<endl;
        for(int i=0;i<n;++i)
        {
            int sum=(i+s)%3;
            for(int j=(i+1)%n;j!=i-1;j=(j+1)%n)
            {
                sum=(sum+j+s)%3;
                if(num[sum]>0){
                    printf("%d %d %d\n",i+1,(j+1)%n+1,sum);
                    --num[sum];
                }
                cout<<"sum="<<sum<<endl;
                getchar();
            }
        }
    }
    return 0;
}