#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll st=4713,da=365;
ll day[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
ll q,n,fohy,ohy=36524,fy=365*4+1,gongyuan=4713*365+1178,gery=1581*365+395;
int main()
{ cin>>q;
fohy=4*ohy+1;
for(int i=1;i<=9;++i)
gery+=day[i];
gery+=4;
while(q--)
{
cin>>n;
if(n==0)
cout<<"1 1 4713 BC"<<endl;
else if(n<=gongyuan)
{
ll pos1,t=(n/fy)*4;
n%=fy;
t+=(n/da);
n%=da;
for(int i=1;i<=12;++i)
if(n>day[i])
n-=day[i];
else
{
pos1=i;
break;
}
if(n==0)
n=31,pos1=12,t--;
if(t==0 && pos1==1)
cout<<n+1<<' '<<pos1<<' '<<st-t<<" BC"<<endl;
else
cout<<n<<' '<<pos1<<' '<<st-t<<" BC"<<endl;
continue;
}
else if(n-gongyuan<=gery)
{
n-=gongyuan;
ll pos1,t=0;
t+=(n/fy)*4;n%=fy;
t+=(n/da);n%=da;
for(int i=1;i<=12;++i)
if(n>day[i])
n-=day[i];
else
{
pos1=i;
break;
}
if(n==0)
n=31,pos1=12,t--;
cout<<n<<' '<<pos1<<' '<<t+1<<endl;
continue;
}
else if(n-gery-gongyuan<=6287+366)
{
n-=(gery+gongyuan);
ll pos,t;
if(n<=16)
cout<<n+15<<" 10 1581"<<endl;
else if(n<=47)
cout<<n-16<<" 11 1581"<<endl;
else if(n<=78)
cout<<n-47<<" 12 1581"<<endl;
else
{
n-=78;pos=-1;
for(int i=1583;i<=1600;++i)
{
for(int j=1;j<=12;++j)
{
int x=day[j];
if(i%4==0 && j==2)
++x;
if(n<=x)
{
pos=j,t=i;
break;
}
else
n-=x;
}
if(pos!=-1)
break;
}
cout<<n<<' '<<pos<<' '<<t<<endl;
}
continue;
}
else
{
n-=(gery+gongyuan+6287+366);
ll pos1,t=0;
t+=(n/fohy)*400;n%=fohy;
t+=(n/ohy)*100;n%=ohy;
t+=(n/fy)*4;n%=fy;
t+=(n/da);n%=da;
if(n==0)
{
cout<<31<<' '<<12<<' '<<t+1600<<endl;
continue;
}
for(int i=1;i<=12;++i)
{
int x=day[i];
if((((t+1)%4==0 && (t+1)%100!=0) || (t+1)%400==0) && i==2)
++x;
if(n>x)
n-=x;
else
{
pos1=i;
break;
}
}
cout<<n<<' '<<pos1<<' '<<t+1601<<endl;
continue;
}
}
return 0;
}