#include<bits/stdc++.h>
using namespace std;
#define F(i,a,b) for(int i=a;i<=b;++i)
const int M1=1721424,M2=2299161,st=365*4+1;
int cldr[14]={0,1,32,60,91,121,152,182,213,244,274,305,335,10000};
int Q,r;
int date(int r,int t)//t=1表示闰年
{
int day=1,mth=1;
if(r<=31)printf("%d %d ",r,mth);
else if(r<=59+t)printf("%d %d ",r-31,2);
else{
while(cldr[mth+1]+t<=r)mth++;
printf("%d %d ",r-cldr[mth]-t+1,mth);
}
return 0;
}
void solve1(int r){
int t=r/st;
r-=t*st;
int ans=-4712+t*4;
if(r>366){r-=1;while(r>365){r-=365;ans++;};}
date(r,ans%4==0);printf("%d BC\n",-ans+1);
}
void solve2(int r){
int t=r/st;
r-=t*st;
int ans=1+t*4;
if(r>=365){r-=365;ans++;}
if(r>=365){r-=365;ans++;}
if(r>=365){r-=365;ans++;}
date(r,ans%4==0);printf("%d\n",ans);
}
void solve3(int r){
int t1=r/(st*100-3);r-=t1*(st*100-3);
int ans=1+t1*400;
if(r>=st*25-1){r-=st*25-1;ans+=100;}
if(r>=st*25-1){r-=st*25-1;ans+=100;}
if(r>=st*25-1){r-=st*25-1;ans+=100;}
int t2=r/st;r-=t2*st;ans+=t2*4;
if(r>=365){r-=365;ans++;}
if(r>=365){r-=365;ans++;}
if(r>=365){r-=365;ans++;}
date(r,(ans%4==0&&ans%100!=0)||ans%400==0);printf("%d\n",ans);
}
int main(){
cin>>Q;
F(i,1,Q){
scanf("%d",&r);r+=1;
if(r<=M1)solve1(r);
else if(r<=M2)solve2(r-M1);
else solve3(r-M1-2);
}
return 0;
}